[英]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. 在SQL Sever中转换为在谓词中使用的最佳短日期是什么。
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. 我有一个日期2013-06-11 15:06:27.000
并且想在谓词中使用短日期部分2013-06-11
。
What would be the best short date to convert to in SQL Server for this purpose? 为此目的,在SQL Server中转换为最佳短期日期是什么时候?
Select Convert(DateTime, Convert(VarChar, GetDate(), 12))
not really that hard, if you are using sqlserver 2008+ 如果您使用的是sqlserver 2008+,那就不那么难了
cast(@date as date)
or 要么
convert(date, @date)
Since you are using sqlserver 2005 因为您使用的是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: 如果您使用的是2008之前的SQL Server版本,那么我将使用以下方法:
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. 您也可以选择将此方法用于2008之后的版本,具体取决于您打算对此值执行的操作...如果您要将其与另一个datetime
值进行比较,则此方法至少会保留相同的数据类型进行比较。 If it is to compare against a date
value then used the aforementioned SELECT Cast(Current_Timestamp As date);
如果要与date
值进行比较,则使用前面提到的SELECT Cast(Current_Timestamp As date);
For SQL2005+: 对于SQL2005 +:
Note: Select Convert(DateTime, Convert(VarChar, DateTimeColumn, 12)) <operator> <const>
isn't SARG
able! 注意: Select Convert(DateTime, Convert(VarChar, DateTimeColumn, 12)) <operator> <const>
不是SARG
能够的! So, if you have an index on DateTimeColumn
then SQL Server can't seek ( Index Seek
) on this column. 因此,如果您在DateTimeColumn
上有索引,则SQL Server无法在此列上搜索( Index Seek
)。 Instead, SQL Server will use an Index Scan
, Clustered Index Scan
or Table Scan
. 相反,SQL Server将使用Index Scan
, Clustered Index Scan
或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. 如果要在DATETIME
列上过滤行,可以使用DateTimeColumn >= RangeStart AND DateTimeColumn < RangeEnd
或DateTimeColumn BETWEEN RangeStart AND RangeEnd
谓词。
How can be generated RangeStart
and RangeEnd
? 如何生成RangeStart
和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 ). 注2 :对于DATETIME
列,最后一毫秒可以是其中一个{0,3,7}
(参见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: 示例#1:
...
WHERE h.OrderDate>=DATEADD(DAY,DATEDIFF(DAY,0,@SelectedDate),0)
AND h.OrderDate<DATEADD(DAY,DATEDIFF(DAY,0,@SelectedDate)+1,0)
Example #2: 示例#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: 示例#3:
...
WHERE h.OrderDate BETWEEN DATEADD(DAY,DATEDIFF(DAY,0,@SelectedDate),0) AND DATEADD(MILLISECOND,-3,DATEADD(DAY,DATEDIFF(DAY,0,@SelectedDate)+1,0))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.