以下是我的sql查询请告诉我为什么它返回null
Select STR_TO_DATE ('11-APR-74','%e%b%Y')
OR
Select DATE_FORMAT ('11-APR-74','%e%b%Y')
最佳答案
来自 MySQL STR_TO_DATE function :
The server scans str attempting to match format to it. ... Scanning starts at the beginning of str and fails if format is found not to match.
这就是您的第一个查询失败的原因:11-APR-74 看起来不像 %e%b%Y,因此无法解析日期。你应该这样做
SELECT STR_TO_DATE ('11-APR-74','%e-%b-%Y')
来自 MySQL Date and Time types :
Although MySQL tries to interpret values in several formats, date parts must always be given in year-month-day order (for example, '98-09-04'), rather than in the month-day-year or day-month-year orders commonly used elsewhere (for example, '09-04-98', '04-09-98').
这就是您的第二个查询失败的原因:74 不是一个月中的有效日期,您应该这样做
SELECT DATE_FORMAT ('74-APR-11','%e%b%Y')
请注意,DATE_FORMAT 通常用于数据库值,而不是像您那样用于字符串文字 - 以获得与默认输出不同的输出。
关于mysql - 为什么我从这个查询中得到空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13855585/