I am getting a null result when trying to change the date format,
SELECT DATE_FORMAT("31-Mar-2022", '%d/%m/%Y');
What would be the right way of changing the format from 31-Mar-2022 to 2022-03-31 ?
dbfiddle
The route you want to take here in general is STR_TO_DATE to first convert the string date into a bona fide, followed by DATE_FORMAT, to convert the bona fide date back to a string in some other format. So the following will work:
SELECT DATE_FORMAT(STR_TO_DATE('31-Mar-2022', '%d-%b-%Y'), '%Y-%m-%d') AS output;
But note that the default rendering of a MySQL date often will be %Y-%m-%d, so in this case, you probably can avoid the outer call to DATE_FORMAT, and just use:
SELECT STR_TO_DATE('31-Mar-2022', '%d-%b-%Y') AS output;
Related
So I'm trying to insert dates into a table and the date is in this format:
8/3/2021
However I want to add a leading 0 before the month and day so the date shows 08/03/2021. Also I want to add it as a string concatenated with another string so test123-08/03/2021
If you really store date in that format then you may try this:
SELECT
DATE_FORMAT(STR_TO_DATE(date_col_string,'%d/%m/%Y'),'%d/%m/%Y') as 'zero-padded',
CONCAT(string_val,'-',DATE_FORMAT(STR_TO_DATE(date_col_string,'%d/%m/%Y'),'%d/%m/%Y')) as 'concatenated'
FROM mytable;
Use STR_TO_DATE() function to change the date value to standard MySQL date format of YYYY-MM-DD then use DATE_FORMAT() function to display the date value as per your desired output. The second operation is adding CONCAT() function on the converted date with your selected string. I'm assuming that your date value is d/m/y, because as #Stu mentioned in the comment, since you're not storing as MySQL standard date format, that means 8/3/2021 can be either d/m/y or m/d/y. With a standard date format value, the query would be shorter:
SELECT
DATE_FORMAT(date_col,'%d/%m/%Y') as 'zero-padded',
CONCAT(string_val,'-',DATE_FORMAT(date_col,'%d/%m/%Y')) as 'concatenated'
FROM mytable;
Demo fiddle
You should be inserting your source dates into a proper date or datetime column. Then, to view your dates in the format you want, use the DATE_FORMAT() function with the appropriate format mask:
SELECT DATE_FORMAT(date_col, '%d/%m/%Y') AS date_out
FROM yourTable;
I am trying to convert long date format string (e.g. 1st-June-1999 or 20th-March-2001) to date YYYY-MM-DD using MySql 5.7.
Reading docs tried to use STR_TO_DATE:
select STR_TO_DATE(dob,'%d-%M-YYYY') from table
However, this returns null due to the day suffix (e.g. th).
I could use update query with REPLACE() to remove the suffix and then STR_TO_DATE, but is there a better solution?
You have two problems. First, you need %D (capital not lowercase) since you have the suffix after the day of the month.
Next, the year should simply be %Y.
So, the select statement would be
select STR_TO_DATE(dob,'%D-%M-%Y') from table;
w3schools has a good reference for the abbreviations: https://www.w3schools.com/sql/func_mysql_str_to_date.asp
%D does what you want:
str_to_date(dob, '%D-%M-%Y')
Can the MYSQL function STR-TO-DATE convert a string in this format 99-MMM-9999 (ex., '21-Sep-2014' to a date format? And what if that field is defined as 12 chars instead of the specific 11?
I tried this and it got NULLS.
You can, just setting proper format mask:
SELECT STR_TO_DATE('21-Sep-2014','%d-%M-%Y');
If you have 12 chars, set proper mask, sample:
SELECT STR_TO_DATE(' 21-Sep-2014',' %d-%M-%Y');
SELECT STR_TO_DATE(yourdatefield, '%m/%d/%Y')
FROM yourtable
You can also handle these date strings in WHERE clauses. For example
SELECT whatever
FROM yourtable
WHERE STR_TO_DATE(yourdatefield, '%m/%d/%Y') > CURDATE() - INTERVAL 7 DAYS
You can handle all kinds of date / time layouts this way. Please refer to the format specifiers for the DATE_FORMAT() function to see what you can put into the second parameter to STR_TO_DATE()
reference :- how to convert a string to date in mysql?
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
select DATE_FORMAT('8:48:30 AM', '%H:%i:%s')
It returns Null why ?
but when using
select DATE_FORMAT(CURTIME(), '%H:%i:%s')
It return formatted value.
It's returning NULL because MySQL isn't successfully parsing the string into a valid DATETIME value.
To fix the problem, use the STR_TO_DATE function to parse the string into a TIME value,
SELECT STR_TO_DATE('8:48:30 AM', '%h:%i:%s %p')
Then, to get the TIME value converted to a string in a particular format, use the TIME_FORMAT function, e.g. 24-hour clock representation:
SELECT TIME_FORMAT(STR_TO_DATE( '8:48:30 AM', '%h:%i:%s %p'),'%H:%i:%s')
returns:
--------
08:48:30
The method DATE_FORMAT is used to display date and time, however in the first you are not assigning any date except time, so its is throwing null.
From the manuals -
DATE_FORMAT Formats the date value according to the format string.
In MySql version 5.5 SELECT DATE_FORMAT( CURTIME( ) , '%H:%i:%s' ) returns null
DATE_FORMAT 's first parameter is of type DATETIME. On recent mysql server versions both your queries return NULL.
So the answer to your question is that this difference in behaviour is because of a bug in your mysql version - in some way it converts the TIME to DATETIME, while it cannot convert the string to DATETIME.
Here is also an example of a working query:
select DATE_FORMAT(NOW(), '%H:%i:%s')
NOW() returns a DATETIME while CURTIME() returns TIME.
To my knowledge, I think it's because MySQL recognises the function as a time, and therefore knows how to handle it. Whereas, in the first example, it regards it as a string and doesn't know what to do with it.
I've got an issue selecting a date range with MySQL.
SELECT MvtDate,date_format(MvtDate,'%d-%m-%Y')
FROM (`immmvt`)
WHERE date_format(MvtDate,'%d-%m-%Y') BETWEEN '01-01-2010' AND '02-01-2010'
mvtDate type is date like 2010-01-01 00:00:00.
When I run the query, the result works for the days and the months but it also show me other result from other years.
Like 01-01-2011 etc.
You should use STR_TO_DATE since you want to convert string back to date
SELECT MvtDate, date_format(MvtDate,'%d-%m-%Y')
FROM `immmvt`
WHERE MvtDate BETWEEN STR_TO_DATE('01-01-2010','%d-%m-%Y') AND
STR_TO_DATE('02-01-2010','%d-%m-%Y')
FYI: DATE_FORMAT() converts date to formatted string representation.
STR_TO_DATE() converts formatted string back to date
SELECT MvtDate,date_format(MvtDate,'%d-%m-%Y')
FROM (`immmvt`)
WHERE date_format(MvtDate,'%d-%m-%Y') IN ('01-01-2010', '02-01-2010')