Oracle遷移mysql修改語法時,發現日期比較出現報錯,原因是date_format函數返回字符集
- 報錯片段,其中BEGIN_DATE是data_time類型
SELECT * FROM xxx a where date_format('2018/11/1', '%Y-%m') < a.BEGIN_DATE;
- 官方解釋
DATE_FORMAT() returns a string with a character set and collation given by character_set_connection and collation_connection so that it can return month and weekday names containing non-ASCII characters.
- 直接說方案吧
方案一:兩邊都date_format
SELECT * FROM xxx a where date_format('2018/11/1', '%Y-%m') < date_format(a.BEGIN_DATE, '%Y-%m-%d');
方案二:前面的常量format字符串加上“日”,’%Y-%m-%d’
SELECT * FROM xxx a where date_format('2018/11/1', '%Y-%m-%d') < a.BEGIN_DATE;
方案三:表字段放在前面
SELECT * FROM xxx a where a.BEGIN_DATE > date_format('2018/11/1', '%Y-%m') ;