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
Related
I'm trying to use sql CURDATE(), in format "YYYY-MM-DD", to select items from yesterday but time column is in "YYYY-MM-DD HH-MM-SS" format.
Is it possible to put CURDATE between % %, something like in this example (which obviously doesn't work);
SELECT * FROM table WHERE created_at LIKE % CURDATE() - INTERVAL 1 day %
Or if there is no syntax variation which makes use of curdate & also that works, do please recommend another way if there is any in sql bag of tricks.
Don't use string functions as strings! If you want yesterday, you can use:
where created_at >= current_date - interval 1 day and
created_at < current_date
Plus, this can use an index.
Or, you can simplify this to:
where date(created_at) = current_date - interval 1 day
And this version does not use an index on created_at.
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 am using GoDaddy webserver of shared hosting.I have a mysql database in which there are 2 columns which contains datatype date.. I am using following sql queries. From query1 I am getting Expired records & from query2 getting ongoing records. Problem is because webserver uses it's local time so as per indian time zone I am not getting accurate records. Is there any way to fix problem in query.
Query1
SELECT * FROM offers WHERE startDate <= CURRENT_DATE AND endDate <= CURRENT_DATE
Query2
SELECT * FROM offers WHERE startDate <= CURRENT_DATE AND endDate >= CURRENT_DATE
If your values in database are stored in IST then below should work.
Query 1
SELECT * FROM offers WHERE DATE_ADD(startDate,INTERVAL -330 MINUTE) <=
CURRENT_DATE AND DATE_ADD(endDate,INTERVAL -330 MINUTE) <=
CURRENT_DATE
Query2
SELECT * FROM offers WHERE DATE_ADD(startDate,INTERVAL -330 MINUTE) <= CURRENT_DATE AND DATE_ADD(endDate,INTERVAL -330 MINUTE) >=
CURRENT_DATE
You need to convert the time zones. I assume MySql is using UTC time by default.
SELECT * FROM offers WHERE CONVERT_TZ(startDate,'UTC','Asia/Kolkata') <= CURRENT_DATE AND CONVERT_TZ(endDate,'UTC','Asia/Kolkata') <= CURRENT_DATE;
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz
You need to check time zone tables.
SELECT * FROM mysql.time_zone;
SELECT * FROM mysql.time_zone_name;
http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
Mysql uses these tables to convert th time zones. If above tables are empty. You have to fill it.
shell> mysql_tzinfo_to_sql /usr/share/zoneinfo
more info here http://dev.mysql.com/doc/refman/5.5/en/mysql-tzinfo-to-sql.html
I'm working on a project where I want to display data after 3 days have passed.
What I'm having an issue with is getting the current date dynamically in php/sql. I'm aware of how to get the current date in php, but I dont know how to compare that value to the date that I have in the sql database.
You can do that directly in SQL
select * from your_table
where date_column <= curdate() - interval 3 day
You can use an interval select to limit the records to within 3 days if the column you're checking istimestamp, date, or datetime.
select * from tablename where timestamp_column >= NOW() - INTERVAL 3 DAY
You can use DATEDIFF function to check for days.
SELECT *
FROM tablename
WHERE DATEDIFF(CURRENT_DATE(), datecol) >= 3;
I have two fields, one with the purchase date and one with the lifespan of an item. I am wondering if there is any way to determine if the current date is past the lifespan. So, pretty much if MySQL could test if current_date is > purchase_date + lifespan.
The purchase date is a date formated yyyy-mm-dd and lifespan is an integer of months. Is there any way to perform this calculation?
You might try:
SELECT * FROM table WHERE NOW() > (purchase_date + INTERVAL lifespan MONTH)
But you might use
SELECT * FROM table WHERE NOW() > date_add(purchase_date, INTERVAL lifespan MONTH)
Or DATEDIFF
SELECT * FROM table WHERE DATEDIFF(NOW(), purchase_date) / 30 < lifespan
How about using DATEDIFF?
Your query would be something along these lines:
SELECT * FROM table WHERE DATEDIFF(NOW(), purchase_date) < lifespan
Forgive me if my syntax is a little off, I don't have an SQL instance to test on right now
Yes it is possible, using the MySQL date and time functions.