开发者

MySql's date separator in str_to_date

开发者 https://www.devze.com 2023-01-25 12:11 出处:网络
I allow users to enter dates as 15-10-2010 and 15/10/2010. Problem is, I can only specify one separator type in mysql\'开发者_Python百科s str_to_date. So if a user enters 15-10-2010, then STR_TO_DATE(

I allow users to enter dates as 15-10-2010 and 15/10/2010. Problem is, I can only specify one separator type in mysql'开发者_Python百科s str_to_date. So if a user enters 15-10-2010, then STR_TO_DATE('15-10-2010','%d-%m-%Y') would work fine. But if the user did 15/10/2010, I get a null value in the database column. Is there a way to tell mysql to all both - and / as the seperator?


You could use the Replace function in mysql.

Something like this:

STR_TO_DATE(REPLACE('15/10/2010', '/', '-'), '%d-%m-%Y')

Hope this helps, Arne

0

精彩评论

暂无评论...
验证码 换一张
取 消