[英]Using LIKE in ms sql server management studio
Question: When I use the LIKE in my query it is gray and my query does not return anything when I know if should. 问题:当我在查询中使用LIKE时,它是灰色的,并且在我知道是否应该查询时不返回任何内容。
I am not sure if there is a configuration setting I am missing but normally when I use MS sql server management studio terms like: UPDATE WHERE SELECT ADD all show as blue in my query window. 我不确定是否缺少配置设置,但通常在使用MS sql服务器管理工作室之类的术语时:UPDATE WHERE SELECT ADD在我的查询窗口中都显示为蓝色。
Example query: 查询示例:
SELECT *
FROM [MainSiteDB].[dbo].[usr_user]
WHERE [MainSiteDB].[dbo].[usr_user].[usr_lastLogin] LIKE '2014-11-10%'
In my table in that field there are many entries like: 2014-11-10 13:19:46.967
在我该字段的表格中,有很多条目,例如:
2014-11-10 13:19:46.967
Like is not applicable for datetime or date field for SQL server.
Like不适用于SQL Server的日期时间或日期字段。 'Like' is for varchar/char/text and other text related field
“喜欢”用于varchar / char / text和其他与文本相关的字段
You can use between or <> sign 您可以在或<>之间使用
Select * from tblTable where Date between '2014 Jan 01' and '2014 Jan 31'
OR 要么
Select * from tblTable where Date >= '2014 Jan 01' and Date <= '2014 Jan 31'
You can find in menu in Management studio menu under Tools->Option then Fonts and color.
您可以在Management Studio菜单的“工具”->“选项”下找到“字体和颜色”。 Then you can change the customize color for SQL keywords.
然后,您可以更改SQL关键字的自定义颜色。
you Can Use it : 您可以使用它:
SELECT * FROM [MainSiteDB].[dbo].[usr_user] WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, [MainSiteDB].[dbo].[usr_user].[usr_lastLogin]))= '2014-05-9' SELECT * FROM [MainSiteDB]。[dbo]。[usr_user] WHERE DATEADD(dd,0,DATEDIFF(dd,0,[MainSiteDB]。[dbo]。[usr_user]。[usr_lastLogin]))='2014-05- 9'
Try this query. 试试这个查询。 Using LIKE directly in DateTime filed is won't give expected result.
直接在DateTime字段中使用LIKE不会产生预期的结果。 Convert the DateTime field to specific format to Varchar and use LIKE .
将DateTime字段转换为Varchar的特定格式 ,然后使用LIKE 。
101 - Format the DateTime to mm/dd/yyy 101-将DateTime格式化为mm / dd / yyy
SELECT *
FROM [MainSiteDB].[dbo].[usr_user]
WHERE CONVERT(VARCHAR(20), [MainSiteDB].[dbo].[usr_user].[usr_lastLogin], 101)
LIKE '05/22/2014%'
105 - Format the DateTime to mm-dd-yyy 105-将DateTime格式化为mm-dd-yyy
SELECT *
FROM [MainSiteDB].[dbo].[usr_user]
WHERE CONVERT(VARCHAR(20), [MainSiteDB].[dbo].[usr_user].[usr_lastLogin], 105)
LIKE '05-22-2014%'
You can cast the date part and then use the LIKE operator. 您可以转换日期部分,然后使用LIKE运算符。 Check the below script:
检查以下脚本:
Select * from TableName Where colName like Cast('2014-09-25' as datetime)
select * from tablename
where CONVERT(varchar(10), getdate(), 105) = @inputdate
Below convert will fetch the datepart alone from your columnname, and then based on your input it will fetch all matching records. 在convert下方,将仅从列名中获取日期部分,然后根据您的输入将获取所有匹配的记录。
SELECT CONVERT(varchar(10), getdate(), 105) --> 11-11-2014
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.