怎么比较DateTime类型的中的年月部分???

时间:2022-03-01 04:46:08
只要年月相同 就找出这条记录,比如:
我界面上的日期是“2009-10-1 00:00:00”这种,但也能对应到数据库中的数据是这样的“2009-10-12 00:00:00”,也就是说我只要年、月相同,找就出这条记录
这个语句要怎么写呢?

16 个解决方案

#1


covert(char(7),date,121)

#2


select 
 *
from
 tb 
where 
 year(col1)=year(col2)
and
 month(col1)=month(col2)

#3


或者
select * from tb where covert(varchar(7),col1,120)=covert(varchar(7),col2,120)

#4


或者
select * from tb where datediff(mm,col1,col2)=0

#5


要这个样子拆开比吗?好像太复杂了吧 ,我是从一个表中查出的日期和另一个表中的日期比较,只要年月相同就匹配,如果像这样拆开是不是太麻烦了。

#6


select * from Table where convert(nvarchar(7),[date],120)=convert(nvarchar(7),@参数,120)

#7


引用 4 楼 fredrickhu 的回复:
SQL code或者select*from tbwheredatediff(mm,col1,col2)=0

Up!

#8


引用 3 楼 fredrickhu 的回复:
SQL code或者select*from tbwhere covert(varchar(7),col1,120)=covert(varchar(7),col2,120)

000

#9



select * from tb where DateDiff(M,[date],'2009-10-1')=0

这里将查询出所有的200910月份的数据

#10


select * from tb where  year(col1)=year(col2) and month(col1)=month(col2)

select * from tb where covert(varchar(7),col1,120)=covert(varchar(7),col2,120)

select * from tb where datediff(mm,col1,col2)=0
我给总结一下,呵呵

#11


引用 3 楼 fredrickhu 的回复:
SQL code或者select*from tbwhere covert(varchar(7),col1,120)=covert(varchar(7),col2,120)

#12


上面要 错了
引用 3 楼 fredrickhu 的回复:
SQL code或者select*from tbwhere covert(varchar(7),col1,120)=covert(varchar(7),col2,120)

#13


引用 4 楼 fredrickhu 的回复:
SQL code或者select*from tbwheredatediff(mm,col1,col2)=0


这个好

#14


select * from tb where datediff(mm,col1,col2)=0

这样子会不会只比较了月份  而没有比较年份呢?

#15


先 conver(varchar(10),col,120) 成"YYYY-MM-DD"格式的
再 substring(col,1,7) 取头7个字符比较

#16


引用 14 楼 shuihan16 的回复:
select * from tb where datediff(mm,col1,col2)=0

这样子会不会只比较了月份  而没有比较年份呢?


不会,因为DataDiff()这个函数返回的是两个日期的差值,返回值还是一个Datetime类型,只是改变了显示日期的形式,比如,'12/10/2009'-'12/10/2008' 若以'MM'显示,结果是12

#1


covert(char(7),date,121)

#2


select 
 *
from
 tb 
where 
 year(col1)=year(col2)
and
 month(col1)=month(col2)

#3


或者
select * from tb where covert(varchar(7),col1,120)=covert(varchar(7),col2,120)

#4


或者
select * from tb where datediff(mm,col1,col2)=0

#5


要这个样子拆开比吗?好像太复杂了吧 ,我是从一个表中查出的日期和另一个表中的日期比较,只要年月相同就匹配,如果像这样拆开是不是太麻烦了。

#6


select * from Table where convert(nvarchar(7),[date],120)=convert(nvarchar(7),@参数,120)

#7


引用 4 楼 fredrickhu 的回复:
SQL code或者select*from tbwheredatediff(mm,col1,col2)=0

Up!

#8


引用 3 楼 fredrickhu 的回复:
SQL code或者select*from tbwhere covert(varchar(7),col1,120)=covert(varchar(7),col2,120)

000

#9



select * from tb where DateDiff(M,[date],'2009-10-1')=0

这里将查询出所有的200910月份的数据

#10


select * from tb where  year(col1)=year(col2) and month(col1)=month(col2)

select * from tb where covert(varchar(7),col1,120)=covert(varchar(7),col2,120)

select * from tb where datediff(mm,col1,col2)=0
我给总结一下,呵呵

#11


引用 3 楼 fredrickhu 的回复:
SQL code或者select*from tbwhere covert(varchar(7),col1,120)=covert(varchar(7),col2,120)

#12


上面要 错了
引用 3 楼 fredrickhu 的回复:
SQL code或者select*from tbwhere covert(varchar(7),col1,120)=covert(varchar(7),col2,120)

#13


引用 4 楼 fredrickhu 的回复:
SQL code或者select*from tbwheredatediff(mm,col1,col2)=0


这个好

#14


select * from tb where datediff(mm,col1,col2)=0

这样子会不会只比较了月份  而没有比较年份呢?

#15


先 conver(varchar(10),col,120) 成"YYYY-MM-DD"格式的
再 substring(col,1,7) 取头7个字符比较

#16


引用 14 楼 shuihan16 的回复:
select * from tb where datediff(mm,col1,col2)=0

这样子会不会只比较了月份  而没有比较年份呢?


不会,因为DataDiff()这个函数返回的是两个日期的差值,返回值还是一个Datetime类型,只是改变了显示日期的形式,比如,'12/10/2009'-'12/10/2008' 若以'MM'显示,结果是12