Convert month Fullname to month number - mysql

Is There any built-in function in MySQL for getting Month Number for given Month Full
Name like 'January','March',etc.
I tried with MONTH() function to get month number from the date like this:
SELECT MONTH(STR_TO_DATE('Apr','%b'))
but it is not working for Fullname like April

Try with DATE_FORMAT option
SELECT MONTH(STR_TO_DATE('April','%M')) as Month;

For matching full month names, you'll have to use the %M specifier in DATE_FORMAT:
%M Month name (January..December)
Therefore, the following would give this:
SELECT MONTH(STR_TO_DATE('April','%M')) /* result is 4

Related

Sort on string as date on MySql

I have CHAR strings stored in the database field in the format mm/dd/yyyy. Such as
2/26/2022
2/19/2022
2/12/2022
2/5/2022
12/31/2021
12/18/2021
11/27/2021
I need to sort them as shown according to the "date" without changing the declaration.
The post at MySQL date format DD/MM/YYYY select query? suggested using ORDER BY STR_TO_DATE(datestring, '%d/%m/%Y')
My MySQL statement looks like this:
SELECT stringdate
FROM mytable
WHERE product = '#myproduct#'
ORDER BY STR_TO_DATE(stringdate, '%m/%d/%y') DESC
However, the result is not sorted properly. Instead of the desired order as shown above, it is showing like this:
12/31/2021
12/18/2021
11/27/2021
2/26/2022
2/19/2022
2/12/2022
2/5/2022
It seems that the year is being ignored. How can I sort this without actually changing the database field declaration?
Thanks in advance.
2/5/2022 is month and day without leading zeros, and four digit year. The format string you have specified is -
%m - Month, numeric (00..12)
%d - Day of the month, numeric (00..31)
%y - Year, numeric (two digits)
SELECT stringdate
FROM mytable
WHERE product = '#myproduct#'
ORDER BY STR_TO_DATE(stringdate, '%c/%e/%Y') DESC
%c - Month, numeric (0..12)
%e - Day of the month, numeric (0..31)
%Y - Year, numeric, four digits
Executing the following query shows the difference in the converted dates -
SELECT
stringdate,
STR_TO_DATE(stringdate, '%m/%d/%y'),
STR_TO_DATE(stringdate, '%c/%e/%Y')
FROM mytable
WHERE product = '#myproduct#'
ORDER BY STR_TO_DATE(stringdate, '%c/%e/%Y') DESC
db<>fiddle
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
%y is the two-digit year code. So you are sorting them all as '20'
%Y is the four-digit year code.
See reference for the date format codes here: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
I recommend you use the DATE data type instead of CHAR.

What is the MySQL Workbench version of DATEDIFF(YEAR,C.END_DT,GETDATE())?

I understand the syntax for the current date will change from GETDATE() to SYSDATE().
How do we write the difference between the date from c.end column and the current date in terms of years in MySQL?
Do we use TIMESTAMPDIFF() ?
There are many ways to get current year from MySQL, you just have to extract it using YEAR() function like this:
SELECT YEAR(NOW());
NOW() returns todays date + time in this format 'YYYY-MM-DD HH:MM:SS' so with YEAR(), you're just taking the year value from NOW(). Apply the same thing to the date field in your table like YEAR(C.END_DT) and do a subtraction between those year values. A simple query like this should work:
SELECT YEAR(NOW()) - YEAR(C.END_DT)
FROM mytable C;
But if you still want to use DATEDIFF, you can write something like this:
SELECT FROM_DAYS(DATEDIFF(NOW(),C.END_DT)) FROM mytable C;
DATEDIFF here return the differences in total days and using FROM_DAYS(), it will return the total year, month and day from the specific date in the comparison.
Refer to this updated fiddle

How to convert a string (in three separate columns) to a timestamp(one column) MYSQL

Hi i'm a new coder and messed up on my sql table. Instead of storing my date with a timestamp I made the date in three separate columns: day, month, and year. I now realized that I need these in a timestamp. So I can perform more complicated queries.
Here is what I need the UPDATE to look like:
UPDATE coding_tracker SET coded_at = column(day)"/"column(month_number(month))"/"column(year);
Thank you in advance
Assuming your columns are called day, month_number and year, this query should work:
UPDATE coding_tracker SET coded_at = STR_TO_DATE(CONCAT_WS('/', day, month_number, year), '%d/%m/%Y')
In the case where your month column is a name, you can change %m in the above query to %b for short month names (Jan..Dec) or %M for long month names (January..December) e.g. for long names:
UPDATE coding_tracker SET coded_at = STR_TO_DATE(CONCAT_WS('/', day, month, year), '%d/%M/%Y')
Documentation about formats for STR_TO_DATE can be found in the DATE_FORMAT section of the MySQL manual.

My-SQL inserting Date in format '17-DEC-80'

How can we insert date in in '17-DEC-80' format ?
How to find the last day of this month ?
like this I want to insert 10 dates in the given format and find the last day of the months specified in the date.
This can be acheived through the STR_TO_DATE and DATE_FORMAT internal functions.
You can insert a date so long as you accompany it with the format you are providing it in.
INSERT INTO DateFormats (DF_DATE_FIELD) VALUES (STR_TO_DATE('17-Dec-80', '%d-%b-%y'));
You can also SELECT a that same format date value with the DATE_FORMAT() function. To return the value you want from the table you would use
SELECT
DF_ID,
DF_DATE_FIELD,
DATE_FORMAT(DF_DATE_FIELD, '%d-%b-%y')
FROM DateFormats;
Assuming that DF_DATE_FIELD is in fact a DATE or DATETIME field. Also beware that this is case sensitive
The formatting is as follows
%d = Day of Month for 2 places (i.e 05, 12, 23)
%b = Abbreviated Month (i.e JAN, FEB, DEC)
%y = 2 digit year code (i.e 18, 80, 99)
Read more about formatting At this handy W3 Schools page
And use this DBFiddle for reference
To follow your desired format, use below;
select STR_TO_DATE('17-Dec-80', '%d-%b-%y');
To get last day of the month
select LAST_DAY(STR_TO_DATE('17-Dec-80', '%d-%b-%y'));

MYSQL - Retrieving weeks in a month

I'm trying to list all weeks in a month, (e.g., 2015-02-01 and 2015-02-28) in the following format: (week yyyy-mm-dd to week yyyy-mm-dd etc.)
Tried using WEEK e.g., SELECT WEEK(2015-02-01) - WEEK(2015-02-28); but this just gave me the number of numbers in the month - 4.
What is the proper MYSQL statement to achieve this?
DATE_FORMAT should work, have you tried it that way :
select date_format(your_date, '%Y-%m-%d')
from yourTable;
If your field is a string you could also use str_to_date
select str_to_date(your_date, '%Y-%m-%d')
from yourTable;