parsing string in mysql and get the date - mysql

Does anyone here know how to parse string in mysql?
I have a column which value is in string.
example
movie1view_2014_01_01
movie2view_2014_05_02_part1
I want to get only the date that is this example 2014_01_01 and 2014_05_02 and convert it into date.

This is how you can do
select replace(SUBSTRING_INDEX(SUBSTRING_INDEX(name,'_',4),'_',-3),'_','-') as
date
from test
DEMO

Related

Parsing Custom Date Format in MySQL

Problem
I need to query out a date value for use in some ETL processing. However, the data is stored in MySQL as a VARCHAR column in values like 1/1/19.
What I've tried
A simple CAST(myDateColumn as DATE) fails as I get values like 0001-01-19 returned back.
Question
Am I able to pass a custom date format string into the CAST call somehow to tell it how to parse out the date parts? If not, could a SUBSTRING type of function help here?
As discussed in the comments above, you can use the STR_TO_DATE() function to parse a string into a proper YYYY-MM-DD date.

How to convert a string month name into month number in SQL

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.

Convert any valid date format to date in mysql

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}';

Tableau Timestamp String to Date

i have a timestamp in my bigquery looking like this: 2017.09.25 10:22:19
i want to convert this string to a date dimension. i tried it with the dropdown menu, calculated fields like datetime, dateparse, date,... and a calculated field where i trimmed the string and took only parts of the date as a sum, but nothing is working. I always get the error that google bigquery could'nt compile my task: "Invalid date: '2017.07.03 10:52:16' "
does anyone have an idea as a solution for my problem?
regards
Date parts need to be separated with dashes, not dots, in order for the cast to work. For example,
'2017-09-25 10:22:19'
As a string, this is valid to cast both to a DATETIME and a TIMESTAMP type. If you want to convert your original string to one of these types, however, you can use PARSE_DATETIME, or similarly PARSE_TIMESTAMP:
SELECT
PARSE_DATETIME('%Y.%m.%d %T', timestamp_string) AS datetime
FROM YourTable;

String To Date Conversion in Mysql

I have a column values like 20150921. I want to convert this string value to a specified date format like 2015-09-21 in mysql.
Try this:
date_format(str_to_date('20150921', '%Y%m%d'),'%Y-%m-%d')
SQL FIDDLE DEMO
(Assuming that you want your date format to be in YYYY-MM-DD format)
I am able to run this query on my MySql 5.6.
SELECT DATE('20150921');
So i believe, this should work directly DATE(colStringDate)