2008年9月16日

T-SQL效能調校-日期

/* 找最近60天的資料 */
方法一:效能高
SELECT top 10 * FROM [dbo].[tbl_name] where strdate <= dateadd(d,60,getdate())

方法二:
SELECT top 10 * FROM [dbo].[tbl_name] where datediff(d,getdate(),strdate) <= 60

/* 找 2008/5/1 的資料 */
方法一:找不到資料
SELECT * FROM [dbo].[tblname] Where strDate = '2008/5/1'

方法二:
SELECT * FROM [dbo].[tblname] Where DateDiff(D,strDate , '2008/5/1') = 0

方法三:效能較高
SELECT *
FROM [dbo].[tblname]
Where strDate between Cast('2008/5/1 00:00:00.000' as datetime) and Cast('2008/5/1 23:59:59.999' as datetime)

沒有留言: