SQL SERVER'喜欢'datetime coloumn的运算符 [英] SQL SERVER 'like' operator for datetime coloumn
本文介绍了SQL SERVER'喜欢'datetime coloumn的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在SQL Server中的DateTime数据类型列上执行LIKE语句?我有一个SQL查询像这样:
How do you perform a LIKE statement on a column of DateTime datatype in SQL Server? I had a SQL Query Like this:
select CustomerID from Orders where OrderDate LIKE '1996-07- %'
这对我没有任何回报!
this returns me nothing!
推荐答案
您可以使用 DATEPART [ ^ ]提取部分日期的功能。试试这样:
Instead you can use the DATEPART[^] function to extract portions of dates. Try like this:
SELECT * FROM TableName where DATEPART(yy, YourDateColumn) = 1996 --For Year
SELECT * FROM TableName where DATEPART(mm, YourDateColumn) = 4--For Months
SELECT * FROM TableName where DATEPART(dd, YourDateColumn) = 4--For day
--Or, For all togather
SELECT * FROM TableName where (DATEPART(yy, YourDateColumn) = 1996 AND DATEPART(mm, YourDateColumn) = 4 AND DATEPART(dd, YourDateColumn) = 4)
类似运算符不能与DateTime一起使用。请参阅此 [< a href =http://www.varindersandhu.in/2013/02/12/sql-server-like-operator-does-not-work-with-datetime/target =_ blanktitle =New Window > ^ ]。
select CustomerID from Orders
where convert(nvarchar(50), OrderDate,126) LIKE '1996-07- %'
试试这个, ,,,, :)
Try this,,,,,:)
SELECT * FROM TableName WHERE CONVERT(VARCHAR, DateTimeColumn , 120) LIKE '2013-06%'
问候,
Nirav Prabtani。
Regards,
Nirav Prabtani.
这篇关于SQL SERVER'喜欢'datetime coloumn的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文