I have a legacy table which has a varchar column represent date, format is MM/DD/YYYY (e.g. 01/08/2015). It is not convenient to perform data range selection since it is a varchar (when I use < or > kinds comparison, it goes to varchar/string comparison, which have different results from date comparision).
For example, I want to select only rows which dates are between 01/08/2015 and 01/10/2015. Any smart solution is appreciated, and I cannot change the data type of varchar to date in my existing table.
I am using MySQL Workbench/MySQL.
Varchar dates are evil and they are not real date, the best solution is to use mysql's native date data types.
Since you can't change the datatype you may use str_to_date() function and here how it works
mysql> select str_to_date('01/08/2015','%d/%m/%Y') as d ;
+------------+
| d |
+------------+
| 2015-08-01 |
+------------+
So the query for select would be
select * from table_name
where
str_to_date(date_column,'%d/%m/%Y')
between
str_to_date('01/08/2015','%d/%m/%Y')
and
str_to_date('01/10/2015','%d/%m/%Y')
There are many answers which addresses many different way of converting the string to date.
You may choose whichever is perfect for your need
SELECT * FROM `your_table` WHERE DATE_FORMAT(my_column_with_the_string_date, "%Y-%m-%d") <= '2011-09-30'
DATE_FORMAT can be used to convert your date string to any format: I will use the NOW() function instead of string to list different
formats that are supported
DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
DATE_FORMAT(NOW(),'%m-%d-%Y')
DATE_FORMAT(NOW(),'%d %b %y')
DATE_FORMAT(NOW(),'%d %b %Y %T:%f')
The output of the above is:
Nov 04 2014 11:45 PM
11-04-2014
04 Nov 14
04 Nov 2014 11:45:34:243
You can modify your query accordingly
You can cast your dates as strings using STR_TO_DATE:
STR_TO_DATE(yourdatefield, '%m/%d/%Y')
SELECT STR_TO_DATE(got_fired_at, '%m/%d/%Y') BETWEEN ? AND ? FROM firings;
(field/table names guaranteed to have been chosen randomly)
Use MySQL's STR_TO_DATE function to parse the date strings to date objects then do the comparison.
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')
MYSQL
Format of time in two tables:-
Jan 1 2018 10:42:06
table1.time greater than table2.time
Comparison is not working in these tables
table1.time > table2.time
No data is coming
You seem to be looking for :
STR_TO_DATE(a.time, '%b %d %Y %H:%i:%d')
The mysql STR_TO_DATE function lets you translate strings to date.
Once strings are converted to date, you can compare them using the < operator or the-like
my mysql database tb_date (varchar 20):
16 November 2014
06 December 2014
01 April 2014
12 April 2015
I want select between 01 January 2014 until 31 December 2014, how the query is with date conversion?
thanks..
This is an anti-pattern, storing date values in VARCHAR columns, rather than using datatypes specifically designed and implemented for storing date values... DATE, DATETIME or TIMESTAMP.
To answer your question, before it gets closed, you could use the STR_TO_DATE function to convert the strings into DATE datatype, and then do the comparison. MySQL won't be able to make use of an index range scan operation, it will need to evaluate that function on every flipping row in the table.
As an example:
SELECT t.mycol
FROM mytable t
WHERE STR_TO_DATE(t.mycol,'%d %M %Y') >= '2014-01-01'
AND STR_TO_DATE(t.mycol,'%d %M %Y') < '2015-01-01'
We'll need to check the MySQL Reference Manual to verify that '%M' is the right format specifier for the full month name...
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
Yes, it looks like I guessed right. M is the month name.
As I already commented, you should store your date as Timestamp or Data format then you could simply compare.
However, there is still a solution.. You can convert the varchar to a date directly in your query :
select * from yourTable
where (str_to_date(tb_date, '%d %M %Y') between '2014-01-01' and '2014-12-31');
But please don't use this hack and change your date format...
Edit : If you are really willing to use varchar to store your date, change it to varchar(17) which is the max character possible using your string format.
I have a char field which is having Date in it as '26-Nov-2011'
I want this date to be printed in YYYY-MM-DD format.
I used following date conversion which is not allowing me to do.
select date_format('26-Nov-2011','%d-%b-%y')
returns Null.
I know date_format function can accept date in YYYY-DD-MM format but
Please let me know how to convert a Character to Required Date Format
Try this:
SELECT STR_TO_DATE('26-Nov-2011', '%d-%b-%Y') dte;
Output:
2011-11-26
You can use STR_TO_DATE() to convert your strings to MySQL date values and ORDER BY the result
STR_TO_DATE(datestring, '%d/%m/%Y')
STR_TO_DATE can be used with this format '%d-%M-%Y', which will match the format of your current date string. Otherwise it will return null. Check on date formats as well.
select str_to_Date('26-Nov-2011','%d-%M-%Y')
from table1
;
Output:
| STR_TO_DATE('26-NOV-2011','%D-%M-%Y') |
-----------------------------------------
| November, 26 2011 00:00:00+0000 |
SQLFIDDLE DEMO
In my table, dates are stored like this: 2011-03-03T13:30:00
I'm trying to output dates like this: March 3, 2011 1:30 PM
I'd much rather work it into the query rather than use php to format it, but I'm having some difficulty doing that. Trying various iterations of DATE_FORMAT, but it's not giving me what I want, maybe because of the way it's being stored?
You basically have two different operations you may need to perform when handling dates: date to string and vice versa. The functions you can use are DATE_FORMAT() and STR_TO_DATE(). Full reference can be found in the manual.
Usage example:
SELECT
DATE_FORMAT(CURRENT_TIMESTAMP, '%d/%m/%Y %H:%i:%s'),
STR_TO_DATE('31/12/2001 23:55:00', '%d/%m/%Y %H:%i:%s')
If your dates are not real dates but strings, you'll need to convert twice: from string to date and again from date to string:
SELECT
STR_TO_DATE('2011-03-03T13:30:00', '%Y-%m-%dT%H:%i:%s'),
DATE_FORMAT(STR_TO_DATE('2011-03-03T13:30:00', '%Y-%m-%dT%H:%i:%s'), '%M %e, %Y %l:%i %p')
Use DATE_FORMAT:
DATE_FORMAT(date, "%M %e, %Y %h:%i %p")
The MySQL date storage format is actually YYYY-MM-DD, but using the str_to_date() and date_format() functions you can accept and generate any date format required.
select DATE_FORMAT(DateTable.MyDate,'%d %b %y')
from DateTable
would return
04 Nov 08
You should really use a DATETIME field for such things (and clean the input on the way in) rather than having to sort this out at the point of output.
Irrespective, you can simply use the DATE_FORMAT function to re-format your field into the format you require, although this might produce some un-expected results on a VARCHAR or CHAR field. (If so, you'll have to use STR_TO_DATE or failing that some various string functions to extract the date bits you require.)