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'));
Related
Im a bit trap in this date format.
I have a date format in my database "j-M-Y h:i a" = "10/15/2020 Thu 09:29:35 am"
i am trying to convert this date to mm-dd-yyyy format so i can get the year and make a trap sql code to select only 2022 dates in my database.
LIKE THIS QUERY
SELECT * FROM document WHERE date_received >= 2022
This code not work for me
SELECT * FROM document WHERE YEAR(date_received) >= 2022
i also tried other conversion query but i only getting "null" or invalid syntax
First, you have to cast your field as date. First trim that field. Then using date_format as '%Y", you can extract four digit year from it. If you want two digit year, you have to use '%y'.
SELECT * FROM document WHERE DATE_FORMAT(CAST(TRIM(date_received) AS DATE),'%Y') >= '2022';
If you are using varchar field for datetime values you have to convert string value to datetime using STR_TO_DATE function after that YEAR function will work becase YEAR function take date field or datetime field as input:
In your case like this:
SELECT *
FROM document
WHERE YEAR(STR_TO_DATE("10/15/2020 Thu 09:29:35 am", '%m/%d/%Y')) >= 2022
SELECT *
FROM document
WHERE YEAR(STR_TO_DATE(date_received, '%m/%d/%Y')) >= 2022
If your column data type is string "17-Aug-2020" in that case you need to convert string to date and then cast in expected date format then using %Y you can get year from your string.
SELECT * FROM document m WHERE DATE_FORMAT(CAST(TRIM(STR_TO_DATE(date_received ,'%d-%M-%Y')) AS DATE),'%Y') >= '2022';
try this example also
SELECT DATE_FORMAT(CAST(TRIM(STR_TO_DATE('May-01-2022','%M-%d-%Y')) AS DATE),'%Y')
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.
I have a date column that has the date and time stored in string format in my sql database. I am trying to convert to a proper date-time field, but when I use the STR_TO_DATE function it is only reading the 2nd digit of my 2 digit hour field.
For example
SELECT '2018-11-26 15:55:58', STR_TO_DATE('2018-11-26 15:55:58', '%Y-%m-%D %T')
is returning:
'2018-11-26 15:55:58', '2018-11-26 05:55:58'
I am trying to understand what I'm doing wring, the hour is being incorrectly calculated
You're supposed to use %d (Day of the month, numeric (00..31)) instead of %D (Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)).
Apparently it considers two characters after 26 as the ordinal suffix.
I have a MySQL query that reads:
select sum(AmtPd) as HCSA from HC062017 where CostCenter = '8718' and ServiceDate > '08/31/2013'
Have been using it for several months with no problem. Today I encountered a problem with it not working. ServiceDate is a var(10) containing a date pulled from a report. The format is mm-dd-yyyy. What appears to be happening is if the ServiceDate is in 2013 (which some of them still are), then the statement works perfectly. However, when the ServiceDate is in 2014, no records are selected. If I leave off the ServiceDate parameter, I get the desired results (since all records this time are past the 08/31/2013 date). Maybe I lucked out with the months '09', '10', etc, falling after '08'. I've tried using str_to_date with no luck
select sum(AmtPd) as HCSA from HC062017 where CostCenter = '8718' and str_to_date(ServiceDate, '%m, %d, %Y') > '08-31-2013';.
Any suggestions on how to handle this?
You need to convert both to common format:
str_to_date(ServiceDate, '%m-%d-%Y') > str_to_date('08-31-2013', '%m-%d-%Y')
Comparing strings like you did is a bad idea. Strings are compared in alphabetic order, so '08-31-2013' is "smaller" than '09-30-2010' because 9 is compared to 8 before everything else. You can compare dates in ISO format YYYY-MM-DD.
STR_TO_DATE produces a date in a format of YYYY-MM-DD. So your date that you are comparing to must be in the same format:
str_to_date(ServiceDate, '%m, %d, %Y') > '2013-08-31';
I'm importing a column of strings with partial dates into a csv file.
During import, is there a way to convert strings with the format "September 23" into a DATETIME format, using the partial date to populate month, day and year, and the time of insert as the time?
Edit: I clarified the question, so the below answers aren't quite relevant.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
I think you can try DATE_FORMAT().
mysql> SELECT STR_TO_DATE('OCT 1, 2013','%M %d,%Y'); // -> '2013-10-01'
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. If the date, time, or datetime value extracted from str is illegal, STR_TO_DATE() returns NULL and produces a warning.
Refer this link and this also.
This is ugly, but it works. The main problem was creating the year and time, and also dealing with single numeric values for the days of the month. CONCAT_WS is the glue in this scenario:
STR_TO_DATE(CONCAT_WS(',','September 5',YEAR(NOW()), TIME(NOW())), '%M %e, %Y, %T')
Returns:
2012-09-05 11:51:46