I have one date-field ref_event_times.end_date and one time-field ref_event_times.end_time in my table "ref_event_times"...
I try to union that as a one datetime field "end_date_time"...
Use follow
STR_TO_DATE('CONCAT(`ref_event_times`.`end_date`,' ',`ref_event_times`.`end_time`)','%m/%d/%Y %H:%i') AS `end_date_time`
return null...
Where is the mistake?
MYSQL doesn't support your given datetime format '%m/%d/%Y %H:%i'
The mysql date format should be like this '%Y-%m-%d %H:%i:%s' Datetime format in mysql
Data Type “Zero” Value
DATE '0000-00-00'
TIME '00:00:00'
DATETIME '0000-00-00 00:00:00'
TIMESTAMP '0000-00-00 00:00:00'
YEAR 0000
try this example
select STR_TO_DATE(CONCAT(current_date,' ',current_time),'%Y-%m-%d %H:%i:%s');
Related
I have a datetime datatype in my table,
`DateAdded` datetime(4) DEFAULT NULL,
I have a record in my database with DateAdded = 2017-09-11 17:02:48.6531 value, ( it's ID = 16452994 ).
When I want to get it with following query return NULL
select `ID`,`DateAdded` from `Add` where `DateAdded` <= FROM_UNIXTIME(('1505071799' +86400 ), '%Y-%m-%d %h:%i:%s') and ID =16452994 ;
FYI : FROM_UNIXTIME(('1505071799' +86400 ), '%Y-%m-%d %h:%i:%s') = 2017-09-11 23:59:59
It's strange that 2017-09-11 17:02:48.6531 <= 2017-09-11 23:59:59 return false
but when I try the following query I'll get my desire result.
select `ID`,`DateAdded` from `Add` where `DateAdded` <= FROM_UNIXTIME(('1505071799' +86400 +1 ), '%Y-%m-%d %h:%i:%s') and ID =1645299;
I want to know why this is happening and how can I resolve this problem?
FROM_UNIXTIME(unix_timestamp), FROM_UNIXTIME(unix_timestamp,format)
Returns a representation of the unix_timestamp argument as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone. unix_timestamp is an internal timestamp value such as is produced by the UNIX_TIMESTAMP() function.
If format is given, the result is formatted according to the format string, which is used the same way as listed in the entry for the DATE_FORMAT() function.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
I have simple problem with SQL query.. I would like to search fields, where dates begin and ends in specific date and time.
Field is defined as CHAR and have structure: DD.MM.YYYY hh:mm:ss and I cannot change it.
I tried to declare some variables and search by this, also tried by Converting.
This is what I tried and didn't work:
SELECT date FROM table WHERE date BETWEEN '1.01.2017 00:00:00' AND '1.02.2017 23:59:59'
SELECT date FROM table WHERE date >= '1.01.2017 00:00:00' AND date <= '1.02.2017 00:00:00'
SELECT date FROM table WHERE date >= Convert(DATETIME, '1.01.2017', 104) AND date <= Convert(DATETIME, '1.02.2017', 104)
Always after this query I get all dates, not this what I asked.
Your field is not in a format that can be compared lexicographically as strings, so you need to convert it to a date. But it's not in the format that MySQL can parse by default (it expects dates in the format YYYY-MM-DD). So use STR_TO_DATE() to parse it with a format string.
SELECT date
FROM table
WHERE STR_TO_DATE(date, '%d.%m.%Y %H:%i:%s') BETWEEN '2017-01-01 00:00' AND '2017-01-02 23:59:59'
I solved my problem. Problem was with that HOW this fields are storage in DB.
I thought that they are storage like: DD.MM.YYYY hh:mm:ss, but it was only structure. In DB they are storage like: YYYYMMDDhhmmss and after changes in WHERE query line it works.
SELECT date FROM table WHERE date >= '20170101000000' AND date <= '20170101235959'
SELECT date FROM table WHERE STR_TO_DATE(date, '%d.%m.%Y %H:%i:%s') BETWEEN '2017-01-01 00:00:00' AND '2017-02-01 23:59:59'
OR
You can use STR_TO_DATE() function.
SELECT date FROM table WHERE STR_TO_DATE(date, '%d.%m.%Y %H:%i:%s') BETWEEN STR_TO_DATE('1.01.2017 00:00:00','%d.%m.%Y %H:%i:%s') AND STR_TO_DATE('1.02.2017 23:59:59','%d.%m.%Y %H:%i:%s')
You can try above query.
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')
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.
How to convert 1300464000 to 2011-03-18 16:00:00 in MySQL?
Use the FROM_UNIXTIME() function in MySQL
Remember that if you are using a framework that stores it in milliseconds (for example Java's timestamp) you have to divide by 1000 to obtain the right Unix time in seconds.
DATE_FORMAT(FROM_UNIXTIME(`orderdate`), '%Y-%m-%d %H:%i:%s') as "Date" FROM `orders`
This is the ultimate solution if the given date is in encoded format like 1300464000
To answer Janus Troelsen comment
Use UNIX_TIMESTAMP instead of TIMESTAMP
SELECT from_unixtime( UNIX_TIMESTAMP( "2011-12-01 22:01:23.048" ) )
The TIMESTAMP function returns a Date or a DateTime and not a timestamp, while UNIX_TIMESTAMP returns a unix timestamp
You can use
select from_unixtime(1300464000,"%Y-%m-%d %h %i %s") from table;
For in details description about
from_unixtime()
unix_timestamp()
SELECT from_unixtime( UNIX_TIMESTAMP(fild_with_timestamp) ) from "your_table"
This work for me