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 >
Related
I want to get total counts from SuperScottTable1 from the previous day and narrow to a time frame (From-To). The below works fine until I add
AND (time > '08:00:00.000' AND time < '22:00:00.000')
It does not error, just returns null.
Is this possible to do?
SELECT
SUM(COALESCE(confirmedCount, 0))
FROM
SuperScottTable1
WHERE
superLaneID = '90099'
AND time >= GETDATE()-1
AND (time > '08:00:00.000' AND time < '22:00:00.000')
You can do some CAST/CONVERT shenanigans to generate a starting and stoping DATETIME value which you can then compare to your time column. If the time column is indexed then this will allow the server to do a simple range search on the index to find matches.
WHERE
superLaneID = '90099'
AND time > CAST(CONVERT(VARCHAR(20), GETDATE()-1, 112) + ' 08:00:00' AS DATETIME)
AND time < CAST(CONVERT(VARCHAR(20), GETDATE()-1, 112) + ' 22:00:00' AS DATETIME)
Presumably, time is stored as a datetime. You seem to be using SQL Server, so you can extract the hour and use that:
WHERE superLaneID = '90099' AND
time >= GETDATE()-1 AND
datepart(hour, time) >= 8 AND
datepart(hour, time) < 22
EDIT: To get the "other" hours:
WHERE superLaneID = '90099' AND
time >= GETDATE()-1 AND
(datepart(hour, time) < 8 or
datepart(hour, time) >= 22
)
GETDATE() in SQL Server returns the system date and time to arrive at the current day (i.e. date at time 00:00:00.0000000) you can use either of these:
cast(getdate() as date) -- available from SQL 2008 onward
dateadd(day, datediff(day,0, getdate() ), 0) -- any SQL Server version
Using these to establish the current date, then add (8-24) hours and or (22-24) hours to establish the boundaries of yesterday e.g.
WHERE superLaneID = '90099'
AND time >= dateadd(hour,(8-24),cast(getdate() as date))
AND time < dateadd(hour,(22-24),cast(getdate() as date))
Avoid using non-sargable predicates (such as using a function) that require you to alter each row of data to compare to a fixed value. ref this prior answer
Btw: This method discussed here would work for a time range that spans midnight.
I have SQL script that selects everything from current day.
SELECT [ClientID] from [logs] where Date > CONVERT (date, SYSDATETIME())
Date is type of DateTime.
How to get everything within last 3 days? I suppose I need subtract 3 days from function SYSDATETIME() result, but how?
SELECT [ClientID] from [logs] where Date > DATEADD(day, -3, CONVERT (date, SYSDATETIME()))
Use GETDATE() : Yes, it gets date from system!
Returns the current database system timestamp as a datetime value
without the database time zone offset. This value is derived from the
operating system of the computer on which the instance of SQL Server
is running.
Query:
SELECT [ClientID] from [logs] where ( Date > GETDATE() - 3)
More Reference:
GETDATE Detailed Documentation
For mysql use this:
SELECT DATE_ADD(CURRENT_DATE, INTERVAL - 3 DAY);
Use BETWEEN
SELECT ClientID
FROM logs
WHERE Date BETWEEN SYSDATETIME() AND SYSDATETIME() - 3
Using BETWEEN is nice. I also prefer the DATEADD function. But be aware of the fact that the SYSDATETIME function (or I would us GETDATE()) also includes the time which would mean that events before the current time but within the three day period may not be included. You may have to convert both sides to a date instead of datetime.
SELECT [ClientID] from [logs] where Date > DATEADD(day, -3, SYSDATETIME())
In my case:
select * from Table where row > now() - INTERVAL 3 day;
So you can fetch all of 3 days ago!
Im having trouble with this query. I want to not select records that have passed the SYSTIME for the current date but display records for future dates even if they have passed the current SYSTIME
SELECT *
FROM TABLE
WHERE DATE>= CONVERT(date, SYSDATETIME())
AND STARTTIME > CONVERT(time, SYSDATETIME())
This is the query. I know why it doesnt work but I can't think of a way to do what I stated above.
SELECT *
FROM TABLE
WHERE
(
(DATE = CONVERT(date, SYSDATETIME()
AND STARTTIME > CONVERT(time, SYSDATETIME()
)
OR Date > sysdatetime()
)
You need an or condition since date time are in different fields you must first resolve today's date and time and then all future dates regardless of time.
This depends a lot on what the original data types are. Let's assume they are date, time, or datetime.
If you want anything in the future, then this works:
where [date] + starttime > sysdatetime()
If you want just future dates then try:
where [date] > cast(sysdatetime() as date)
The latter is how I interpret your question, but I'm not 100% sure that my interpretation is correct.
I am trying to get tomorrows date in a sql statement for a date comparison but it is not working.
Below is my code:
select *
from tblcalendarentries
where convert(varchar,tblcalendarentries.[Start Time],101)
= convert(varchar, GETDATE() +1, 101)
To get tomorrows date you can use the below code that will add 1 day to the current system date:
SELECT DATEADD(day, 1, GETDATE())
GETDATE()
Returns the current database system timestamp as a datetime value without the database time zone offset. This value is derived from the operating system of the computer on which the instance of SQL Server is running.
DATEADD(datepart , number , date)
Returns a specified date with the specified number interval (signed integer) added to a specified datepart of that date.
So adding this to your code in the WHERE clause:
WHERE CONVERT(VARCHAR, tblcalendarentries.[Start Time], 101) =
CONVERT(VARCHAR, DATEADD(DAY, 1, GETDATE()), 101);
First off, GETDATE() will get you today's date in the following format:
2013-04-16 10:10:02.047
Then using DATEADD(), allows you to add (or subtract if required) a date or time interval from a specified date. So the interval could be: year, month, day, hour, minute etc.
Working with Timezones?
If you are working with systems that cross timezones, you may also want to consider using GETUTCDATE():
GETUTCDATE()
Returns the current database system timestamp as a datetime value. The database time zone offset is not included. This value represents the current UTC time (Coordinated Universal Time). This value is derived from the operating system of the computer on which the instance of SQL Server is running.
Try the below:
SELECT GETDATE() + 1
This adds one day to current date
Specify size of varchar in convert()
where convert(varchar(11),tblcalendarentries.[Start Time],101) = convert(varchar(11), GETDATE() +1, 101)
I would write:
where
DATEADD(day,DATEDIFF(day,0,tblcalendarentries.[Start Time]),0) =
DATEADD(day,DATEDIFF(day,0,GETDATE()),1)
This avoids converting dates to strings entirely, whilst also removing the time portion from both values.
Goal:
To display data with this list:
Hour
-----
2
2,5
1
Problem:
Is it any possibiltiy to convert column StartTime and EndTime into specific datatype and then doing a calculacution of X1- X2
CREATE TABLE Data
(
StartTime VARCHAR(5),
EndTime VARCHAR(5),
)
GO
INSERT INTO Data(StartTime,EndTime)
SELECT '10:00','12:00' UNION ALL
SELECT '13:30','16:00' UNION ALL
SELECT '14:00','15:00' UNION ALL
GO
EDITED: To get mins in decimals
SELECT ROUND(CAST( Mins AS FLOAT)/60,2) AS [hour]
FROM (
SELECT DATEDIFF(minute, CAST(StartTime AS TIME),CAST(EndTime AS TIME)) AS Mins
FROM Data
) A
SELECT DATEDIFF(mi, CONVERT(DATETIME, StartTime), CONVERT(DATETIME, EndTime))
/ 60.0 AS Hour
FROM Data
It work's for your example data, but you should check if EndTime could be the next day, like:
StarTime: 22:30
EndTime: 01:20
Is that escenario possible? If it is, you must store the date for both Start and End times, not only the hour.
If you are using SQL-SERVER 2008, then why not just use the time object. You could then run the appropriate time comparison functions. If not, you can still make it work by converting to a datetime and running comparison functions. I believe a convert with just a time to a datetime will result in the date as the minimum date.