I am trying to convert a character value generated using date_format() function into a Date value but I get null as a result
Following is my query:
select Str_to_date(date_format(cast(last_update as datetime), '%D %M %Y %T'),'%d/%m/%y') from address;
and result is NULL
is there any way to convert resulting character value generated from date_format function into a date?
If last_update is datetime, then you can simply use
SELECT
last_update, DATE(last_update)
from address;
If it is varchar of format '2006-02-15 04:45:30', then, you can use
SELECT
last_update, DATE(CAST(last_update as datetime))
from address;
Reference:
DATE function on MySQL Reference Manual
Related
I am trying to convert string '2022-12-28T22:28:43.260781049Z' to datetime format.
I have such query:
SELECT date(str_to_date('2022-12-28T22:28:43.260781049Z','%Y-%m-%d')) as date,
hour(str_to_date('2022-12-28T22:28:43.260781049Z',"%H:%M:%S")) as hour
FROM transaction
And such output:
date
time
'2022-12-28'
NULL
How to get time as well?
You can directly use a CAST on your string value to TIMESTAMP, then extract the date and the time with the hononimous DATE and TIME MySQL functions.
SELECT DATE(CAST(timestamp_ AS DATETIME)) AS date_,
TIME(CAST(timestamp_ AS DATETIME)) AS time_
FROM transactions;
Check the demo here.
Use timestamp instead of str_to_date:
SELECT hour(timestamp('2022-12-28T22:28:43.260781049Z')) as hour
I have a column with values '2015-02-14 12:23 AM' declared as varchar(150), I tried using to_date, convert and cast but not able to change the format. I would need this to filter on specific month/year/day. Thanks for the help
PS: Mysql instance is running on RDS through amazon AWS - not sure if its relevant
I suggest storing dates on proper date data type.
to_date is an Oracle function.
In MySQL, you can use STR_TO_DATE to converts a string to date then use Date_format to give the format you need
SELECT DATE_FORMAT(STR_TO_DATE(dt,'%Y-%m-%d %h:%i %p'),'%m/%Y/%d')
from test;
Demo
You can use substring with concat without the need of converting to date format:
select concat(substring(dt,6,2),'/', substring(dt,1,4),'/',substring(dt,9,2)) as my_date
from test;
Demo
SELECT CAST( SUBSTRING_INDEX( '2015-02-14 12:23 AM',
' ',
2
)
AS DATETIME
)
returns the datetime value. If you need to use this value in datetime context then you may remove CAST, it will be performed implicitly:
SELECT TIMESTAMPDIFF ( MINUTE,
SUBSTRING_INDEX( '2015-02-14 12:23 AM',
' ',
2
),
'2015-02-14 12:34'
)
I want to convert string column of format 'mm-yyyy' to timestamp.
Try this following logic. As you have only Month and Year value in your string, I have added the static date 01 to each date to generate the DATETIME value.
select
CAST(
CONCAT(right('06-2019',4) , '-', left('06-2019', 2),'-01') AS DATETIME
);
The default date format in my SQL is : '2019-05-06'
and its fine but when i insert date to my table i want this format 2019-5-6 not the above format
It means month and day must be start 1 to 30 not 01 to 31.Is there any way to change default format in my sql?
You seem to be looking for the MySQL STR_TO_DATE function :
It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts.
So if the date coming out of your application is like '2019-5-6', to convert it to a MySQL date you need :
STR_TO_DATE('2019-5-6', '%Y-%c-%e')
In an INSERT statement :
INSERT INTO mytable VALUES(1, STR_TO_DATE('2019-5-6', '%Y-%c-%e'));
Tip :
%Y : Year as a numeric, 4-digit value
%c : numeric month name (0 to 12)
%e: day of the month as a numeric value (0 to 31)
The default way to store a date in a MySQL database is by using DATE. The proper format of a DATE is: YYYY-MM-DD. If you try to enter a date in a format other than the Year-Month-Day format, it might work but it won't be storing the dates as you expect.
In order to run a MySQL Insert command and add the current date into your table you can use MySQL's built-in function CURDATE() in your query.
An example of how to Insert a Date in MySQL using CURDATE
$query_auto = "INSERT INTO tablename (col_name, col_date) VALUE ('DATE: Auto CURDATE()', CURDATE() )";
Also, you can run a query to set the date manually
An example of how to Insert a Date in MySQL manually
$query_manual = "INSERT INTO tablename (col_name, col_date) VALUES ('DATE: Manual Date', '2008-7-04')";
It is recommended to do the date formatting when doing a query, like so:
SELECT DATE_FORMAT(BirthDate, "%W %M %e %Y") FROM Employees;
You can find more examples of formatting the date here:
https://www.w3schools.com/sql/func_mysql_date_format.asp
I have a column in my MySql database which stores a date as a string "dd/mm/yyyy" is there a way for me to have this in an update query where I can check if the date in the table is overdue compared to now().
I've had a look at the CONVERT function but can't figure out how to use it for my case.
STR_TO_DATE() converts the str string into a date value based on the fmt format string. The STR_TO_DATE() function may return a DATE , TIME, or DATETIME value based on the input and format strings. If the input string is illegal, the STR_TO_DATE() function returns NULL.
SELECT STR_TO_DATE('21/5/2013','%d/%m/%Y');
With time :-
SELECT STR_TO_DATE('01/01/2013 1130','%d/%m/%Y %h%i') ;
compare with now()
Where now()=STR_TO_DATE('01/01/2013 1130','%d/%m/%Y %h%i') ;
Try this:
SELECT ... FROM ... WHERE STR_TO_DATE(col, '%d/%m/%Y') > NOW()