STR_TO_DATE parsing of %b-%Y - mysql

I need to parse date in the format JAN-1980 to 1980-01-01 i.e. by adding the day as always being the first date of the indicated month.
How can I go about his?

something like this,
SELECT STR_TO_DATE(CONCAT(`colName`, '-01'),'%b-%Y-%d')
FROM table1
SQLFiddle Demo
SOURCE
STR_TO_DATE

Concat -01 in your date, convert to date and then format back in your desired format e.g. below:
DATE_FORMAT(STR_TO_DATE(CONCAT('JAN-1980','-01'),'%b-%Y-%d'), '%Y-%m-%d')

Related

How to extract date and hour only from MySQL?

I have this column in MySQL table:
Date
2022-07-13 07:01:20
I want to extract this column to get this (the date and hour only):
2022-07-13 07
Any help is appreciated thanks!
This date column is in the form of timestamp
Apparently timestamp is a string so i can use substring.
To select the date and hour from that format first you have to take the substring using SUBSTRING()
the convert to datetime format using STR_TO_DATE()
you can take the specific date using SELECT CONVERT(date, your_date_time);
then extract the hour to add it in there SELECT HOUR()
you can then convert back to a string or use it that way

MySQL - How to convert date to the month has leading zeros?

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;

Date conversion query in SQL using STR_TO_DATE

In my table, there is a date column i.e. ORDER DATE which is in dd-mm-yyyy format, and the datatype of this date column is text. Now, I want to convert the format from yyyy-mm-dd. But the date format is not changing at all. Trying to use STR_TO_DATE but the year in the dates are coming as 2020 only. Not sure how to fix it or if there is a better way to convert the date in MySQL.
For example:
SELECT STR_TO_DATE('14-09-2017','%d-%m-%y') AS DATE;
Result coming as 2020-09-14
Or suppose
SELECT STR_TO_DATE('20-MAR-2018','%d-%b-%y') AS DATE;
Result coming as 2020-03-20
day and month are coming properly but the year is not coming. Kindly help how to get convert the date format correctly.
Try to change your command like this below and you will get the answer:
SELECT STR_TO_DATE('14-09-2017','%d-%m-%Y') AS DATE;
Refer to the documentation for more info about the commands.

Convert dd.mm.yyyy to yyyy-mm-dd in SQL

I need to convert a date in format dd.mm.yyyy to yyyy-mm-dd
For example, if the date is 04.12.2020, i want it to be like 2020-12.04(which is 4th december)
But the command giving me the output as 2020-04-12( which is 12th April)
select convert(date,'04.12.2020' )
I think you just want str_to_date():
select str_to_date('04.12.2020', '%d.%m.%Y')
It sounds like you have a date as a string? (ie. the "04.12.2020").
MySql has a STR_TO_DATE() function:
select str_to_date('04.12.2020', '%d.%m.%Y')
That should convert it to an actual date datatype.
If needed an actual date can then be formated in which ever way you want or just visualized depending on what you're trying to do with DATE_FORMAT():
select date_format(date, '%Y-%m-%d')

How to convert a date string to mysql date format for calculating date diff using mysql query

I have a column where a date store in ddmmyy format (e.g. 151216). How can I convert it to yyyy-mm-dd format (e.g 2016-12-15) for calculating a date difference from the current date? I try using DATE_FORMAT function but its not appropriate for this.
If you want to get the date difference, you can use to_days() after converting the string to a date using str_to_date():
select to_days(curdate()) - to_days(str_to_date(col, '%d%m%y'))
or datediff():
select datediff(curdate(), str_to_date(col, '%d%m%y'))
or timestampdiff():
select timestampdiff(day, str_to_date(col, '%d%m%y'), curdate())
You can use the function, STR_TO_DATE() for this.
STR_TO_DATE('151216', '%d%m%y')
A query would look something like:
select
foo.bar
from
foo
where
STR_TO_DATE(foo.baz, '%d%m%y') < CURDATE()
Note: Since both STR_TO_DATE() and CURDATE() return date objects, there's no reason to change the actual display format of the date. For this function, we just need to format it. If you wanted to display it in your query, you could use something like
DATE_FORMAT(STR_TO_DATE(foo.baz, '%d%m%y'), '%Y-%m-%d')
To get the difference, we can simply subtract time
select
to_days(CURDATE() - STR_TO_DATE(foo.baz, '%d%m%y')) as diff
from
foo
If you wanted to only select rows that have a difference of a specified amount, you can put the whole to_days(...) bit in your where clause.
SELECT STR_TO_DATE('151216', '%d%m%y') FROM `table`
use this '%d%m%y'