Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

sql - How to query between Date Range

I am querying a table with a time column returned in the format below:

2020-12-30 18:26:23.537
2020-12-30 17:43:19.707
2020-12-30 15:36:13.653
2020-12-30 15:35:23.160
2020-12-30 15:23:33.063
2020-12-30 15:22:42.243
2020-12-30 15:18:26.230
2020-12-30 15:15:20.083
2020-12-30 15:13:08.813

When I query what format this column is it shows as datetime. Update: Using this query it returns date time: SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'vStatusMessagesWithStrings' AND COLUMN_NAME = 'TIme'

I have tried to narrow down results through a where statement in many ways but every time I run it I just get what looks like all the dates returned, all the way back to July.

I have tried just using >= and <= and I have also tried use "Between" but nothing works. I have tried with just the string as 20201228 as 2020-12-28 and as 2020/12/30 but does not help. I read that when using between you need to specify the time as well so I have tried it down to the millisecond but still returns everything. I have tried cast and convert on the date strings and nothing works.

here are a few examples of the different formats I have tried in my where statement:

and (sms.[Time] <= (convert(date,'20201228')) and SMS.[TIME] >= (convert(date,'20201222')))
and (sms.[Time] >= '20201222' and  SMS.[Time] <= '20201228')
and ((sms.[Time] >= Cast('20201222' as DateTime)) and  (SMS.[Time] <= Cast('20201228' as DateTime)))
and (sms.[Time] Between Cast('20201222 00:00:00.000' as DateTime) and  Cast('20201228 00:00:00.000' as DateTime))
and (sms.[Time] Between Cast('2020-12-22 00:00:00.000' as DateTime) and  Cast('2020-12-28 00:00:00.000' as DateTime))
and ((sms.[Time] >= Cast('2020-12-22 00:00:00.000' as DateTime)) and sms.[Time] <= Cast('2020-12-28 00:00:00.000' as DateTime))
and ((sms.[Time] >= '2020-12-22' ) and (sms.[Time] <= '2020-12-28'))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There must be something else going on because every example you say you tried works for me e.g.

declare @Test table ([time] datetime);

insert into @Test ([Time])
values
('2020-12-21T18:26:23.537'),
('2020-12-22T17:43:19.707'),
('2020-12-23T15:36:13.653'),
('2020-12-24T15:35:23.160'),
('2020-12-25T15:23:33.063'),
('2020-12-26T15:22:42.243'),
('2020-12-27T15:18:26.230'),
('2020-12-28T15:15:20.083'),
('2020-12-29T15:13:08.813');

select *
from @Test sms
where sms.[Time] between cast('2020-12-22T00:00:00.000' as datetime) and cast('2020-12-28T00:00:00.000' as datetime);

Returns:

time
2020-12-22 17:43:19.707
2020-12-23 15:36:13.653
2020-12-24 15:35:23.160
2020-12-25 15:23:33.063
2020-12-26 15:22:42.243
2020-12-27 15:18:26.230

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...