mysql timestamp compare date string - mysql

we have a store procedure, the IN parameter is DATE today. in this procedure, a aql is to compare this today value with a table which has a timestamp column.
for example:
column A
2012-12-01 00:00:00
SQL:
select * from t where A = today.
We run this procedure in phpmyadmin, it run OK. but it's not work in command line.
Why?

Guess you may need to format both dates into a common format.. To be safe you may even add Date() or str_to_Date if required...if you are not sure column A contains a proper date...
Try this please:
SELECT * FROM tablename
WHERE DATE_FORMAT(A, '%d/%m/%Y') = DATE_FORMAT(TODAY, '%d/%m/%Y');
if you meant CURDATE() by today then try this as well,
SELECT * FROM tablename
WHERE DATE_FORMAT(A, '%d/%m/%Y') = DATE_FORMAT(CURDATE(), '%d/%m/%Y');

It's possible that the dates are in different formats and that's causing them to be not equal. You can use datediff(date1, date2) = 0 to fix this.
http://www.w3schools.com/sql/func_datediff_mysql.asp

Related

Like with Date Function

I have a table that has dates stored as text and not as a date_time. I want to select rows that happened today. I can hard code the date, but obvious I want to do it dynamically.
SELECT * FROM view WHERE anniversary LIKE '%09-07'
SELECT * FROM view WHERE anniversary LIKE '%' + DATE_FORMAT(now(), "%m-%d")
First, you should be storing dates as dates, not as strings.
But, if they are stored as strings, you can use a comparison like this:
where right(anniversary_datestring, 5) = str_to_date(curdate(), '%m-%d')
But, I recommend fixing your data. If the values are in the format YYYY-MM-DD, simply do:
alter table t modify column anniversary date;
The month and day operators can be used:
SELECT * from view WHERE MONTH(anniversary) = MONTH(NOW()) AND DAY(anniversary) = DAY(NOW())
if the column is of type varchar the STR_TO_DATE function can be used to turn it into a date:
SELECT * from view WHERE MONTH(STR_TO_DATE(anniversary,'%Y-%m-%d')) = MONTH(NOW()) AND DAY(STR_TO_DATE(anniversary,'%Y-%m-%d')) = DAY(NOW())
This should work,
convert string to date and check in where clause
SELECT * FROM view WHERE STR_TO_DATE(anniversary, '%d-%m-%Y') = CURDATE()

How to convert a date string to mysql date format for calculating date diff using mysql query

I have a column where a date store in ddmmyy format (e.g. 151216). How can I convert it to yyyy-mm-dd format (e.g 2016-12-15) for calculating a date difference from the current date? I try using DATE_FORMAT function but its not appropriate for this.
If you want to get the date difference, you can use to_days() after converting the string to a date using str_to_date():
select to_days(curdate()) - to_days(str_to_date(col, '%d%m%y'))
or datediff():
select datediff(curdate(), str_to_date(col, '%d%m%y'))
or timestampdiff():
select timestampdiff(day, str_to_date(col, '%d%m%y'), curdate())
You can use the function, STR_TO_DATE() for this.
STR_TO_DATE('151216', '%d%m%y')
A query would look something like:
select
foo.bar
from
foo
where
STR_TO_DATE(foo.baz, '%d%m%y') < CURDATE()
Note: Since both STR_TO_DATE() and CURDATE() return date objects, there's no reason to change the actual display format of the date. For this function, we just need to format it. If you wanted to display it in your query, you could use something like
DATE_FORMAT(STR_TO_DATE(foo.baz, '%d%m%y'), '%Y-%m-%d')
To get the difference, we can simply subtract time
select
to_days(CURDATE() - STR_TO_DATE(foo.baz, '%d%m%y')) as diff
from
foo
If you wanted to only select rows that have a difference of a specified amount, you can put the whole to_days(...) bit in your where clause.
SELECT STR_TO_DATE('151216', '%d%m%y') FROM `table`
use this '%d%m%y'

Common date format function for Oracle-sql and Mysql

I need to write the common queries for MySQL and Oracle databases. Problem occurs when I have to put date conditions.
For example: there is one field Txn_date which is in format of '20150116' in MySQL and '16-JAN-2015' in Oracle.
I use date_format(now(),'%Y%m%d') for MySQL and to_char(sysdate,'dd-MON-YYYY') for Oracle.
Is there any common function of way by which I can use the same function in both Oracle and MySQL?
I tried Txn_date in ( date_format(now(),'%Y%m%d') OR to_char(sysdate,'dd-MON-YYYY') ) but did not work because to_char() not recognized in MySQL.
First, in MySQL dates usually have the following format when converted implicitly - 2015-01-16 - rather than 20150116. I think you can do the following in both MySQL and Oracle (it is standard SQL) - I've checked it in Oracle (10g) and it works, and it seems to work in my fiddling with MySQL:
SELECT * FROM mytable
WHERE mydate IN ( DATE '2015-01-16', DATE '2015-01-18' );
The string literal to be converted to DATE has to be of the form yyyy-mm-dd. Now this will work if your dates are dates and don't have a time portion. Now if your dates do have a time portion, then things become more difficult since MySQL uses the DATE() function to get the date portion, while Oracle would use TRUNC(). But you can get around that with judicious use of >= and <, e.g.:
SELECT * FROM mytable
WHERE ( mydate >= DATE '2015-01-16' AND mydate < DATE '2015-01-17' )
OR ( mydate >= DATE '2015-01-18' AND mydate < DATE '2015-01-19' );
Now if you want to use SYSDATE, the best thing to do would be to use the ANSI standard CURRENT_DATE or CURRENT_TIMESTAMP. These can be compared directly with no need for formatting and should work in both MySQL and Oracle. You can also do date arithmetic using INTERVAL, in which case you could try the following:
SELECT * FROM mytable
WHERE mydate > CURRENT_DATE - INTERVAL '1' DAY;
UPDATE I've been doing some thinking about this. The query immediately above doesn't really work if you want to get all the rows that have been entered today. The difficulty is that Oracle recognizes ANSI date literals as dates (that is, with no time portion), but there isn't, as far as I know, an ANSI-standard way of converting a date/time value (which an Oracle DATE is) to a date. That said, both Oracle and MySQL support the EXTRACT() function, so you should be able to do the following to get today's records:
SELECT * FROM mytable
WHERE EXTRACT(YEAR FROM mydate) = EXTRACT(YEAR FROM CURRENT_DATE)
AND EXTRACT(MONTH FROM mydate) = EXTRACT(MONTH FROM CURRENT_DATE)
AND EXTRACT(DAY FROM mydate) = EXTRACT(DAY FROM CURRENT_DATE);
Definitely unwieldy, especially if one has more than one date to consider (which I assume you do since you're using the IN operator), but should work on both platforms. See SQL Fiddle Demo here (MySQL) and here (Oracle).
MySQL and Oracle use different syntax for converting dates to strings - you'll have to use different queries.

mysql statement (SELECT) based on date now

I would like to ask if my Mysql statement is correct or not.. When I run this under mysql it does not return any error but I cannot retrieve the row for it. Here's my statement:
SELECT * FROM timekeeping WHERE createddate = NOW()
Here's what my table looks like
MySQL compare now() (only date, not time) with a datetime field
Try this:
SELECT * FROM timekeeping WHERE DATE(createddate) = DATE(NOW());
Most likely the createddate = NOW() is an exact time comparison , you are probably only interested in the year, month, day being the the same.
See here for details on how to do what you are trying to do:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Probably you want to search for today date.
Try this:
SELECT * FROM timekeeping WHERE DATE(createddate) = DATE(NOW());
now() includes the time. Given that your fields contain date AND time values, you'll only ever get a match if the date AND time are exact matches. You need to compare dates only:
... WHERE DATE(createddate) = CUR_DATE()
'2014-05-02' = '2014-05-02'
v.s.
... WHERE createddate = now()
'2014-05-02 22:14:00' = '2014-05-02 01:02:03'
My guess is that this isn't exactly what you want: NOW() function will return the exact timestamp for when the query is run, which means you are asking it for any records created at that exact moment in time.
You may want to try something more like:
SELECT * FROM timekeeping WHERE createddate = YOUR_DATE_CRITERIA

SQL NOW() function not working properly

I'm trying to insert the date in a a query, using the NOW() statement.
However only the Y-m-d are being inserted correctly, while the hours, minutes and seconds are all appearing zeros ( 00:00:00 )
Any reason for that?
Did you check the type of the column you are inserting into? Make sure it's datetime, not just date.
Refer to the docs for more info.
Try this :
SELECT CURRENT_TIMESTAMP
or
SELECT GETDATE()
or
Select {fn NOW()}
Note the accolades in the function.