Date without the time

前端 未结 2 490
渐次进展
渐次进展 2020-12-22 01:58

I\'m working with SQL Server 2005.

I have a column called purchase_time of type datetime. How do I select this column with the time part - just the date.

Tha

相关标签:
2条回答
  • 2020-12-22 02:28

    In SQL Server 2008 you can use the newly added date type:

    select convert(date, purchase_time) from TableName
    

    Update:

    In versions prior to SQL 2008, I used the following solution for this problem:

    select convert(datetime, convert(int, convert(float, purchase_time)))
    from TableName
    
    0 讨论(0)
  • 2020-12-22 02:46

    In versions < 2008 (which, based on other comments to some of the answers, I believe you are running), the most efficient way is to keep it as a datetime type and use date math to avoid string conversions.

    SELECT DATEADD(DAY, DATEDIFF(DAY, '20000101', purchase_time), '20000101') 
      FROM dbo.table;
    

    EDIT

    If you want the date only for display purposes, not for calculations or grouping, that is probably best handled at the client. You can do it in SQL simply by saying:

    SELECT dt = CONVERT(CHAR(10), purchase_time, 120)
      FROM dbo.table;
    
    0 讨论(0)
提交回复
热议问题