gpt4 book ai didi

mysql - 为什么在 "to_date"表达式中将 "where"应用于已经是 "date"类型的变量?

转载 作者:可可西里 更新时间:2023-11-01 08:31:51 24 4
gpt4 key购买 nike

我觉得这个问题应该很容易回答,但由于某些原因,我到处搜索并空手而归,即使使用mysql docs中的搜索功能也根本没有返回“to_date”功能!! !有人可以解释为什么在已经是“日期”变量的“bdate”上使用“to_date”吗?为什么要再次将其转换为日期?实际上对“to_date”的解释很受欢迎,因为搜索文档没有产生任何有用的东西?在一些讲座幻灯片中,教授举了一个例子:

mysql> create table student
(SID DECIMAL(7) primary key not null,
SNAME VARCHAR (20), MAJOR CHAR(4), YEAR DEC(1),
BDATE DATE);

然后他使用以下方式查询:

select s.sname
from student s
where to_date(s.bdate) like '%13%';

最佳答案

假设这个问题真的是关于具有 to_date() 函数的 Oracle 或 Postgres,这似乎是一个非常糟糕的主意。也许意图更像是:

select s.sname
from student s
where to_char(s.bdate, 'YYYY-MM-DD') like '%13%';

除了使用 to_date() 之外,like 比较假设是字符串。在这种情况下(对我而言)转换为日期没有意义。

编辑:

我应该补充一点 Oracle explicitly warns反对这样做:

Do not use the TO_DATE function with a DATE value for the char argument. The first two digits of the returned DATE value can differ from the original char, depending on fmt or the default date format.

关于mysql - 为什么在 "to_date"表达式中将 "where"应用于已经是 "date"类型的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24230102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com