22,207
社区成员
发帖
与我相关
我的任务
分享
--设用户输入的查询参数为@beginmonth和@endmonth,类型为字符串,格式为"2010-7"
DECLARE @sdate datetime, @edate datetime
SET @sdate = cast(@beginmonth+'-1' as datetime)
SET @edate = dateadd(d,-1,dateadd(m,1,cast(@endmonth+'-1' as datetime)))
查询的时候用FROM table where (日期>=@sdate and 日期<=@edate)
--直接
cast(日期 as date) between '2010-7-1' and '2010-9-30'
select * from tb where convert(varchar(7),日期,120) between '2010-07' and '2010-09'
SELECT 日期,销售量
FROM table where convert(varchar(7),日期,120)>='2010-07' and convert(varchar(7),日期,120)<='2010-09'
where 日期>='2010-7-1' and 日期<'2010-10-1'
SELECT 日期,销售量
FROM table where convert(varchar(7),日期,120)>='2010-07' and convert(varchar(7),日期,120<='2010-09'