I am new to one of my project. Previous people saved date and time in two columns in the below format :
For date : 2015-04-15 (date type)
For time : 04:20 PM (varchar)
Now I want to compare the above columns with present date and time.
I tried this below query but it's showing NULL.
SELECT UNIX_TIMESTAMP(
STR_TO_DATE('evnt_endDate Event_EndTime', '%Y-%m-%d %h:%i%p')
)
FROM events
Please could any one tell me how to compare?
Use concat():
SELECT UNIX_TIMESTAMP(STR_TO_DATE(concat(evnt_endDate, ' ', Event_EndTime
), '%Y-%m-%d %h:%i%p')
)
'evnt_endDate Event_EndTime' is a string. You need to reference the fields/columns and concatenate their values like
CONCAT(`evnt_endDate`, ' ', `Event_EndTime`)
The resulting query would then be
SELECT UNIX_TIMESTAMP( STR_TO_DATE( CONCAT(`evnt_endDate`, ' ', `Event_EndTime`), '%Y-%m-%d %h:%i%p') ) from events
Related
I currently have a mySQL database with two TEXT fields: Date and Time. These are in the format 'dd/mm/yyyy' and '0:00' respectively, for example '11/08/2020' and 19:12. I want to create a third field called Timestamp (of type INT) and convert the two original fields into this timestamp field and remove the date/time text fields.
I have done a bit of research in regards to using UNIX_TIMESTAMP() and STR_TO_DATE() but I can't seem to get it to work, the format seems to be wrong for it.
How can I achieve this in SQL and convert two string fields which represent date and time into a third field to replace them both which just stores the unix timestamp?
This is my best attempt so far..
SELECT UNIX_TIMESTAMP(STR_TO_DATE(CONCAT(`InfractionDate`, " ", `InfractionTime`), '%d %M %Y %h:%i%p')) FROM `playerinfractions`
The table is called playerinfractions and the date/time are stored in the TEXT fields InfractionDateand InfractionTime.
Many thanks in advance!
The format pattern that you use in the function STR_TO_DATE()is wrong.
Try this:
SELECT
UNIX_TIMESTAMP(
STR_TO_DATE(
CONCAT(`InfractionDate`, ' ', `InfractionTime`),
'%d/%m/%Y %H:%i')
)
FROM `playerinfractions`
You need to tell STR_TO_DATE the correct format of your date and time, which you suggested was '%d/%m/%Y %H:%i'
SELECT UNIX_TIMESTAMP(
STR_TO_DATE(
CONCAT('10/08/2020', ' ', '12:20'), '%d/%m/%Y %H:%i' )
)
)
FROM `playerinfractions`
So using your columns
SELECT UNIX_TIMESTAMP(
STR_TO_DATE(
CONCAT(`InfractionDate`, ' ', `InfractionTime`), '%d/%m/%Y %H:%i' )
)
)
FROM `playerinfractions`
I tried passing a string to the same function in below format and it worked. Also you can share your format to check it further.
select UNIX_TIMESTAMP(STR_TO_DATE(CONCAT('8/12/2020',' ', '12:01:49' ),'%m/%d/%Y %h:%i:%s'))
The datatype of date column in my table is char(10). So each date is stored as a string like '02/01/2016/'.
How can I get the day of week and month of year from this '02/01/2016' in mysql?
Convert the string to DATE datatype using STR_TO_DATE function, and then use DATE_FORMAT function.
SELECT DATE_FORMAT( STR_TO_DATE( '02/01/2016', '%m/%d/%Y'), '%w') AS dow
, DATE_FORMAT( STR_TO_DATE( '02/01/2016', '%m/%d/%Y'), '%c') AS moy
(The format specifier needs to match the format of the string. This demonstration assumes that the string is in month/day/year format, that this represents February 1st, and not January 2nd.)
If you want to return a string like 'Monday', use '%W' in place of '%w'
MySQL Reference Manual:
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_str-to-date
As #paxdiablo commented, you should seriously consider storing your date information as date, datetime, or timestamp. That being said, if you must live with your current setup, you can work around this by first parsing your text into a date using STR_TO_DATE() and then extracting out a string weekday and month name using DATE_FORMAT(). Something like this should work:
SELECT DATE_FORMAT(STR_TO_DATE(date, '%d/%m/%Y'), '%W') AS day_of_week,
DATE_FORMAT(STR_TO_DATE(date, '%d/%m/%Y'), '%M') AS month_of_year
FROM yourTable
Demo here:
SQLFiddle
Any idea how to format a timestamp from a MySQL table inside a query to a human readable form?
Example:
{ts '1978-01-16 00:00:00'}
Needed output:
16-01-1978
I have tried:
select DATE_FORMAT(BIRTHDAY, '%d-%m-%Y') FROM cs_user
with no success:
[Table (rows 10 columns DATE_FORMAT(BIRTHDAY, '%D-%M-%Y')): [DATE_FORMAT(BIRTHDAY, '%D-%M-%Y'): coldfusion.sql.QueryColumn#1077e6ed] ] is not indexable by DATE_FORMAT(BIRTHDAY
My BIRTHDAY column is created as date in the MySQL table.
Take the alias name for the field and try it.
select DATE_FORMAT(BIRTHDAY, '%d-%m-%Y') as some_alias_name FROM cs_user
check, this for an example related to this.
after DATE_FORMAT in a bracket first parameter will be column of the table, then the date format,
Try this
select DATE_FORMAT(date, '%d-%m-%Y') as BIRTHDATE FROM cs_user
find the below link for all the date format which mysql supports.
http://www.w3schools.com/sql/func_date_format.asp
Try this:
SELECT DATE_FORMAT( FROM_UNIXTIME( table.column ) , '%d - %m - %Y' ) FROM table
I have a table, which has several thousand rows in it, each of which has a time stamp in this format:
** YYYY-MM-DD HH-MM-SS** so 2014-01-10 04:20:26
Is it possible to use a simple method to get MySQL Select, presumably using WHERE, to get all articles, saved in January say. Alternatively I guess the only other option is to loop through each row, split the string and add it to another string if it is. Not ideal to say the least.
Note: While I can change this table the data is being fed from a third party so it isn't possible to save the month/year in its own cell.
SELECT * FROM myTable WHERE date >= '2014-01-01' AND date < '2014-02-01'
Use STR_TO_DATE to convert the string to a DATE, then use the MONTH function to extract the month and check against that.
For example:
select * from tbl where month(str_to_date(date, '%Y-%m-%d %h:%i:%s')) = 1
And for month and year:
select * from tbl
where month(str_to_date(date, '%Y-%m-%d %h:%i:%s')) = 1
and year(str_to_date(date, '%Y-%m-%d %h:%i:%s')) = 2014
Is there way in MySQL to create DATETIME from a given attribute of type DATE and a given attribute of type TIME?
Copied from the MySQL Documentation:
TIMESTAMP(expr), TIMESTAMP(expr1,expr2)
With a single argument, this function returns the date or datetime expression expr as a datetime value. With two arguments, it adds the time expression expr2 to the date or datetime expression expr1 and returns the result as a datetime value.
mysql> SELECT TIMESTAMP('2003-12-31');
-> '2003-12-31 00:00:00'
mysql> SELECT TIMESTAMP('2003-12-31 12:00:00','12:00:00');
-> '2004-01-01 00:00:00'
To get a true DATETIME value from your two separate DATE and TIME values:
STR_TO_DATE(CONCAT(date, ' ', time), '%Y-%m-%d %H:%i:%s')
You could use ADDTIME():
ADDTIME(CONVERT(date, DATETIME), time)
date may be a date string or a DATE object.
time may be a time string or a TIME object.
Tested in MySQL 5.5.
datetime = CONCAT(date, ' ', time);
select timestamp('2003-12-31 12:00:00','12:00:00');
works, when the string is formatted correctly. Otherwise, you can just include the time using str_to_date.
select str_to_date('12/31/2003 14:59','%m/%d/%Y %H:%i');
Without creating and parsing strings, just add an interval to the date:
set #dt_text = '1964-05-13 15:34:05.757' ;
set #d = date(#dt_text) ;
set #t = time(#dt_text) ;
select #d, #t, #d + interval time_to_sec( #t ) second;
However this truncates the microseconds.
I agree with Muki - be sure to take account of time zones and daylight savings time!