比如以日期段 记录的只有日期 1 2010-12-20
2 2004-05-18
3 2007-02-27
.............,SQL语句 怎么判断不同年的 同月同天?
例如今天 2010 12 20 日 忽略年份 要日期是 12-20的
2 2004-05-18
3 2007-02-27
.............,SQL语句 怎么判断不同年的 同月同天?
例如今天 2010 12 20 日 忽略年份 要日期是 12-20的
drop table tb
Go
Create table tb([ID] int,[date] Datetime)
Insert tb
select 1,'2010-12-20' union all
select 2,'2004-05-18' union all
select 3,'2007-02-27'
Go
select right(convert(varchar(10),[date],120),5)[date]
from tb
/*
date
----------
12-20
05-18
02-27(3 行受影响)
*/
from tb
where datepart(month,[date])=datepart(month,getdate())
and datepart(day,[date])=datepart(day,getdate())
--or
select *
from tb
where right(convert(varchar(10),[date],120),5)=right(convert(varchar(10),getdate(),120),5)
[日期]是记录日期的那个列 后面那个对比的时间 我程序自己提供
select * from 用户 where right(convert(varchar(10),[日期],120),5)=right(convert(varchar(10),2010-11-21,120),5)
只有formatdatetime函数
--date()表示当前日期,相当于sql中的getdate()
where formatdatetime(date1,'mm-dd')= formatdatetime(date2,'mm-dd')
或者 month(date1)=month(date2) and day(date1)=day(date2)
--3
datepart(month,[date1])=datepart(month,date2)
and datepart(day,[date1])=datepart(day,date2)
from 用户
where Format([日期], "mm-dd") =Format('2010-11-21',"mm-dd")