在SQL QUERY中使用介于日期之间的日期 [英] Using a between Date in SQL QUERY
问题描述
我在Sql Server 2008 R2中使用vb.net代码.
我正在尝试通过使用SQL查询来获取所有具有2个日期之间的值的行的结果:这是我的声明中的 where
子句:
Im using vb.net code with Sql Server 2008 R2.
I'm trying to get result by using a SQL query to get all rows that have a value between 2 dates:
Here is my the where
clause of my statement:
Where (CONVERT(varchar(10), visit_nextVisitDate, 103) between '02/04/2017' AND '15/05/2017')"
但是我总是得到同一个月(第4个月)的所有行.我试过了:
but I always get all rows for the same month (month 4). I tried this:
位置(CAST(dbo.Visits.visit_date AS日期)在"24/04/2017"和"02/05/2017"之间)
但是出现错误,因为我的日期字段以 yyyy/mm/dd
but I got an error cause my date fields are saved in format yyyy/mm/dd
如何将SQL日期格式更改为 dd/mm/yyyy
?
How can I change the SQL date format to dd/mm/yyyy
?
推荐答案
为什么要使用字符串进行日期比较?那是错的,错的,错的.(如果这样做,请使用ANSI标准格式YYYY-MM-DD,以便进行正确的比较.)
Why would you do date comparisons using strings? That is just wrong, wrong, wrong. (If you do it, use ANSI standard formats, YYYY-MM-DD so the comparisons are correct.)
只需使用日期即可:
Where visit_nextVisitDate between '2017-04-02' AND '2017-05-02'
实际上,在日期之间使用之间</code>是个坏主意.亚伦·伯特兰德(Aaron Bertrand)有一个很好的关于该主题的博客.
Actually, it is a bad idea to use between
with dates. Aaron Bertrand has a very good blog on this subject.
我推荐:
Where visit_nextVisitDate >= '2017-04-02' AND
visit_nextVisitDate < '2017-05-03'
这篇关于在SQL QUERY中使用介于日期之间的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!