How can we check if a string is a valid date without specifying the format in MySQL.A user may enter below formats;
21-08-2013
21/08/2013
2013/08/21
21jan2018
I don't know the format in advance which a user may enter.
I tried :
Select str_to_date() but here I need to specify format which I don't want.
I just want to check if a string is valid date without specifying the format.
Please suggest.
Thanks
All four of the date strings you showed us in the question can be converted to a MySQL date using STR_TO_DATE with an appropriate format mask. So, you should be able to simply check for these date patterns, if all you want is to know if you could convert them to dates:
SELECT *
FROM yourTable
WHERE
col REGEXP '[0-9]{2}-[0-9]{2}-[0-9]{4}' OR
col REGEXP '[0-9]{2}/[0-9]{2}/[0-9]{4}' OR
col REGEXP '[0-9]{4}/[0-9]{2}/[0-9]{2}' OR
col REGEXP '[0-9]{2}[A-Z]{3}[0-9]{4}';
Related
How can I convert a string name into the actual month number. For instance if I have a string as 'March', how can this be converted to 3.
I know that this can be done using a case statement but then I want to know if there is any predefined function that can do this conversion.
select date_format(str_to_date('March','%M'),'%c')
firstly, using str_to_date to convert string to month name, and then format the month name to number format
month(str_to_date(MonthField,'%M'))
For more information about month and str_to_date, you can check the link
example for above is select month(str_to_date('March','%M')) from dual;
will give the result 3
Use the STRING_TO_DATE function:
SELECT STRING_TO_DATE('March','%M')
See the (function reference](https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_str-to-date).
See the date formatting codes reference.
I have data in excel in which there is a column date formatted dd/mm/yyyy.
that excel data I want to convertted to mysql.
the problem is after converting this, mysql read as 0000-00-00.
I used formula =TEXT(A1,"YYYY-MM-DD") but the excel ask for punctuation mark (') in front of sign equal to (=)
I change the date to 'YYYY-MM-DD and mysql success to read the date.
What exactly the best formula I used to apply to all (if more than 200) date so it can read by mysql.
thanks
This should do the trick:
Highlight the column
Right click and select Format Cells
Select Custom
In the Type field enter yyyy-mm-dd
Now your dates should be in the correct format.
I need to convert dates of varying strings. They come in 3 different ways.
yyyy/mm/dd
mm/dd/yyyy
or blank (can fill in some default)
What is a good way to handle this situation for an INSERT statement?
Use STR_TO_DATE function in combination with COALESCE - something like:
set #strdate := '2014/05/10';
select COALESCE(STR_TO_DATE(#strdate,'%m/%d/%Y'),STR_TO_DATE(#strdate,"%Y/%m/%d"))
You can use MySQL str_to_date() function. It requires one format and will return NULL if the data doesnt match the format. UseCoalesce with str_to_date with str_to_date in descending order of likelihood of the format.
Example if yyyy/mm/dd is more common than mm/dd/yyyy then use
COALESCE(STR_TO_DATE(your_date_here,'%Y/%m/%d'),STR_TO_DATE(#strdate,"%m/%d/%Y"))
Edit: This is the same as Ondřej Šotek's answer but with a possible performance improvement
Following is my sql query kindly let me know why is it returning null
Select STR_TO_DATE ('11-APR-74','%e%b%Y')
OR
Select DATE_FORMAT ('11-APR-74','%e%b%Y')
From MySQL STR_TO_DATE function:
The server scans str attempting to match format to it. ... Scanning
starts at the beginning of str and fails if format is found not to
match.
This is why your first query fails: 11-APR-74 does not look like %e%b%Y, so date cannot be parsed. You should do instead
SELECT STR_TO_DATE ('11-APR-74','%e-%b-%Y')
From MySQL Date and Time types:
Although MySQL tries to interpret values in several formats, date
parts must always be given in year-month-day order (for example,
'98-09-04'), rather than in the month-day-year or day-month-year
orders commonly used elsewhere (for example, '09-04-98', '04-09-98').
This is why your second query fails: 74 is not a valid day of month, you should do instead
SELECT DATE_FORMAT ('74-APR-11','%e%b%Y')
Note, that DATE_FORMAT is usually used on DB values, not string literals as you do - to get an output different from the default one.
If you want to convert from string to date
Select STR_TO_DATE ('11-APR-74','%d-%b-%y')
use it like ::
Select STR_TO_DATE ('11-APR-74','%e-%b-%Y')
Because '%e%b%Y' format does not correspond to '11-APR-74' string value (as STR_TO_DATE function expects), and because '11-APR-74' value is of type CHAR, but not DATETIME (as DATE_FORMAT function expects).
If you want to reformat a date represented by a CHAR value, convert it from its original format to DATETIME first, and then convert it to a string of desired format:
SELECT DATE_FORMAT(STR_TO_DATE('11-APR-74','%e-%b-%Y'),'%e%b%Y');
By the way, you could strip dashes with a plain string sunction:
SELECT REPLACE('11-APR-74','-','');
I'm trying to select all columns after a specific date but the trick is the "date" column is varchar
I have this
SELECT * FROM `users` WHERE STR_TO_DATE(birthday,'%m/%d/%Y') > '10-10-2000'
as an example and I want to select all users whos birthday is after the 10/10/2000 but this just returns all rows.
Anybody got a clue what's wrong?
You are comparing a data and a string. I would use CAST:
WHERE STR_TO_DATE(birthday,'%m/%d/%Y') > CAST('2000-10-10' AS date)
Also note that I changed the format of the second to match what mysql expects. You could also use STR_TO_DATE here.
I think mysql dates are usually yyyy-mm-dd which means you should change the string at the end to 2000-10-10.
Try that and let me know if that works.
We need to know what format the string birthday is in, now you're saying it is e.g. 12/31/2001. Is that right?
If you have multiple strings, consider using the same STR_TO_DATE-formatting:
WHERE STR_TO_DATE(birthday, '%m/%/d/%Y') BETWEEN STR_TO_DATE(some_date, '%m/%/d/%Y')
AND STR_TO_DATE(another_date, '%m/%/d/%Y')
More info:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date