SQL - Query condition for future date time - sql

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.

Related

SQL date comparision in yyyy/mm/dd

Not being that great at sql, i've reached my limit.
I have a date in the yyyy/mm/dd format and i need to get all records "from a week ago"
I think i need some conversion stuff to be done cause this
d.date_begin >= DATEADD(day,-7, GETDATE())
is not working :), i'm TERRIBLE at convert and data type..
This will work if you want records from 7 days ago up to and including today's records
CAST(d.date_begin AS DATE) >= CAST(DATEADD(day,-7, GETDATE()) AS DATE)
where DATEDIFF(month,Your_date_column,getdate()) < 3
SQL server 2012 onwards, if date_begin is of datatype date
where d.date_begin >= cast(DATEADD(day,-7, GETDATE()) as date)
This will get anything in the last 7 days, regardless of time
You should store date/time values using native formats. Ok, sometimes we cannot. But you can easily convert your values to the correct format:
where cast(replace(d.date_begin, '/', '') as date) >= DATEADD(day, -7, GETDATE())
I should note that SQL Server is pretty good about conversions, so your initial code should not generate any errors -- unless you have unusual internationalization settings.
Or, actually, a better way to do this is to convert the current value to a string:
where d.date_begin >= format(dateadd(day, -7, getdate()), 'yyyy/MM/dd')
This is better because it is "sargable", meaning that SQL Server can use an index on the column if available.
SELECT * FROM tbl_name
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
Don't manipulate d.date_begin. Making calculation on a column while comparing can give bad performance. You should manipulate getdate() to get the same format as d.date_begin. In this case it works because the format is yyyy/MM/dd - the comparison will give the same result as if both columns were date columns.
WHERE
d.date_begin >= convert(char(10),DATEADD(day,-7, GETDATE()), 111)

GETUTCDATE but specify custom 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 >

Get date of 3 days ago

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!

How to compare current time to retrieve record stored in SQL

I have a database in which one of the column name is 'date' I need to count the number of entries that have been added with today's date after 09.00.00
So far I have done this..I am wondering where do I specify the time '09.00.00'
SELECT COUNT(*) AS total from DATABASENAME
WHERE date >= Convert(datetime, Convert(int, GetDate()))
Here's code that should work. You're essentially converting a datetime to a date to remove the time, then back to a datetime and adding in the hour that you want.
SELECT COUNT(*) AS Total
FROM TableName
WHERE Date >= DATEADD(HOUR, 9, CONVERT(DATETIME, CONVERT(DATE, GETDATE())))
Well is the date column store date and time or date only, if it is date only you need to add the time also in the column and also avoid converting data type in where condition try the above query with a parameter variable

Easier way to compare a DATE field to the date value of GETDATE?

I have a field in a table Event.EventDate and it's of the data type DATE rather than DATETIME and then I have a view that has the following WHERE clause:
WHERE e.EventDate >= CAST(CONVERT(VARCHAR(MAX), GETDATE(), 101) AS DATETIME)
As you can see, I'm just trying to get all events >= today's date. The above code works, but it's ugly. I tried this ...
WHERE e.EventDate >= CONVERT(VARCHAR(MAX), GETDATE(), 101)
... and this ...
WHERE e.EventDate >= CONVERT(DATETIME, GETDATE(), 101)
... but those didn't work, they gave me every event > today's date. However, even if the above worked, it's still ugly.
Isn't there a better way?
Try:
WHERE e.EventDate >= cast(getdate() as date)
To cast getdate() into a date time. It's a clean way in SQL Server 2008 and up to strip out the time portion of a datetime type.
Using Shan Plourde's method is certainly cleaner and quicker, but for the more general case when you want to round a datetime to a particular time interval, I use
dateadd(dd,datediff(dd,0,[datetime column]),0)
where dd stands for day, and can be replaced with mm (month), hh (hour), mi (minute), and probably others.
If you want to get fancy and round to, say, 15 minute intervals, you can use
dateadd(mi,
-datepart(mi,[datetime column])%15,
dateadd(mi,datediff(mi,0,[datetime column]),0)
)
where % is the modulo operator. You'll get wacky results for this if you don't use an interval that divides 60 evenly.