Working with one of my applications, I am adding two databases support to my project, but I am very limited in my knowledge with MSSQL. My MySQL code is like below
SELECT count(ip_address) AS failed_login_attempt
FROM failed_login
WHERE ip_address = '$ip'
AND date BETWEEN DATE_SUB( NOW() , INTERVAL 1 DAY ) AND NOW()
and I am trying to do same kind of functionality with MSSQL:
SELECT count(ip_address) AS failed_login_attempt
FROM failed_login
WHERE ip_address = '$ip'
AND date between [tHIS pIECE OF cODE I DO NOT KNOW HOW TO FIx ]
SQL Server has several ways of returning the current date, for example getdate(). To subtract one day from the current datetime use the dateadd function. So this:
BETWEEN DATE_SUB( NOW() , INTERVAL 1 DAY ) AND NOW()
should be equivalent to
BETWEEN dateadd(day, -1, getdate()) and getdate()
Related
I'm trying to select my table where I'm looking at things that happened today.
I have it set up as:
WHERE date between CURRENT_DATE and DATE_ADD(CURRENT_DATE, 1, DAY);
I keep getting the error that DATE_ADD is not a valid identifier. I'm using mySQL.
I also tried DATEADD() but I don't think that one works in MYSQL.
Do not use between for this! That includes both endpoints. Instead:
where date >= current_date and
date < current_date + interval 1 day
Or:
where date >= current_date and
date < date_add(current_date, interval 1 day)
You (presumably) do not want midnight between today and tomorrow.
By the way, if date is really just a date and not a datetime, the above will work, but you can also write:
where date = current_date
I strongly discourage you from using:
where date(date) = current_date
because this prevents an index on date from being used.
Sometimes people confuse MySQL and Oracle because Oracle owns both. In Oracle, this would look like:
where date >= current_date and
date < current_date + interval '1' day
That would actually work in MySQL as well. Or using Oracle-specific features:
where date >= trunc(sysdate) and
date < trunc(sysdate) + interval '1' day
Issue with your code: DATE_ADD() accepts a date as first argument and an interval expression as second argument
Consider:
WHERE mydate BETWEEN CURRENT_DATE() AND DATE_ADD(CURRENT_DATE(), INTERVAL 1 DAY);
Or you can use ADDDATE(), which, as second argument, can accept either an interval or a number of days
WHERE mydate BETWEEN CURRENT_DATE() AND ADDDATE(CURRENT_DATE(), 1);
Note 1: please note that CURRENT_DATE() is a function. Although MySQL also accepts spelling CURRENT_DATE and CURRENT_DATE(), I find that that the parentheses make the purpose clearer.
Note 2: as for DATEADD(), it just does not exist in MySQL.
older MySQL versions you can do like this.
where date between curdate() and curdate() + 1;
and this is still supported up to this current versions of mySQL.
I'm trying to create views in MySQL Workbench which would serve me to extract weekly reports, that covers the period between Monday and Sunday.
I don't know how to write it dynamically so that the date gets updated by itself.
This is the query i wrote:
SELECT from_unixtime(date, '%d/%m/%Y'), count(distinct(playerId)) FROM innodb.player_spin
WHERE date between 1512950400 and 1513555199
GROUP BY from_unixtime(date, '%d/%m/%Y');
I usually work in T-SQL, so I cannot confirm that this works by running it.
Use the Weekday function and some date math
SELECT from_unixtime(date, '%d/%m/%Y'), count(distinct(playerId)) FROM
innodb.player_spin
where date between (Date_ADD(CURDATE(), INTERVAL (0 - WEEKDAY(CURDATE()) DAY) and (Date_ADD(CURDATE(), INTERVAL (0 - WEEKDAY(CURDATE() +7) DAY)
group by from_unixtime(date, '%d/%m/%Y');
I have a joomla website and I want to make a query that it could run with a cronjob every day. It would look for any articles 30 days old and delete the articles this specific day.
I can make a simple query like this:
SELECT * FROM `ibps6_content`
WHERE created > '2013-10-16' AND created < '2013-10-16'
But I don’t know how to make it specify the last 30 days, instead of hardcoding the dates.
Which rdbms?
In oracle you can use select based on sysdate which represents the current date/time.
Example:
select * from ibps6_content WHERE created between sysdate - 30 and sysdate
In T-SQL, you could use the following:
SELECT
*
FROM
ibps6_content
WHERE
created BETWEEN DATEADD(DAY, -30, GETDATE()) AND GETDATE()
Note that this will give you the articles written up to the specific time that you run the article. To remove the times and utilize just the dates, you could use:
DECLARE #CurDate DATETIME;
SET #CurDate = DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()));
SELECT
*
FROM
ibps6_content
WHERE
created BETWEEN DATEADD(DAY, -30, #CurDate) AND #CurDate
If created is a date datatype in the table
DELETE FROM ibps6_content WHERE created = CURRENT_DATE -INTERVAL 30 DAY
If it's datetime use DATE(created)
You need mysql DATE_FORMAT(date,format) function.
Please try to set your query as:
DELETE FROM `ibps6_content` WHERE created > DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m-%d');
Hope this helps
I want to select from a MySQL table and filter depending on the time of day:
if now > 10am select uploaded date where created date is today
if now < 10am select uploaded date where created date is yesterday
I think you must use Cron jobs for this.
Then, your sql will look like that:
WHERE tbl.TimeCreated = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
assuming this format of the field: DateTime(YYYY-MM-DD HH:MM:SS)
This should do what you need.
SELECT
`uploaded_date`
FROM
`table`
WHERE
`created_date` = CASE WHEN CURTIME() > 100000
THEN CURDATE()
ELSE CURDATE() - INTERVAL 1 DAY
END
You can see a similar example in this fiddle which should return a single row of "AM" in the morning, "Noon" at noon, and "PM" in the afternoon (and 2 empty result sets).
I am trying to construct the SQL query, that should be executed as is
both on HSQL (v2.2.4) and MySQL (v5.1.36) server (if it can run also on DB2 v9, that would be wonderful bonus!)
The query is:
select count(*) from document where current_date - cast(indexing_date as date) <= ?
(here current_date is a standard HSQL/MySQL function and indexing_date is a column with type datetime, parameter ? is substituted by integer 20 which is the number of days).
The problem is that MySQL returns the difference between dates as between numbers while HSQL returns the difference in days (which is logical when you subtract date from date).
Also HSQL supports this syntax (but MySQL does not):
select count(*) from document where cast(indexing_date as date) between current_date - 20 day and current_date
while MySQL does not. I am aware about DATEDIFF() in MySQL, but as I said the solution should be inter-operable.
HSQLDB also supports this:
select count(*) from document where current_date - cast(indexing_date as date) <= cast(? as interval day)
and
select count(*) from document where cast(indexing_date as date) between current_date - '20' day and current_date
or
select count(*) from document where indexing_date >= current_date - interval '20' day
Also, from version 2.2.6, HSQLDB supports DATEDIFF(datevaluea, datevalueb), which returns the number of days between the two dates, as well as DAYS(datevalue), which returns the day number since the epoch.
db2:
SELECT COUNT(*)
FROM document
WHERE DATE(indexing_date) BETWEEN DATE(DAYS(CURRENT DATE) - 20) AND CURRENT DATE