select YMD from FM where convert(varchar(7),YMD,120)>'1999-09' and convert(varchar(7),YMD,120)<'1999-09'
select YMD from FM where datediff(month,YMD,'1999/09/09')
select YMD from FM where datediff(month,YMD,'1999/09/09')=0
--得到1999年9月的数据 select ymd from fm where datediff(month,ymd,'1999/9/1')=0--得到1999年9月及以前的数据 select ymd from fm where datediff(month,ymd,'1999/9/1')>=0--得到1999年9月及以后的数据 select ymd from fm where datediff(month,ymd,'1999/9/1')<=0
create function datetoint (@日期 datetime) returns int as begin return year(@日期)*10000 + month(@日期)*100 + day(@日期) endgoselect YMD from FM where (select dbo.datetoint(YMD))>'19990909' and (select dbo.datetoint(YMD))<'19990909'
from
FM
where convert(varchar(7),YMD,120)>'1999-09' and convert(varchar(7),YMD,120)<'1999-09'
--返回 2004-04-07 14:34:20.853select convert(varchar(10),getdate(),120)
--返回 2004-04-07select convert(varchar(7),getdate(),120)
--返回 2004-04
where year(YMD) + month(YMD) > '19999' and
year(YMD) + month(YMD) < '20004'
利用year,month函数获取拼接的字符串,然后与需要的值比较大小。
還可 datediff()函數進行月比較。
from
FM
where convert(varchar(7),YMD,120)>'1999-09' and convert(varchar(7),YMD,120)<'1999-09'
from
FM
where datediff(month,YMD,'1999/09/09')
from
FM
where datediff(month,YMD,'1999/09/09')=0
select ymd from fm where datediff(month,ymd,'1999/9/1')=0--得到1999年9月及以前的数据
select ymd from fm where datediff(month,ymd,'1999/9/1')>=0--得到1999年9月及以后的数据
select ymd from fm where datediff(month,ymd,'1999/9/1')<=0
returns int
as
begin
return year(@日期)*10000 + month(@日期)*100 + day(@日期)
endgoselect
YMD
from
FM
where
(select dbo.datetoint(YMD))>'19990909' and (select dbo.datetoint(YMD))<'19990909'