首页 文章

将结果与今天的日期进行比较?

提问于
浏览
60

有没有办法在SQL中使用 Now() 函数来选择具有今天日期的值?

我的印象是 Now() 会包含时间和日期,但今天的日期会将时间设置为 00:00:00 ,因此这永远不会匹配?

9 回答

  • 2

    只关闭日期的时间元素 . 例如

    select    DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)
    

    我已经使用了GetDate作为MSSQL函数,因为你已经标记了,但是Now()可能是MySQL,或者你正在使用ODBC函数调用,如果你只是用另一个替换它,它仍然可以工作 .

  • 0

    不确定你想要做什么,但听起来像 GETDATE() 就是你所追求的 . GETDATE() 返回日期时间,但如果您对时间组件不感兴趣,则可以转换为日期 .

    SELECT  GETDATE()
    SELECT  CAST(GETDATE() AS DATE)
    
  • 7

    不确定你的要求!

    然而

    SELECT  GETDATE()
    

    会告诉你当前的日期和时间

    SELECT  DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
    

    只需将时间设置为00:00:00即可获得

  • 3

    SQL Server中没有本机Now()函数,因此您应该使用:

    select GETDATE() --2012-05-01 10:14:13.403
    

    您可以通过以下方式分别获得日,月和年:

    select DAY(getdate())  --1
    select month(getdate())  --5
    select year(getdate()) --2012
    

    如果你在sql server 2008上,那么DATE日期时间只有日期部分,而不是时间:

    select cast (GETDATE() as DATE) --2012-05-01
    
  • 5

    你可以试试这个sql代码;

    SELECT [column_1], [column_1], ...    
        FROM (your_table)
         where date_format(record_date, '%e%c%Y') = date_format(now(), '%e%c%Y')
    
  • 83

    如果你的表只有一个存储的日期(没有时间),并希望通过“现在”得到它们,那么你可以这样做:

    SELECT * FROM tbl WHERE DATEDIFF(d, yourdate, GETDATE())=0
    

    这导致行的日差为0(所以今天) .

  • 69

    基于之前的答案,请注意重要的一点,您还需要 manipulate your table column 以确保它不包含datetime数据类型的时间片段 .

    下面是一个展示上述内容的小样本脚本:

    select getdate()
    --2012-05-01 12:06:51.413
    select cast(getdate() as date)
    --2012-05-01
    
    --we're using sysobjects for the example
    create table test (id int)
    select * from sysobjects where cast(crdate as date) = cast(getdate() as date)
    --resultset contains only objects created today
    drop table test
    

    我希望这有帮助 .

    EDIT:
    关于@dwurf评论(感谢)关于上述示例可能对性能产生的影响,我想建议以下内容 . 我们创建一个日期范围,从今天午夜(一天的开始)和一天的最后一毫秒(SQL服务器计数到.997,即减少3毫秒的's why I') . 通过这种方式,我们避免操纵左侧并避免性能影响 .

    select getdate()
    --2012-05-01 12:06:51.413
    select dateadd(millisecond, -3, cast(cast(getdate()+1 as date) as datetime))
    --2012-05-01 23:59:59.997
    select cast(getdate() as date)
    --2012-05-01
    
    create table test (id int)
    select * from sysobjects where crdate between cast(getdate() as date) and dateadd(millisecond, -3, cast(cast(getdate()+1 as date) as datetime))
    --resultset contains only objects created today
    drop table test
    
  • 5

    其中created_date位于CURRENT_TIMESTAMP-180和CURRENT_TIMESTAMP之间

  • 0

    好的,让我们这样做吧 . 选择今天匹配的日期,使用索引(如果可用),并显示所有不同的日期/时间类型 .

    这里的原则在每种情况下都是相同的 . 我们 grab 日期列所在的行或最近午夜之后的行(今天的时间00:00:00)和下一个午夜之前(明天的日期时间00:00:00,但排除任何具有该确切值的行) ) .

    对于纯日期类型,我们可以与今天的日期进行简单比较 .

    为了保持良好和快速,我们明确避免对存储在DB中的日期进行任何操作(下面所有示例中的 where 子句的LHS) . 这可能会触发全表扫描,因为必须为每次比较计算日期 . (此行为似乎因DBMS,YMMV而异) .

    MS SQL Server:SQL Fiddle | db<>fiddle

    首先,使用DATE

    select * from dates 
    where dte = CAST(CURRENT_TIMESTAMP AS DATE)
    ;
    

    现在使用DATETIME:

    select * from datetimes 
    where dtm >= CAST(CURRENT_TIMESTAMP AS DATE)
    and dtm < DATEADD(DD, 1, CAST(CURRENT_TIMESTAMP AS DATE))
    ;
    

    最后使用DATETIME2:

    select * from datetimes2
    where dtm2 >= CAST(CURRENT_TIMESTAMP AS DATE)
    and dtm2 < DATEADD(DD, 1, CAST(CURRENT_TIMESTAMP AS DATE))
    ;
    

    MySQL:SQL Fiddle | db<>fiddle

    使用日期:

    select * from dates 
    where dte = cast(now() as date)
    ;
    

    使用DATETIME:

    select * from datetimes 
    where dtm >= cast((now()) as date)
    and dtm < cast((now() + interval 1 day) as date)
    ;
    

    PostgreSQL:SQL Fiddle | db<>fiddle

    使用日期:

    select * from dates 
    where dte = current_date
    ;
    

    使用TIMESTAMP不带时区:

    select * from timestamps
    where ts >= 'today'
    and ts < 'tomorrow'
    ;
    

    Oracle:SQL Fiddle

    使用日期:

    select to_char(dte, 'YYYY-MM-DD HH24:MI:SS') dte
    from dates 
    where dte >= trunc(current_date)
    and dte < trunc(current_date) + 1
    ;
    

    使用TIMESTAMP:

    select to_char(ts, 'YYYY-MM-DD HH24:MI:SS') ts
    from timestamps
    where ts >= trunc(current_date)
    and ts < trunc(current_date) + 1
    ;
    

    SQLite:SQL Fiddle

    使用日期字符串:

    select * from dates 
    where dte = (select date('now'))
    ;
    

    使用日期和时间字符串:

    select dtm from datetimes
    where dtm >= datetime(date('now'))
    and dtm < datetime(date('now', '+1 day'))
    ;
    

    使用unix时间戳:

    select datetime(dtm, 'unixepoch', 'localtime') from datetimes
    where dtm >= strftime('%s', date('now'))
    and dtm < strftime('%s', date('now', '+1 day'))
    ;
    

    Backup of SQL Fiddle code

相关问题