I'm trying to write an automation code for which I want to use the current date but the time must be specific.
SELECT
col_name
FROM
table
WHERE
CONVERT(DATE, start_datetime) = CONVERT(date, GETUTCDATE())
How do I mention specific time?
I'm trying to write an automation code for which I want to use the current date but the time must be specific.
SELECT
col_name
FROM
table
WHERE
CONVERT(DATE, start_datetime) = CONVERT(date, GETUTCDATE())
How do I mention specific time?
wow took me a minute I wasn't understanding your question. but I think you are asking how do you specify a time today and compare that to your start_datetime value to see if they are at the same time.
The question will come down to how accurate do you want to be. e.g. at a specific hour? within x # of minutes? Seconds....????
And there are tons of ways of answering this.
The first question is what timezone is your start_datetime stored in? Because you likely do not want to use UTC Date if your start_date is not also in UTC! If not utc what timezone is your server set to, would GETDATE() and start_datetime be in the same zone?
Anytime Today in a specific hour.
SELECT
col_name
FROM
table
WHERE
CONVERT(DATE, start_datetime) = CONVERT(date, GETUTCDATE())
AND HOUR(start_datetime) = 5
Anytime Today at specific hour and minute
SELECT
col_name
FROM
table
WHERE
CONVERT(DATE, start_datetime) = CONVERT(date, GETUTCDATE())
AND DATEPRT(HOUR,start_datetime) = 5
AND DATEPART(MINUTE,start_datetime) = 15
OR
SELECT
col_name
FROM
table
WHERE
start_datetime = CAST(CAST(GETUTCDATE() AS DATE) AS DATETIME) + CAST('05:15' AS DATETIME)
OR
SELECT
col_name
FROM
table
WHERE
start_datetime = CONVERT(DATETIME,(CONVERT(VARCHAR(10),GETUTCDATE(),120) + ' 19:15'))
For seconds you can use basically the same technique as the hour/minute only add an either seconds to the string you are converting or
For after a specific time just switch the = to >