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
);
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
In one of my DB column i have a string like this
19-Oct-19
i want to change this string to a proper date format like this
19-10-2019 or 19-10-2019
how can i achieve this with sql
With STR_TO_DATE() you can convert String to Date and with DATE_FORMAT() format your Date value.
select DATE_FORMAT(STR_TO_DATE('19-Oct-19', '%Y-%M-%d'), '%d-%m-%Y');
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 my sql database which have a field date and its type is varchar,I want to convert it into the Date but i have a lot of record in this field.
Kindly guide me how i can covert it into Date type without loosing my data.
It's looking like : 20-10-2018
But i want to change the date column varchar type to Date.
Thanks.
You will first need to convert the date string (varchar) to MySQL date format (YYYY-MM-DD). We can use STR_TO_DATE() function for this.
Your sample date string (20-10-2018) is basically in dd-mm-yyyy format. Following format specifiers can be used:
%d Day of the month as a numeric value (01 to 31)
%m Month name as a numeric value (00 to 12)
%Y Year as a numeric, 4-digit value
The query to update the date would look as follows (DB Fiddle DEMO):
UPDATE your_table_name
SET date_column_name = STR_TO_DATE(date_column_name, '%d-%m-%Y');
Now, you can use Alter Table to change the data type from varchar to date.
ALTER TABLE your_table_name
MODIFY COLUMN date_column_name date;
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