--1.
--查询五月份的记录
use pubs
go
select * from sales where month(ord_date)='5'
--这种的效率比第一种高,用函数做查询,效率很低,特别是上了万级的数据,因这会引发全表扫描
SELECT * FROM sales WHERE ord_date>='1993-05-01' AND ord_date<'1993-06-01'
--2.
--清空表的记录
truncate table Table_Name
select * from sales
GO
SELECT DATEADD(wk,5601, 6)AS TimeFrame
FROM Sales
GO
--3.
--这个月第一个周日(2007-05-06 00:00:00.000)
select Dateadd(wk,datediff(wk,0,getdate()),-1)
--从6号以后的周日(2007-05-13 00:00:00.000)
select Dateadd(wk,datediff(wk,0,getdate()),6)
--这个月第一天(2007-05-01 00:00:00.000)
select Dateadd(mm,datediff(mm,0,getdate()),0)
--这个月的最后一天(2007-05-31 23:59:59.997)
select Dateadd(ms,-3,dateadd(mm,datediff(m,0,getdate())+1,0))
--今年的第一天(2007-01-01 00:00:00.000)
select Dateadd(yy,datediff(yy,0,getdate()),0)
--今年的最后一天(2007-12-31 23:59:59.997)
select Dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0))
本文介绍了使用SQL进行高效查询的方法,包括如何针对特定月份的数据进行检索,以及清空表中所有记录的操作。此外,还提供了获取当前月份及特定日期范围内的各种日期基准点的SQL表达式。

被折叠的 条评论
为什么被折叠?



