How can i obtain the date only from the datetime column?

This question already has an answer here:

  • How to return only the Date from a SQL Server DateTime datatype 37 answers

  • SELECT CONVERT(VARCHAR(10), GETDATE(), 101) 
    

    Returns

    11/15/2013
    
    SELECT CONVERT(VARCHAR(10), GETDATE(), 120)
    

    Returns

    2013-11-15
    

    Most efficient will be to keep it is a date type instead of converting it to a string:

    SELECT CONVERT(DATE, GETDATE());
    

    If you really want string output for whatever reason:

    SELECT CONVERT(CHAR(10), GETDATE(), 120);
    

    (There's no reason to use VARCHAR , and the way to get a specific length to a string is to specify a length for the string. Please read this post.)

    The accepted and highly up-voted answer on the proposed duplicate - which I'm sure will eventually close this question, due to lemming factor - does not adequately address this problem.

  • It uses a technique that is an efficient method for SQL Server 2000 and SQL Server 2005, but not for SQL Server 2008+, since the above conversion is more efficient than doing the date math (the difference is probably negligible, but it's the principle of the thing).

  • It still returns time in the output (it's just at midnight instead of now). The OP here doesn't want time included in the output (whether that's the real time, or midnight, etc).


  • 因为你在SQL Server 2008上

    SELECT cast(GETDATE() as DATE)
    
    链接地址: http://www.djcxy.com/p/94376.html

    上一篇: getdate()函数

    下一篇: 我怎样才能从datetime列获取日期?