For best performance potential avoid using functions on the data. This maximizes the opportunity to leverage from an index on the created column, and also saves on performing function calls on every row of data.
SELECT *
FROM Table1
WHERE created >= dateadd(hour,-145,dateadd(dd, datediff(dd,0, getDate()), 0))
AND created < dateadd(hour,-137,dateadd(dd, datediff(dd,0, getDate()), 0))
You can choose which method you prefer, both sets below work by "truncating" getdate() setting the time to 00:00:00+0000000. Then deduct (-724)+23 and (-724)+23+8 (-145 and -137).
SELECT
dateadd(hour,-145,dateadd(dd, datediff(dd,0, getDate()), 0))
, dateadd(hour,-137,dateadd(dd, datediff(dd,0, getDate()), 0))
, dateadd(hour,-145,convert(datetime,convert(date,getdate())))
, dateadd(hour,-137,convert(datetime,convert(date,getdate())))
If necessary add or subtract 24 from those numbers to find the time period you need.
Oh, and please note that "between" is NOT a good thing for date/time ranges. Do NOT use "between" here. For more on this topic read Bad habits to kick : mis-handling date / range queries
CREATE TABLE Table1
([COD] int, [CREATED] datetime2)
;
INSERT INTO Table1
([COD], [CREATED])
VALUES
(6001, '2017-11-22 22:03:23.5007015 -02:00')
, (6002, '2017-11-22 23:03:24.3593854 -02:00')
, (6003, '2017-11-23 06:03:24.7344270 -02:00')
, (6004, '2017-11-23 13:03:25.1680686 -02:00')
, (6005, '2017-11-23 23:56:00.6874197 -02:00')
, (6006, '2017-11-24 05:33:58.4243013 -02:00')
, (6007, '2017-11-24 22:40:08.3255570 -02:00')
, (6008, '2017-11-24 23:42:50.9885384 -02:00')
;
update table1
set created = dateadd(day,-7,created)
;
Results:
| COD | CREATED |
|------|-----------------------------|
| 6001 | 2017-11-15 22:03:23.5007015 |
| 6002 | 2017-11-15 23:03:24.3593854 |
| 6003 | 2017-11-16 06:03:24.7344270 |
| 6004 | 2017-11-16 13:03:25.1680686 |
| 6005 | 2017-11-16 23:56:00.6874197 |
| 6006 | 2017-11-17 05:33:58.4243013 |
| 6007 | 2017-11-17 22:40:08.3255570 |
| 6008 | 2017-11-17 23:42:50.9885384 |
Query
SELECT *
FROM Table1
WHERE created >= dateadd(hour,-145,dateadd(dd, datediff(dd,0, getDate()), 0))
AND created < dateadd(hour,-137,dateadd(dd, datediff(dd,0, getDate()), 0))
;
Results
| COD | CREATED |
|------|-----------------------------|
| 6005 | 2017-11-16 23:56:00.6874197 |
| 6006 | 2017-11-17 05:33:58.4243013 |
Demo
-02:00
concatenated at the end. – Zorkolot Nov 23 '17 at 21:59