Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

str_to_date for dates in other languages

Status
Not open for further replies.

vacunita

Programmer
Aug 2, 2001
9,166
MX
Not really used to getting knee deep in complex queries, but need to build a trigger to take a date in a format like "10 de febrero de 2014" into a usable date format. It seems the str_to_date function will not recognize month names in anything other than the set language for lc_time_names global. The database configuration cannot really be changed as other tables and columns depend on it.

Anyone have any suggestions, on how to actually get it to work?

I'm going to add this to a trigger, so that when data is inserted by a different process it can convert the dates automatically and make them more searchable, and accionable since I need to have a cron check every so often when something has expired via these dates.

Any help is appreciated.

Current query if it helps. It currently returns Null since it cannot identify the spanish month name:
Code:
SELECT STR_TO_DATE("10 de febrero de 2014","%d de %M de %Y") AS mydate;

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Figured out how to actually modify it. It's not the cleanest, nor likely the most optimal way, but it works:

The starting date string is a bit more complex than what I had above, as it contains 2 dates not just 1, separated by a slash /.

Code:
STR_TO_DATE(CONCAT(SUBSTRING_INDEX(TRIM(SUBSTRING_INDEX(fechas,"/",-1)),"de",1),(SELECT id FROM meses WHERE `name`= SUBSTRING_INDEX(TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(fechas,"/",-1),"de",-2)),"de",1)),SUBSTRING_INDEX(TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(fechas,"/",-1),"de",-2)),"de",-1)),"%e %m %Y")

I created a table to hold the spanish names of the months, and then used that to convert the months to their correct number.

The trigger will need to run this before insert.

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top