In fact I have a table to store the details of calls and I need to filter the calls which entered the IVR after 16:00:00 till 06:59:00 the next day for ENTIRE MONTH I have used BETWEEN clause but it includes the details of all times of the month.
SELECT
[conversationId],
[conversationStart]
FROM
[Customer].[dbo].[vw_Conversations]
WHERE
conversationStart BETWEEN '2018-01-01 16:00:00.000' AND '2018-01-31 06:59:59.000'
Any help would be appreciated
Use DATEPART() function to test hour of the day. In your case, checking hours is enough:
SELECT conversationId,
conversationStart
FROM [Customer].[dbo].[vw_Conversations]
WHERE conversationStart BETWEEN '2018-01-01' AND '2018-02-01'
AND DATEPART(hour, conversationStart) NOT BETWEEN 7 AND 15
You need to just test the time component. I am not sure what date range you want, but the query is something like this:
SELECT conversationId, conversationStart
FROM Customer.dbo.[w_Conversations c
WHERE CONVERT(date, conversationStart) >= '2018-01-01' AND
CONVERT(date, conversationStart) < '2018-02-01' AND
(CONVERT(time, conversationStart) >= '16:00:00') OR
CONVERT(time, conversationStart) < '07:00:00')
);
Related
I have a small question about SQL Server: how to get the last 30 days information from this column from table1:
created_at
updated_at
2020-02-05T01:25:42Z
2020-02-05T01:25:42Z
2020-05-05T02:31:56Z
2020-05-05T02:31:56Z
With the above data, I would need something like day count within 30 days.
I have tried
SELECT * FROM table1
DATEDIFF(CAST(SUBSTR(updated_at,1,10)) AS VARCHAR,CURRENT_TIMESTAMP) BETWEEN 0 AND 30 ;
and
SELECT * FROM table1
WHERE updated_at BETWEEN DATETIME('now', '-30 days') AND DATETIME('now', 'localtime')
Would need your expertise to help me with this query
Thank you!
SELECT *
FROM (
SELECT otherColumns
, DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), updated_at) AS updated_at
FROM table1
) b
WHERE CAST(b.updated_at AS DATE) >= DATEADD(DAY,-30,GETDATE())
I think this will help you
If you want a count of updates by day for 30 (or so) days, then:
SELECT CONVERT(DATE, updated_at) as dte, COUNT(*)
FROM table1
WHERE updated_at >= DATEADD(DAY, -30, CONVERT(DATE, GETDATE()))
GROUP BY CONVERT(DATE, updated_at)
ORDER BY CONVERT(DATE, updated_at);
Note that SQLite date/time functions (which your code uses) are very peculiar to to SQLite. So are SQL Server's -- although I personally find them easier to remember.
I have a case compare date, hour of datetime column and current date,hour
select * from tbl where LEFT(EVENT_TIME_column,13) !=LEFT(GETDATE(),13)
EVENT_TIME_column format is '2019-08-15 12:32:40.0000000'
when i perform LEFT(GETDATE(),13) result is 'Aug 15 2019'
can you suggest how to get GETDate() in '2019-08-15 12' (date and hour)
If you want the format yyyy-MM-dd hh then can do this:
SELECT CONVERT(varchar(13),GETDATE(),120);
db<>fiddle
You can find a full list of all the style codes for CONVERT in the documentation: Date and Time Styles
However, it looks like you want to check if the date is within the current hour. That would be:
WHERE EVENT_TIME_column >= DATEADD(HOUR, DATEDIFF(HOUR, 0,GETDATE()),0)
AND EVENT_TIME_column < DATEADD(HOUR, DATEDIFF(HOUR, 0,GETDATE())+1, 0)
This explicitly avoids any functions on the column EVENT_TIME_column; which would make the query non-SARGable.
Don't use string functions on date/time values! There are perfectly good built-in functions:
where convert(date, event_time_column) = convert(date, getdate()) and
datepart(hour, event_time_column) = datepart(hour, getdate())
If you don't care about index usage, then use datediff():
where datediff(hour, event_time_column, getdate()) = 0
You can check this with 2 separate comparison as below. This is for checking Date and Hour part is same as date and hour part if GETDATE() or not.
WHERE CAST(EVENT_TIME_column AS DATE) = CAST(GETDATE() AS DATE)
AND DATEPART(HH,EVENT_TIME_column) = DATEPART(HH,GETDATE())
To check NOT EQUAL TO, Just replace = sign with != sign.
In addition, If I guess correct you are only trying to avoid records from running hour of to date. If this is the case, you can also filter your data with below logic-
WHERE EVENT_TIME_column < DATEADD(hh, DATEDIFF(hh, 0, getdate()), 0)
am working with MS SQL express and Ignition SCADA by http://www.inductiveautomation.com/
In the SCADA package you are able to create tags from SQL query's. I am trying to use SQL tags to calculate the average packages per minute in a 30min time frame. I was able to do this with two tags and an expression
SELECT MAX(L8Total)
FROM Slicing_tot
WHERE t_stamp BETWEEN DATEADD(minute, -30, GETDATE()) AND GETDATE()
SELECT MIN(L8Total)
FROM Slicing_tot
WHERE t_stamp BETWEEN DATEADD(minute, -30, GETDATE()) AND GETDATE()
What I would like to do from here is store the expressions value and find the max and average for the last 30 days based on time. But I have no idea how to filter 30days of information at a certain time
IE what was the max packages per minute we had at 10:30 from the last 30 days
IE what was the average packages per minute we had at 11:45 form the last 30 days
Please keep in mind that I am new to SQL
SELECT DATEPART(MINUTE, t_stamp)
,MAX(L8Total)
,MIN(L8Total)
FROM Slicing_tot
WHERE ( CONVERT(DATE, t_stamp) >= CONVERT(DATE, GETDATE() - 30)
AND CONVERT(DATE, t_stamp) <= CONVERT(DATE, GETDATE())
)
AND ( CONVERT(TIME, #variable) >= '22:30'
AND CONVERT(TIME, #variable) <= '23:00'
)
GROUP BY DATEPART(MINUTE, t_stamp)
GETDATE()-30 will get you datetime of today minus 30 days ago. Since you are working with datetime field it is best to convert it to date to make sure that you get correct date range. Use of >= and <= is better than between because you is always clear what you doing. Read #Aaron's blog
than for the second part just convert your datetime column to time to limit to specific range during the day.
The following would select between 10 & 11 AM over those 30 days
SELECT MIN(L8Total)
FROM Slicing_tot
WHERE t_stamp BETWEEN DATEADD(dd, -30, GETDATE()) AND GETDATE()
and Datepart(hh,t_stamp) between 10 and 11
or you could compare the time part of the t_stamp to time
SELECT MIN(L8Total)
FROM Slicing_tot
WHERE t_stamp BETWEEN DATEADD(dd, -30, GETDATE()) AND GETDATE()
and convert(time, t_stamp) between '10:30:00.000' and '10:31:00.000'
which would give you the results between 10:30 and 10:31 inclusive of the end points over the last 30 days.
I have two dates, From date and To Date.
Also i have two time fields, From Time and To Time.
The date field in the database is Datetime. I need to select data according to both date and time.
This is my query for selecting data between 13:00 to 15:00, but it is not suitable for 20:00 to 08:00.
where Date>= '2/01/2012' AND Date<'2/28/2013'
AND CAST(Date AS TIME) BETWEEN '20:00' AND '08:00'
Without seeing your specific error/unexpected results, I think the problem is that 20 is greater than 8.
You'll have to use two conditions:
where Date>= '2/01/2012' AND Date<'2/28/2013' AND (CAST(Date AS TIME) > '20:00' OR CAST(Date AS TIME) < '08:00')
EDIT: fixed condition
Is this what you are after?
WHERE Date BETWEEN '2012-01-01 20:00:00.000' AND '2012-12-01 08:00:00.000'
It is a little bit unclear whether you are attempting to generate the WHERE clause variables dynamically?
You need to combine your "date" and "time" parts together.
This code will illustrate how to do this:
SELECT the_date
, the_time
, DateAdd(hh, DatePart(hh, the_time), the_date) As hour_added
, DateAdd(mi, DatePart(mi, the_time), the_date) As minute_added
, DateAdd(mi, DatePart(mi, the_time), DateAdd(hh, DatePart(hh, the_time), the_date)) As both_added
FROM (
SELECT Cast('2013-02-28' As datetime) As the_date
, Cast('08:30' As datetime) As the_time
) As example
You can then use the resultant values in your comparison
I want to create a query like the following, But im unsure of how to code it correctly,
I want it to return all bookings within 1 hour of a StartTime, Here is what i came up with:
SELECT BookingId, StartTime
FROM Booking
WHERE StartTime <=> 1.00
Is the possible? or Is there a way round it?
Everything ive found on the web hasn't been about using Greater than, Equal to and Less Than all in the same query.
Supposing you use sql server:
WHERE StartTime BETWEEN DATEADD(HOUR, -1, GetDate())
AND DATEADD(HOUR, 1, GetDate())
If start time is a datetime type then you can use something like
SELECT BookingId, StartTime
FROM Booking
WHERE StartTime >= '2012-03-08 00:00:00.000'
AND StartTime <= '2012-03-08 01:00:00.000'
Obviously you would want to use your own values for the times but this should give you everything in that 1 hour period inclusive of both the upper and lower limit.
You can use the GETDATE() function to get todays current date.
declare #starttime datetime = '2012-03-07 22:58:00'
SELECT BookingId, StartTime
FROM Booking
WHERE ABS( DATEDIFF( minute, StartTime, #starttime ) ) <= 60
Somthing like this should workL
SELECT BookingId, StartTime
FROM Booking
WHERE StartTime between dateadd(hour, -1, getdate()) and getdate()