Time part of a DateTime Field in SQL

后端 未结 11 1645
小蘑菇
小蘑菇 2020-12-03 04:39

How would I be able to extract the time part of a DateTime field in SQL? For my project I have to return data that has a timestamp of 5pm of a DateTime field no matter what

相关标签:
11条回答
  • 2020-12-03 04:44
    SELECT DISTINCT   
                     CONVERT(VARCHAR(17), A.SOURCE_DEPARTURE_TIME, 108)  
    FROM  
          CONSOLIDATED_LIST AS A  
    WHERE   
          CONVERT(VARCHAR(17), A.SOURCE_DEPARTURE_TIME, 108) BETWEEN '15:00:00' AND '15:45:00'
    
    0 讨论(0)
  • 2020-12-03 04:47

    Try this in SQL Server 2008:

    select *
    from some_table t
    where convert(time,t.some_datetime_column) = '5pm'
    

    If you want take a random datetime value and adjust it so the time component is 5pm, then in SQL Server 2008 there are a number of ways. First you need start-of-day (e.g., 2011-09-30 00:00:00.000).

    • One technique that works for all versions of Microsoft SQL Server as well as all versions of Sybase is to use convert/3 to convert the datetime value to a varchar that lacks a time component and then back into a datetime value:

      select convert(datetime,convert(varchar,current_timestamp,112),112)
      

    The above gives you start-of-day for the current day.

    • In SQL Server 2008, though, you can say something like this:

      select start_of_day =               t.some_datetime_column
                          - convert(time, t.some_datetime_column ) ,
      from some_table t
      

      which is likely faster.

    Once you have start-of-day, getting to 5pm is easy. Just add 17 hours to your start-of-day value:

    select five_pm = dateadd(hour,17, t.some_datetime_column
                       - convert(time,t.some_datetime_column)
                       )
    from some_table t
    
    0 讨论(0)
  • 2020-12-03 04:47

    This should strip away the date part:

    select convert(datetime,convert(float, getdate()) - convert(int,getdate())), getdate()
    

    and return a datetime with a default date of 1900-01-01.

    0 讨论(0)
  • 2020-12-03 04:51

    Note that from MS SQL 2012 onwards you can use FORMAT(value,'format')

    e.g. WHERE FORMAT(YourDatetime,'HH:mm') = '17:00'

    0 讨论(0)
  • 2020-12-03 04:52

    I know this is an old question, but since the other answers all

    • return strings (rather than datetimes),
    • rely on the internal representation of dates (conversion to float, int, and back) or
    • require SQL Server 2008 or beyond,

    I thought I'd add a "pure" option which only requires datetime operations and works with SQL Server 2005+:

    SELECT DATEADD(dd, -DATEDIFF(dd, 0, mydatetime), mydatetime)
    

    This calculates the difference (in whole days) between date zero (1900-01-01) and the given date and then subtracts that number of days from the given date, thereby setting its date component to zero.

    0 讨论(0)
  • 2020-12-03 04:56

    you can use CONVERT(TIME,GETDATE()) in this case:

    INSERT INTO infoTbl
    (itDate, itTime)
    VALUES (GETDATE(),CONVERT(TIME,GETDATE()))
    

    or if you want print it or return that time use like this:

    DECLARE @dt TIME
    SET @dt = CONVERT(TIME,GETDATE())
    PRINT @dt
    
    0 讨论(0)
提交回复
热议问题