I have this query
SELECT * FROM tracklogs.sms_outbound
WHERE gsmno = 'rk4#*******.com.ph'
AND cdate > cast('2013/11/14 09:44:48 PM' as datetime)
where cdate format is in %Y-%m-%d %h:%i:%s %p.
I have tried converting the date into that format then cast it as datetime but still doesn't working.
Use STR_TO_DATE() to correctly convert the datetime literal you have provided to a proper DATETIME value. It seems that your cdate column is a char() or varchar() column. So you will also need to convert that to DATETIME to compare it.
What you need is this:
That works like this (http://sqlfiddle.com/#!2/d41d8/48741/0)
STR_TO_DATE(cdate, '%Y-%m-%d %h:%i:%s %p') >
STR_TO_DATE('2013/11/14 09:44:48 PM', '%Y/%m/%d %h:%i:%s %p')
Converting these strings to DATETIME data items ensures that the comparison handles both the date and the time correctly. See this fiddle (http://sqlfiddle.com/#!2/d41d8/48743/0)
But, you should consider changing your cdate item to a DATETIME, because then you'll be able to index it and speed up your search.
SELECT * FROM tracklogs.sms_outbound
WHERE gsmno = 'rk4#*******.com.ph'
AND UNIX_TIMESTAMP(str_to_date(cdate,'%Y-%m-%d %H:%i:%s')) > UNIX_TIMESTAMP('2013-11-14 09:44:48')
Related
how to compare time with current time but with AM pm format also compare in mysql select query.
I added Database image where stored time in this formate and i compare to current time but i didn't get success.
You need to convert expectedDeliveryDateTime to a date so you can compare it with NOW(). To do that you need to use STR_TO_DATE with the format '%d/%m/%Y %h:%i %p' which matches the data in your image. So try:
SELECT *
FROM yourtable
WHERE STR_TO_DATE(expectedDeliveryDateTime, '%d/%m/%Y %h:%i %p') >= NOW()
I have assumed you are looking for dates in the future, if you want dates in the past just change >= NOW() to <= NOW().
use now()and format date using date_formatas your column is varchar so you have to convert it date by using STR_TO_DATE function
select * from t where
STR_TO_DATE(expectedDeliveryDateTime, '%d/%m/%Y %h:%i %p') >=date_format(now(), '%d/%m/%Y %h:%i %p')
Please, check my time zone. Are we the same?
SELECT UTC_TIMESTAMP() from dual;
If the same, then you compare is correct.
I have a field, 'mydate' in my database which displays the date as a string like this
'8/1/2015 12:35:22 PM'
I am trying to convert this into a unix timestamp i have tried
cast(UNIX_TIMESTAMP('mydate',"yyyy-MM-dd HH:mm:ss.SSSS")) AS new_date
This has resulted in the query not completing completely. How can i convert this 'mydate' field into a unix timestamp.
Try something like this:
UNIX_TIMESTAMP(STR_TO_DATE(mydate, '%M %e %Y %h:%i%p'))
You can simply check in mysql string format -
SELECT STR_TO_DATE('8/1/2015 11:35:22 PM','%d/%m/%Y %h:%i:%s %p');
UPDATE blogs SET start_date = '11/27/2012 00:00',end_date = '11/27/2012 00:00' WHERE id='9'
This query won't store start_date or end_date values for blog id 9 unless I set them to varchar type.
Tried with timestamp and date time but the query allways will return that 0 rows where affected
the thing is I need to be able to check for rows in a interval of time, and with varchar it makes very complicated.
What am i missing?
As stated in Date and Time Literals:
MySQL recognizes DATETIME and TIMESTAMP values in these formats:
As a string in either 'YYYY-MM-DD HH:MM:SS' or 'YY-MM-DD HH:MM:SS' format. A “relaxed” syntax is permitted here, too: Any punctuation character may be used as the delimiter between date parts or time parts. For example, '2012-12-31 11:30:45', '2012^12^31 11+30+45', '2012/12/31 11*30*45', and '2012#12#31 11^30^45' are equivalent.
As a string with no delimiters in either 'YYYYMMDDHHMMSS' or 'YYMMDDHHMMSS' format, provided that the string makes sense as a date. For example, '20070523091528' and '070523091528' are interpreted as '2007-05-23 09:15:28', but '071122129015' is illegal (it has a nonsensical minute part) and becomes '0000-00-00 00:00:00'.
As a number in either YYYYMMDDHHMMSS or YYMMDDHHMMSS format, provided that the number makes sense as a date. For example, 19830905132800 and 830905132800 are interpreted as '1983-09-05 13:28:00'.
Therefore, the strings '11/27/2012 00:00' and '11/27/2012 00:00' are not valid MySQL datetime literals. You have two options (in some vague order of preference, without any further information of your requirements):
Provide your literals in a recognised format:
UPDATE blogs SET
start_date = '2012-11-27 00:00:00',
end_date = '2012-11-27 00:00:00'
WHERE id = 9
Use MySQL's STR_TO_DATE() function to convert the string:
UPDATE blogs SET
start_date = STR_TO_DATE('11/27/2012 00:00', '%m/%d/%Y %H:%i'),
end_date = STR_TO_DATE('11/27/2012 00:00', '%m/%d/%Y %H:%i')
WHERE id = 9
Try this:
UPDATE blogs
SET start_date = STR_TO_DATE('11/27/2012 00:00', '%m/%d/%Y %h:%i:%s') --cast string to date in correct date format
,end_date = STR_TO_DATE('11/27/2012 00:00', '%m/%d/%Y %h:%i:%s')--cast string to date in correct date format
WHERE id = 9 --removed quotes as this field's probably numeric
If using a timestamp column instead of datetime I think you need to do something like this:
UPDATE blogs
SET start_date = UNIX_TIMESTAMP(STR_TO_DATE('11/27/2012 00:00', '%m/%d/%Y %h:%i:%s'))
,end_date = UNIX_TIMESTAMP(STR_TO_DATE('11/27/2012 00:00', '%m/%d/%Y %h:%i:%s'))
WHERE id = 9
The time is stored in the database as '09:21 am'
Now how can i get the range result
for instance
select * from tablename where stored_time between '09:21 am' and '10:05 pm';
I tried the above query but it's not giving the result properly. this is because the time is stored as varchar.
is there any function to convert this string to time and query it in mysql?
Using STR_TO_DATE() with the following format:
SELECT STR_TO_DATE(UPPER('09:21 am'), '%h:%i %p');
/* 09:21:00 */
%h = Hours 00-12
%i = Minutes
%p = AM/PM (UPPER() converts to uppercase so it matches your lowercase version)
So the full query:
SELECT *
FROM tablename
WHERE
/* Use 24h time for the comparison values and wrap stored_time in the STR_TO_DATE() */
STR_TO_DATE(UPPER(stored_time)) between '09:21:00' and '22:05:00';
It is recommended that you switch this column to a real TIME or DATETIME type rather than storing the value as a string.
you can make use of DATE_FORMAT(date,format). Documentation
select * from tablename where DATE_FORMAT(stored_time,'%h:%i %p') between
DATE_FORMAT('09:21 am','%h:%i %p') and DATE_FORMAT('10:05 pm','%h:%i %p');
I'm trying to create a query using mysql.
select ID,NCOde,ifnull(EndTime,now())-starttime from xxx where starttime between
'2012-05-09 00:00:00' and '2012-05-09 23:59:59'
the problem is ifnull(EndTime,now()) return datetime in 24 hours format, while the starttime using am/pm format.
I've tried using DATE_FORMAT(starttime, '%m-%d-%Y %T'), but it seems that the operation changed the datetime type to other type.
Any advice?
Use STR_TO_DATE() to convert your starttime string to a MySQL DATETIME:
STR_TO_DATE(starttime, '%m-%d-%Y %r')
and then use TIMEDIFF() to subtract two times:
select ID,NCOde,
TIMEDIFF(ifnull(EndTime,now()), STR_TO_DATE(starttime, '%m-%d-%Y %r'))
from xxx
where STR_TO_DATE(starttime,'%m-%d-%Y %r')
between '2012-05-09 00:00:00' and '2012-05-09 23:59:59'
You should probably consider changing the data type of the starttime column to DATETIME or TIMESTAMP. Note also that this assumes EndTime is already of such a data type, or else you will also have to perform a similar conversion with it too.
Use the DATE_SUB() function.
Plus what eggyal said.