表里有一值为getdate()的DateTime数据类型字段ADDTIME, 现在需要查询该字段2006-4的数据这是我写的SQL:
select * from table where cast(addtime as varchar(50)) like '%2006-4%'
SQL语句正常,可数据取不到(确认是有数据的)又听别人说,CAST转换数据类型会有问题,要用FORMAT?
这个FORMAT怎么用?
select * from table where format(addtime,"YYYY-M") = '2006-4' ???但SQL提示这不是一个函数,用不了的
实在不理解,请帮一下
select * from table where cast(addtime as varchar(50)) like '%2006-4%'
SQL语句正常,可数据取不到(确认是有数据的)又听别人说,CAST转换数据类型会有问题,要用FORMAT?
这个FORMAT怎么用?
select * from table where format(addtime,"YYYY-M") = '2006-4' ???但SQL提示这不是一个函数,用不了的
实在不理解,请帮一下
--查询2006-04
select * from tablename
where datepart(year,shijianziduan)='2006'
and datepart(month,shijianziduan)='4'
where convert(nvarchar(7),addtime,111)='2006/04'
select cast(addtime as varchar(50)) as theDate from table
个人认为这个不错