【发布时间】:2020-07-28 07:13:35
【问题描述】:
需要将oracle格式的sql转成mysql。
to_date(25-JAN-2020,'DD-MON-YYYY','nls_date_language = FRENCH').
在 mysql 中,我们可以通过更改会话来更改 nls_date_language,但是我们可以像上面那样在 mysql 中使用单独的日期时间来代替更改会话。
【问题讨论】:
标签: mysql sql oracle date select
需要将oracle格式的sql转成mysql。
to_date(25-JAN-2020,'DD-MON-YYYY','nls_date_language = FRENCH').
在 mysql 中,我们可以通过更改会话来更改 nls_date_language,但是我们可以像上面那样在 mysql 中使用单独的日期时间来代替更改会话。
【问题讨论】:
标签: mysql sql oracle date select
其实,比你想象的值。
在 MySQL 中,Oracle 的 to_date() 等价于 str_to_date(),而 nls_date_language 将对应于系统变量 lc_time_names。
然而,就像explained in the documentation(重点是我的):
此变量会影响
DATE_FORMAT()、DAYNAME()和MONTHNAME()函数的输出。
lc_time_names不影响STR_TO_DATE()或GET_FORMAT()功能。
因此,您实际上无法解析 dd-mon-yyyy 格式的日期,其中 mon 是非英语月份,仅使用内置函数和系统变量。
我能想到的唯一解决方案是在解析之前手动翻译日期缩写,使用大量嵌套的REPLACE()s 或CASE 表达式......事实上,一些月份名称确实在法语之间匹配英语稍微简化了任务:
select str_to_date(
case
when mystr like '%FEV%' then replace(mystr, 'FEV', 'FEB')
when mystr like '%AVR%' then replace(mystr, 'AVR', 'APR'),
when mystr like '%MAI%' then replace(mystr, 'MAI', 'MAY')
when mystr like '%JUI%' then replace(mystr, 'JUI', 'JUL'),
when mystr like '%AOU%' then replace(mystr, 'AOU', 'AUG')
end,
'%d-%b-%Y'
) mydate
from ...
【讨论】: