如何在Like operaror SQL SERVER中使用子查询

编程入门 行业动态 更新时间:2024-10-15 10:17:10
本文介绍了如何在Like operaror SQL SERVER中使用子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

I want a query like this select CONVERT(VARCHAR(20),[date] , 120) from timesheetentry where [date] LIKE (SELECT convert(varchar(10), getdate(), 126))% i want to select the date where the date like in subquery can any help me to solve this

推荐答案

试试这个... Try this... SELECT CONVERT(VARCHAR,[date] , 120) DateColumn from timesheetentry WHERE CONVERT(VARCHAR,[date],126) LIKE ''+ CONVERT(VARCHAR, GETDATE(), 126))+'%'

快乐编码:)

Happy Coding :)

试试这个查询 Try this query declare @date varchar(10); select @date = convert(varchar(10), getdate(),126); select convert(varchar(20),[date] , 120) from timesheetentry where [date]>= @date+' 00:00:00.000' and [date]< @date+' 23:59:59:998'

以上解决方案正常工作,但我认为将日期值比较为字符串(varchar)不是一个好主意,因为它将限制只检查一个值(日期)并且不能在其他运算符之间使用。 /> 根据您的问题,您需要比较日期时间值的日期部分而不是时间部分,因此查询将是: Above solutions work properly, but I think comparing date values as string (varchar) is not a good idea as it will limit to check just one value(date) and cannot use between and other operators. According to your question you need to compare just the date part of the datetime value not the time part so the Query will be : select CONVERT(VARCHAR(20),[date] , 120) from timesheetentry where Convert(DateTime,Convert(Char(10),[date],120),120) = Convert(DateTime,Convert(Char(10),getdate(),120),120)

这样,时间部分将从Datetime值中排除,Datetime运算符(之间)也可以用于比较。

this way the time part will be excluded from the Datetime value and Datetime operators(between) can also be used for comparisons.

更多推荐

如何在Like operaror SQL SERVER中使用子查询

本文发布于:2023-10-24 20:21:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524913.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   operaror   SERVER   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!