문제

I want to query a subset from a dataset. Each row has a time stamp of the following format:

2014-04-25T17:25:14
2014-04-25T18:40:16
2014-04-25T18:44:57
2014-04-25T19:10:32
2014-04-25T20:22:12
...

Currently, I use the following query to select a time-based subset:

time LIKE '%2014-04-25T18%' OR time LIKE '%2014-04-25T19%'

This becomes quite complicated when you start to filter by mintutes or seconds.

Is there a way to run a query that such as ...

time > '%2014-04-25T18%' AND time < '%2014-04-25T19%'

A regular expression would be okay, too.

The database is a SpatiaLite database. The time column is of type VARCHAR.

도움이 되었습니까?

해결책 2

Thanks to your posts and this answer I came up with this solution:

SELECT * FROM data 
WHERE DATETIME(
  substr(time,1,4)||'-'||
  substr(time,6,2)||'-'||
  substr(time,9,2)||' '||
  substr(time,12,8)
) 
BETWEEN DATETIME('2014-04-25 18:00:00') AND DATETIME('2014-04-25 19:00:00');

다른 팁

If the date is being treated as a string and based on the example above:

time LIKE '%2014-04-25T18%' AND time <> '%2014-04-25T18:00:00:000'

Otherwise, you could convert the date to seconds since midnight and add 60 minutes to that to create the range part of the filter

DECLARE @test DATETIME = '2014-04-25T17:25:14'
SELECT @test
, CONVERT(DATE,@test) AS JustDate
, DATEDIFF(s,CONVERT(DATETIME,(CONVERT(DATE,@test))), @test) AS SecondsSinceMidnight

-- 60 seconds * 60 minutes * 24 hours = 86400 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top