mysql - 为什么我从这个查询中得到空值

标签 mysql

以下是我的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/

相关文章:

c# - gridview使用时间戳更新更新 asp.net 时出错

mysql - UNION 和 ORDER BY 的错误使用

php - 如何简化这个mysql查询

具有两个输入列的 MySql CASE 表达式

PHP变量和MySQL查询

mysql - 为什么我的查询没有显示错误,但也返回零行?

java - Hibernate/mysql 连接问题

php - 来自 Ajax GET 的语法错误 MySQL

php - PDO多次插入记录,等于数据库值

mysql - 当 curdate() = future 日期 MYSQL 时如何删除事件