Convert a SQL Server datetime to a shorter date format

前端 未结 10 512
一个人的身影
一个人的身影 2021-01-30 05:59

I have a datetime column in SQL Server that gives me data like this 10/27/2010 12:57:49 pm and I want to query this column but just have SQL Server ret

相关标签:
10条回答
  • 2021-01-30 06:39

    The shortest date format of mm/dd/yy can be obtained with:

    Select Convert(varchar(8),getdate(),1)
    
    0 讨论(0)
  • 2021-01-30 06:41

    Try this:

    print cast(getdate() as date )

    0 讨论(0)
  • 2021-01-30 06:41

    In addition to CAST and CONVERT, if you are using Sql Server 2008, you can convert to a date type (or use that type to start with), and then optionally convert again to a varchar:

    declare @myDate date
    set @myDate = getdate()
    print cast(@myDate as varchar(10))
    

    output:

    2012-01-17
    
    0 讨论(0)
  • 2021-01-30 06:44

    With SQL Server 2005, I would use this:

    select replace(convert(char(10),getdate(),102),'.',' ')
    

    Results: 2015 03 05

    0 讨论(0)
  • 2021-01-30 06:44

    Just add date keyword. E.g. select date(orderdate),count(1) from orders where orderdate > '2014-10-01' group by date(orderdate);

    orderdate is in date time. This query will show the orders for that date rather than datetime.

    Date keyword applied on a datetime column will change it to short date.

    0 讨论(0)
  • 2021-01-30 06:50

    For any versions of SQL Server: dateadd(dd, datediff(dd, 0, getdate()), 0)

    0 讨论(0)
提交回复
热议问题