简体   繁体   中英

SQL Server convert to optimal short date

What is the most optimal short date to convert to in SQL Sever to use in a predicate.

I have a date 2013-06-11 15:06:27.000 and want to use the short date part 2013-06-11 in a predicate.

What would be the best short date to convert to in SQL Server for this purpose?

MSDN - Date convert

Select Convert(DateTime, Convert(VarChar, GetDate(), 12))

not really that hard, if you are using sqlserver 2008+

cast(@date as date)

or

convert(date, @date)

Since you are using sqlserver 2005

CONVERT(CHAR(10), @date, 121)

However if you are going to compare with another date, keep the datetime format and remove the time part:

dateadd(day, 0, datediff(day, 0, @date))

那这个呢?

SELECT CONVERT(CHAR(12),GETDATE(),12)

If you're using a SQL Server version prior to 2008 then I would use the following method:

SELECT DateAdd(dd, DateDiff(dd, 0, Current_Timestamp), 0);

The reason I would chose this method over the others suggested is that it avoids casting to a character datatype (and back).

You may also chose to use this method for versions after and including 2008 depending on what you intend on doing to this value... If you're going to compare it to another datetime value, then this method will at least retain the same data type for comparison. If it is to compare against a date value then used the aforementioned SELECT Cast(Current_Timestamp As date);

For SQL2005+:

Note: Select Convert(DateTime, Convert(VarChar, DateTimeColumn, 12)) <operator> <const> isn't SARG able! So, if you have an index on DateTimeColumn then SQL Server can't seek ( Index Seek ) on this column. Instead, SQL Server will use an Index Scan , Clustered Index Scan or Table Scan .

If you want to filter rows on a DATETIME column you could use DateTimeColumn >= RangeStart AND DateTimeColumn < RangeEnd or DateTimeColumn BETWEEN RangeStart AND RangeEnd predicates.

How can be generated RangeStart and RangeEnd ?

    DECLARE @SelectedDate DATETIME;
    SET     @SelectedDate='2013-06-11 15:06:27.000';

    SELECT  DATEADD(DAY,DATEDIFF(DAY,0,@SelectedDate),0) AS Start,
            DATEADD(DAY,DATEDIFF(DAY,0,@SelectedDate)+1,0) AS [End 1],
            DATEADD(MILLISECOND,-3,DATEADD(DAY,DATEDIFF(DAY,0,@SelectedDate)+1,0)) AS [End 2]

Note 2 : For DATETIME column, the last millisecond can be one of these {0,3,7} (see BOL ).

Results:

Start                   End 1                   End 2
----------------------- ----------------------- -----------------------
2013-06-11 00:00:00.000 2013-06-12 00:00:00.000 2013-06-11 23:59:59.997

Example #1:

...
WHERE   h.OrderDate>=DATEADD(DAY,DATEDIFF(DAY,0,@SelectedDate),0)
AND     h.OrderDate<DATEADD(DAY,DATEDIFF(DAY,0,@SelectedDate)+1,0)

Example #2:

...    
WHERE   h.OrderDate>=DATEADD(DAY,DATEDIFF(DAY,0,@SelectedDate),0)
AND     h.OrderDate<=DATEADD(MILLISECOND,-3,DATEADD(DAY,DATEDIFF(DAY,0,@SelectedDate)+1,0))

Example #3:

...
WHERE   h.OrderDate BETWEEN DATEADD(DAY,DATEDIFF(DAY,0,@SelectedDate),0) AND DATEADD(MILLISECOND,-3,DATEADD(DAY,DATEDIFF(DAY,0,@SelectedDate)+1,0))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM