如何将日期与字符串进行比较

发布于 2025-02-07 01:48:25 字数 939 浏览 1 评论 0原文

我有一个MySQL查询,我在其中尝试在Where子句中使用字符串。

select s.schedule_date 
from schedule s 
WHERE DATE(s.schedule_date) = '2022-06-04'

而且它行不通。
我也尝试了 s.schedule_date ='2022-06-04' 日期(s.schedule_date)= date('2022-06-04'),

但似乎无效。我看过的大多数文章都说SQL示例中的位置应起作用。

select
    s.schedule_date,DATEDIFF(s.schedule_date,"2022-06-04")
from
    schedule s
WHERE
    s.schedule_date >= "2022-06-03" and
    s.schedule_date <= "2022-06-05";

​> “在此处输入图像描述”

I have a mysql query where I am trying to use a string in the where clause.

select s.schedule_date 
from schedule s 
WHERE DATE(s.schedule_date) = '2022-06-04'

and it does not work.
I have also tried
s.schedule_date = '2022-06-04'
DATE(s.schedule_date) = DATE('2022-06-04')

But nothing seems to work. Most articles I have seen say the where in the SQL example should work.
enter image description here

enter image description here

select
    s.schedule_date,DATEDIFF(s.schedule_date,"2022-06-04")
from
    schedule s
WHERE
    s.schedule_date >= "2022-06-03" and
    s.schedule_date <= "2022-06-05";

Give me 1 when it should be 0.enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃兔兔 2025-02-14 01:48:25

事实证明,这是一个问题。我将设置nodatimestringsync设置的mySQL驱动程序更改为true。默认为false。不知道为什么它只是开始这样做

I turns out it was a DBeaver issue. I changed the MySQL driver setting noDatetimeStringSync to true. The default it false. Not sure why it just started doing this

痞味浪人 2025-02-14 01:48:25

您可以尝试使用mysql str_to_date 函数:它将字符串转换为日期,给定输入格式,在您的情况下,应为>%y-%m-%d

SELECT s.schedule_date 
FROM schedule s 
WHERE DATE(s.schedule_date) = STR_TO_DATE('2022-06-04', '%Y-%m-%d')

它可以解决您的问题吗?

You can try using the MySQL STR_TO_DATE function: it converts a string to a date, given the input format, that in your case should be %Y-%m-%d.

SELECT s.schedule_date 
FROM schedule s 
WHERE DATE(s.schedule_date) = STR_TO_DATE('2022-06-04', '%Y-%m-%d')

Does it solve your problem?

少女的英雄梦 2025-02-14 01:48:25

您可以使用 cast function-function,以将字符串转换为日期。当然,铸件也可以用于任何其他转换(int将字符串,char(n)的字符串,...)。就您而言,查询应该看起来像这样:

    SELECT
    s.schedule_date, DATEDIFF(s.schedule_date, CAST("2022-06-04" AS DATE))
    FROM
    schedule s
    WHERE
    s.schedule_date >= CAST("2022-06-03" AS DATE) AND
    s.schedule_date <= CAST("2022-06-05" AS DATE);

You could use the CAST-Function, to convert your strings into dates. Of course, CAST can also be used for any other transformation (INT to STRING, STRING TO CHAR(n), ...). In your case, the query should look like this:

    SELECT
    s.schedule_date, DATEDIFF(s.schedule_date, CAST("2022-06-04" AS DATE))
    FROM
    schedule s
    WHERE
    s.schedule_date >= CAST("2022-06-03" AS DATE) AND
    s.schedule_date <= CAST("2022-06-05" AS DATE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文