【问题标题】:Getting the 'date' out of a DateTime field in SQL Server从 SQL Server 中的 DateTime 字段中获取“日期”
【发布时间】:2011-02-07 10:37:11
【问题描述】:

我有一个日期列,其中日期以2009-11-18 10:55:28.370 格式显示。

我只想从该值中获取日期(而不是时间)。我该怎么做?

【问题讨论】:

    标签: sql-server tsql sql-server-2008 datetime


    【解决方案1】:

    如果您使用的是 SQL Server 2008,现在有一个 DATE 数据类型。让它更自然!

    SELECT CONVERT(Date, GETDATE())
    

    【讨论】:

    • 好的答案是:从 WEB_RISK_RISK 中选择 creation_date,其中 CREATION_DATE = convert(date,GETDATE()) - 感谢您的提示。
    【解决方案2】:

    它被称为“降低日期时间”,这样做只是为了删除时间(这是最快的方法,比使用 CONVERT() 或 CAST() sting 格式更快):

    DECLARE @datetime datetime;
    SET @datetime = '2008-09-17 12:56:53.430';
    SELECT DATEADD(day,DATEDIFF(day,0,@datetime),0)
    

    输出:

    -----------------------
    2008-09-17 00:00:00.000
    
    (1 row(s) affected)
    

    下面是如何对日期时间的其他部分执行此操作:

    --Floor a datetime
    DECLARE @datetime datetime;
    SET @datetime = '2008-09-17 12:56:53.430';
    
    SELECT '0 None',  @datetime                                                                   -- none    2008-09-17 12:56:53.430
    UNION SELECT '1 Second',DATEADD(second,DATEDIFF(second,'2000-01-01',@datetime),'2000-01-01')  -- Second: 2008-09-17 12:56:53.000
    UNION SELECT '2 Minute',DATEADD(minute,DATEDIFF(minute,0,@datetime),0)                        -- Minute: 2008-09-17 12:56:00.000
    UNION SELECT '3 Hour',  DATEADD(hour,DATEDIFF(hour,0,@datetime),0)                            -- Hour:   2008-09-17 12:00:00.000
    UNION SELECT '4 Day',   DATEADD(day,DATEDIFF(day,0,@datetime),0)                              -- Day:    2008-09-17 00:00:00.000
    UNION SELECT '5 Month', DATEADD(month,DATEDIFF(month,0,@datetime),0)                          -- Month:  2008-09-01 00:00:00.000
    UNION SELECT '6 Year',  DATEADD(year,DATEDIFF(year,0,@datetime),0)                            -- Year:   2008-01-01 00:00:00.000
    ORDER BY 1
    PRINT' '
    PRINT 'Note that when you are flooring by the second, you will often get an arithmetic overflow if you use 0. So pick a known value that is guaranteed to be lower than the datetime you are attempting to floor'
    PRINT 'this always uses a date less than the given date, so there will be no arithmetic overflow'
    SELECT '1 Second',DATEADD(second,DATEDIFF(second,DATEADD(day,DATEDIFF(day,0,@datetime),0)-1,@datetime),DATEADD(day,DATEDIFF(day,0,@datetime),0)-1)  -- Second: 2008-09-17 12:56:53.000
    

    输出:

    -------- -----------------------
    0 None   2008-09-17 12:56:53.430
    1 Second 2008-09-17 12:56:53.000
    2 Minute 2008-09-17 12:56:00.000
    3 Hour   2008-09-17 12:00:00.000
    4 Day    2008-09-17 00:00:00.000
    5 Month  2008-09-01 00:00:00.000
    6 Year   2008-01-01 00:00:00.000
    
    (7 row(s) affected)
    
    
    Note that when you are flooring by the second, you will often get an arithmetic overflow if you use 0. So pick a known value that is guaranteed to be lower than the datetime you are attempting to floor
    this always uses a date less than the given date, so there will be no arithmetic overflow
    
    -------- -----------------------
    1 Second 2008-09-17 12:56:53.000
    
    (1 row(s) affected)
    

    【讨论】:

      【解决方案3】:

      如果我没看错,

      select convert(varchar, creation_date , 103) as creation_date from tablename

      CAST and CONVERT

      【讨论】:

        【解决方案4】:

        这里:

        SELECT creation_date 
        FROM risks
        WHERE creation_date = GETDATE()
        

        这将返回存储在risks 表中的所有creation_date 值,这些值与GETDATE() 函数返回的值完全相同。我假设creation_date的数据类型是Date

        【讨论】:

        • 但这不会返回当前日期的 creation_date 值。这不就是用 creation_date = getdate() 做的吗
        • @thegunner - 您没有在问题中指定。我应该猜?
        【解决方案5】:

        你只需要像这样在你的选择子句中包含creation_date:

        select id, creation_date from risks where creation_date = getdate() 
        

        【讨论】:

        • 试过它不起作用....必须在某处转换从 WEB_RISK_RISK 中选择 creation_date,其中 CREATION_DATE=GETDATE()。 p.s.有一条记录,今天已经改正了。
        【解决方案6】:

        您总是可以使用月/日/年函数来返回它:

        declare @date datetime
        set @date = '1/1/10 12:00 PM'
        select cast(month(@date) as varchar) + '/' + cast(day(@date) as varchar) + '/' + cast(year(@date) as varchar) as theDate
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-08-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-25
          • 2015-05-21
          • 2022-01-20
          相关资源
          最近更新 更多