I am new to mysql. I need to insert the string '2014-07-10 13:33:33' into the table column which has datetime datatype.
I gave like this,
SELECT STR_TO_DATE('2014-07-09 23:30:00', '%Y/%m/%d %h:%m:%s');
But i didn't not give the result.
How to do this?
Minutes is %i, not %m and 24-hour format is %H, not %h:
SELECT STR_TO_DATE('2014-07-09 23:30:00', '%Y-%m-%d %H:%i:%s');
Shouldnt it be
SELECT STR_TO_DATE('2014-07-09 23:30:00', '%Y-%m-%d %H:%i:%s');
%Y for year numeric, four digits
%m for month numeric
%d for Day of the month, numeric
%H for 24 hours
%i for minutes
%s for seconds
Related
I have a varchar dating field that reads as so - xx/xx/xxxx
I need to return the max as xx/xx/xxxx AM(or PM)
I can't figure out how to get it to return the max correctly while including AM/PM
I have been playing around with
SELECT DATE_FORMAT(max(STR_TO_DATE(pg_date_, '%c/%e/%Y %H:%i')), '%Y-%m-%d %H:%m:%s') from cas_compliance.failedrefunds2
I can't get to return quite the way I need it.
Thanks!
To convert xx/xx/xxxx to xx/xx/xxxx AM(or PM), you can try the following:
SELECT DATE_FORMAT(MAX(STR_TO_DATE(pg_date_,
'%c/%e/%Y %H:%i')
),
'%c/%e/%Y %r')
FROM cas_compliance.failedrefunds2
Details:
%r Time in 12 hour AM or PM format (hh:mm:ss AM/PM)
%H Hour (00 to 23)
%i Minutes (00 to 59)
%c Numeric month name (0 to 12)
%e Day of the month as a numeric value (0 to 31)
%Y Year as a numeric, 4-digit value
select DATE_FORMAT(now(), "%d-%m-%y %r");
Convert your date using above formating will solve problem.
select from_unixtime(floor(1510156036741/1000), '%Y %D %M %h:%i:%s');
'2017 8th November 07:47:16'
The above code outputs a timestamp which doesn't include millisecond precision. How would I convert an unix epoch time to a datetime(3) object which includes milliseconds?
It's possible to get microseconds like:
select from_unixtime(1510156036741/1000, '%Y %D %M %h:%i:%s %f');
'2017 8th November 07:47:16 741000'
Is it possible to convert the microsecond result to milliseconds?
Its a bit clunky, but you can extract the microsecond and divide it by 1000 to convert it to milliseconds. Here I've also cast it to an int and concatinated it with the formatted date to create the final date string:
SELECT CONCAT(
from_unixtime(1510156036741/1000, '%Y %D %M %h:%i:%s '),
CAST(EXTRACT(MICROSECOND FROM from_unixtime(1510156036741/1000))/1000
AS SIGNED)
);
"2017 8th November 03:47:16 741"
I have a column (lastlogin) that contains the following value = '17th May 2017 09:40:43 AM' ----
php function to create stamp:
date('jS F Y h:i:s A');
How can I do a select query that shows the (lastlogin) in that last 30 minutes from current time?
I'm guessing we have to convert date/time to string and separate the 2?
Any help would be amazing.
UPDATE:
I've tried the following but did not work returned more aless of records in table:
SELECT user_id, ('lastlogin' >= DATE_FORMAT(NOW() - INTERVAL 30 MINUTE,'%D %M %Y %H:%m:%s %p')) as result
FROM login_last
where lastlogin >= DATE_FORMAT(NOW() - INTERVAL 30 MINUTE,'%D %M %Y %H:%m:%s %p')
GROUP BY user_id
TABLE Structure
[id] int(8)
[user_id] int(11)
[lastlogin] varchar(30)
[browser] varchar(300)
You will need to convert your string date to a datetime format for the comparison, then compare the current datetime to the value stored.
You could also convert them to sortable strings for comparison. Something like "2017-05-17 10:20:10 AM", but it's easier to compare using the datetime type.
Reference:
str_to_date
date_format
MySQL Query:
SELECT
`user_id`,
`last_login`,
(STR_TO_DATE(`lastlogin`,'%D %b %Y %h:%i:%s %p') >= (NOW() - (INTERVAL 30 MINUTE))) as `in_range`
FROM `login_last`
WHERE (STR_TO_DATE(`lastlogin`,'%D %b %Y %h:%i:%s %p') >= (NOW() - (INTERVAL 30 MINUTE)))
SELECT * FROM tablename WHERE lastlogin >= DATE_SUB(NOW(), INTERVAL 30 MINUTE);
The challenge is to convert the result of DATE_SUB(NOW(), INTERVAL 30 MINUTE) to the correct format '17th May 2017 09:40:43 AM' (or format the left condition alias your lastLogin to UNIX_TIMESTAMP / datetime). In case you are inserting the date values yourself you probably already know how to get that format and can complete that yourself. Otherwise I recommend you to create a new question which is about formatting UNIX_TIMESTAMPs to your desired format.
A blind guess without having the chance to try that on my on would be:
SELECT * FROM tablename
WHERE UNIX_TIMESTAMP(lastlogin) >= DATE_SUB(NOW(), INTERVAL 30 MINUTE);
I've tried using
FROM_UNIXTIME(`date`)
and got a yyyy/mm/dd hour
how can I reverse it, that is have it as
hh:mm:secs mm/dd/yyyy
Thanks
Add specifiers to FROM_UNIXTIME or use DATE_FORMAT:
Specifiers
%T Time, 24-hour (hh:mm:ss)
%m Month, numeric (00..12)
%d Day of the month, numeric (00..31)
%Y Year, numeric, four digits
SELECT DATE_FORMAT(FROM_UNIXTIME(`date`), '%T %m/%d/%Y)
Or
SELECT FROM_UNIXTIME(`date`, '%T %m/%d/%Y')
Use DATE_FORMAT
DATE_FORMAT(FROM_UNIXTIME(`date`), "%T %m/%d/%Y")
This will return hh:mm:ss dd/mm/YYYY from unix time stored in database.
you have to define your custom date/time format. please have a look at:
http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html#function_from-unixtime
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(), '%h:%i:%s %M/%D/%Y');
I have to compare date's in mysql, for example:
select col1,col2 from table where date <= '2011-12-24' (present date)
But this gives an output of all the dates less then '2011-12-24' only.
I suspect your problem is that your date column is a timestamp, but you're comparing it to a date. When this is done, your 2011-12-24 is converted to 2011-12-24 00:00:00.0000 and hence anything that has 2011-12-24 with a reasonable time is after this point. In your situation, I'd use
select col1,col2 from table where my_date < '2011-12-25' (present date + 1 day)
or, if you insist on using <= then
select col1,col2 from table where date(my_date) <= '2011-12-24' (present date)
Well, now try the following.
SELECT col1,col2 FROM table
WHERE date <= STR_TO_DATE('2011-12-24 00:00:00', '%Y-%m-%d %H:%i:%s');
This would better help you, if you need the TimeStamp portion in comparision.
STR_TO_DATE(str,format) is the inverse of the DATE_FORMAT() function.
STR_TO_DATE() returns a DATETIME value.
The following specifiers may be used in the format string.
The '%' character is required before format specifier characters.
Specifier Description
%a Abbreviated weekday name (Sun..Sat)
%b Abbreviated month name (Jan..Dec)
%c Month, numeric (0..12)
%D Day of the month with English suffix (0th, 1st, 2nd, 3rd, ?-)
%d Day of the month, numeric (00..31)
%e Day of the month, numeric (0..31)
%f Microseconds (000000..999999)
%H Hour (00..23)
%h Hour (01..12)
%I Hour (01..12)
%i Minutes, numeric (00..59)
%j Day of year (001..366)
%k Hour (0..23)
%l Hour (1..12)
%M Month name (January..December)
%m Month, numeric (00..12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss followed by AM or PM)
%S Seconds (00..59)
%s Seconds (00..59)
%T Time, 24-hour (hh:mm:ss)
%U Week (00..53), where Sunday is the first day of the week
%u Week (00..53), where Monday is the first day of the week
%V Week (01..53), where Sunday is the first day of the week; used with %X
%v Week (01..53), where Monday is the first day of the week; used with %x
%W Weekday name (Sunday..Saturday)
%w Day of the week (0=Sunday..6=Saturday)
%X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y Year, numeric, four digits
%y Year, numeric (two digits)
%% A literal '%' character
%x x, for any 'x' not listed above
this works... jst need to convert the column to date format
select col1,col2 from table where DATE_FORMAT(date, '%Y%m%d') <= '2011-12-24'