I have a DateTime column, which (because of some reasons) has changed my DateTime values from DateTime to Strings. Now I want to change them back to Date only. How to do this?
e.g my date column has values like: "Sep 2, 2019 8:00:00 PM" (in string format)
select str_to_date('Date','%d-%m-%Y') as Date
from table
This code gives blank cells as a result
Your date's format is: '%b %e, %Y %h:%i:%s %p'.
Read more about the parameters you can use in str_to_date() here.
So do the conversion like this:
select str_to_date(
'Sep 2, 2019 8:00:00 PM',
'%b %e, %Y %h:%i:%s %p'
) as Date
See the demo.
Have you tried using the CONVERT function?
SELECT CONVERT(table.Date, DATE) AS Date FROM table;
Related
I have database which contains string like this
22 Jan 2019 11:03
I would like to convert this string to date so I apply this query
select DATE_FORMAT(STR_TO_DATE('22 Jan 2019 11:03','%d-%m-%Y') ,'%d-%m-%Y');
but I get a null result
All you have to do is change small letter m to big letter M in your str_to_date function.
select STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y');
so the final query would be:
select DATE_FORMAT(STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y') ,'%d-%m-%Y');
Here is a demo
SELECT STR_TO_DATE('22 Jan 2019 11:03','%d,%m,%Y');
Basically you first need to understand the values to pass in STR_TO_DATE() and DATE_FORMAT() functions
STR_TO_DATE(my date string , current format of my date string)
Now what you are not doing right is that in STR_TO_DATE() you are passing the format '%d-%m-%Y' , this format says that the input string has hyphen separated date month and year values which is not true.
Now in your case the actual format of your date string is the following
'%d %M %Y %h:%i'
STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y %h:%i')
%d - day
%M - month
%Y - year
%h - hour
%i - minute
For more info on formats click
Now that we have a complete valid value from string to date including hour and minutes, we can convert this date into any of our required format using correct parameters
SELECT DATE_FORMAT(STR_TO_DATE('22 Jan 2019 11:03','%d %M %Y %h:%i') ,'%d-%m-%y %h:%i');
You can try out various examples here
https://www.mysqltutorial.org/tryit/query/mysql-str_to_date/#1
I am using MySQL, I have a column value like this 08th August 2017.
I want to convert it into a date like 8-8-2017. I have tried MySQL date_formate() function, STR_TO_DATE() function but it return a null value.
How can do this task ?
SELECT STR_TO_DATE('08th August 2017', '%D %M %Y') works fine for me
You need to use the %D %M %Y format for STR_TO_DATE
SELECT STR_TO_DATE(datefield,'%D %M %Y')
FROM yourtable
Input
08th August 2017
Output
2017-08-08
SQL Fiddle: http://sqlfiddle.com/#!9/701e66/5/0
First convert your date string into a DATETIME value and then use that in a DATE_FORMAT() to get the date into whatever format you want
SELECT DATE_FORMAT(STR_TO_DATE('08th August 2017', '%D %M %Y'),'%d-%m-%Y') AS
test;
You can use this code in mysql.
I am just giving example how to use.
select STR_TO_DATE("08th August 2017", "%d %m %y") from demo
where demo is the demo table.
I have a table which contains a varchar field containing date like '15 May 2015 - 03:10 am'
I have to compare all date in this table with the current date to retrieve row which are next the current date and row which are recent to the current date
I found this solution here:
select *
from reunion a
where STR_TO_DATE(a.dateDebut,'%d %b %Y - %I:%i %p')>
DATE_FORMAT(NOW(),'%d %b %Y - %I:%i %p');
but it returns all date: '15 May 2013 - 03:10 AM' ,'15 May 2015 - 03:10 am','15 May 2016 - 03:10 am'
and when I change to "<" it returns 0 rows;
is there any other solution to give the varchar field the ability to be a valid date?
Your query is overkill. You just need to convert your date format to a date. now() is already a date. So try:
select *
from reunion a
where STR_TO_DATE(a.dateDebut,'%d %b %Y - %I:%i %p') > NOW();
I have a table TableA with two fields, strField and dateField. The string field strField has text dates like Wednesday, January 30th, 2013, 1:02 PM in it, but I need to convert that to a date format and replace it in the adjacent field called dateField. Is there a simple way of doing this or do I have to do all the stripping ?
The last step is easy, I think. When I strip everything and get the string to look like this 2013-02-15 11:45:21.0 it will be just a simple
STR_TO_DATE('2013-02-15 11:45:21.0', '%m/%d/%Y')
Perhaps there is a function which can strip the data easily and reformat the original text string.
SELECT
DATE_FORMAT(
STR_TO_DATE( 'Wednesday, January 30th, 2013, 1:02 PM', '%W, %M %D, %Y, %I:%i %p' ), '%Y-%m-%d %T');
sqlfiddle link.
For other format conversions, check the DATE_FORMAT() at MySQL docs.
For your UPDATE this would do:
UPDATE `tableA`
SET `dateField` = DATE_FORMAT(
STR_TO_DATE(`strField`, '%W, %M %D, %Y, %I:%i %p'),
'%Y-%m-%d');
How do I convert the following format to unix timestamp?
Apr 15 2012 12:00AM
The format I get from DB seems to have AM at the end.
I've tried using the following but it did not work:
CONVERT(DATETIME, Sales.SalesDate, 103) AS DTSALESDATE,
CONVERT(TIMESTAMP, Sales.SalesDate, 103) AS TSSALESDATE
where Sales.SalesDate value is Apr 15 2012 12:00AM
Here's an example of how to convert DATETIME to UNIX timestamp:
SELECT UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p'))
Here's an example of how to change date format:
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p')),'%m-%d-%Y %h:%i:%p')
Documentation: UNIX_TIMESTAMP, FROM_UNIXTIME
You will certainly have to use both STR_TO_DATE to convert your date to a MySQL standard date format, and UNIX_TIMESTAMP to get the timestamp from it.
Given the format of your date, something like
UNIX_TIMESTAMP(STR_TO_DATE(Sales.SalesDate, '%M %e %Y %h:%i%p'))
Will gives you a valid timestamp. Look the STR_TO_DATE documentation to have more information on the format string.
If you want to create a timestamp as returned by java's Date.getTime() you should multiply by 1000.
SELECT UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p'))*1000
Now for a more standard date format use:
SELECT UNIX_TIMESTAMP(STR_TO_DATE('2022-12-14 20:58:00', '%Y-%m-%d %H:%i:%s'))*1000
From http://www.epochconverter.com/
SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE())
My bad, SELECT unix_timestamp(time) Time format: YYYY-MM-DD HH:MM:SS or YYMMDD or YYYYMMDD. More on using timestamps with MySQL:
http://www.epochconverter.com/programming/mysql-from-unixtime.php