Get tomorrows date - sql

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.

Related

Proper way of getting rows since a date accounting for DST?

I have a datetime column, changedate, that I need to use to get rows that have changed in the last week since 1PM. This column is unfortunately in local time (EST). The server is Microsoft SQL Server 2016.
Here is the query I have now:
DECLARE #since datetime = DATEADD(week,-1,SMALLDATETIMEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), DAY(GETDATE()), 13, 0)) AT TIME ZONE 'Eastern Standard Time'
SELECT * FROM table WHERE changedate AT TIME ZONE 'Eastern Standard Time' >= #since
Since I'm using AT TIME ZONE for both the column and #since, will this properly account for DST changes? That's my understanding per the documentation I've found, but I'm not 100% sure if that's how it works or if I'm missing something.
First, figure out the time you're wanting to compare against:
-- Get the current date in the given time zone
DECLARE #today date = convert(date, sysdatetimeoffset() AT TIME ZONE 'Eastern Standard Time')
-- Get the date one week ago
DECLARE #dateOneWeekAgo date = DATEADD(week, -1, #today)
-- Join the date with the desired time (local to the same time zone)
DECLARE #since datetime = convert(datetime, #dateOneWeekAgo) + convert(datetime, timefromparts(1, 0, 0, 0, 0))
Then just compare it:
SELECT * FROM table WHERE changedate >= #since
That assumes your changedate field is a datetime or datetime2. If it's a datetimeoffset, you should first convert the target value to a datetimeoffset in the same time zone and use that instead:
DECLARE #sinceDTO datetimeoffset = #since AT TIME ZONE 'Eastern Standard Time'
Regarding the approach you gave in the question, there two issues:
getdate() gives the time based on the server's local time zone. It's possible that it's not the same day in Eastern Time.
You should never apply a function (whether an intrinsic like AT TIME ZONE or something else) against a table field in a where clause, because it makes the query non-sargable. In other words, SQL would have to scan the entire table, rather than using an index. The bigger the table, the slower the query would take.

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

Selecting all the data using time greater than 4pm

I need to select the Data containing time > 4pm in datatimestamp every day in SQL Server Management Studio Microsoft SQL Server 2005 - 9.00.4060.00 (X64) DB table which has two years of data. What's the best way to do this? My time stamp has following format:DATETIME, '2005-10-13 16:00:00', 102. I have data at random times every afternoon. I need to get the data after 4pm for every day. Not just for one day.
For example i tried for one day like this:
SELECT Yield, Date, ProductType, Direct
FROM MIAC_CCYX
WHERE (Date < CONVERT(DATETIME, '2005-10-13 16:00:00', 102)) Thanks for help
It's hard to read your question, but assuming you really are using a datetime data type, you can use datepart to find any dates with a time greater than 4 PM:
WHERE datepart(hh, YourDate) > 16
Since you now need minutes as well, if you want records after 4:45 PM, you can cast your date to a time like this:
SQL Server 2000/2005
SELECT Yield, [Date], ProductType, Direct
FROM MIAC_CCYX
WHERE cast(convert(char(8), [Date], 108) as datetime) > cast('16:45' as datetime)
Essentially you cast the date using convert's Date and Time styles to convert the date to a time string, then convert back to a datetime for comparison against your desired time.
SQL Server 2008+
SELECT Yield, [Date], ProductType, Direct
FROM MIAC_CCYX
WHERE CAST([Date] as time) > CAST('16:45' as time)
This will work whatever your date is. This will not compare the dates. Rather, Datepart() will extract the hour element from datetime and comapre with your given time (e.g. 4 P.M. in your case)
SELECT * from <table> where DATEPART(hh, ModifiedDate) >= 16
I am assuming ModifiedDate as column name. this will return data from 4 P.M. to 11:59:59 P.M
Edit: I have checked this against MS SQL server 2012. Also it will work with datetime format.

SQL Server datetime in query

In an event management database I have list of all events with a field startdate
What I want to do is not to show events passed by an hour. Only events which have not passed by current date and time.
My date and time is in this format 2011-08-24 17:30:00.000
Any kind of help is appreciated
I read that as you want data where startdate is before a specific date;
select
*
from T
where startdate < dateadd(hour, -1, '2011-08-24 17:30:00.000')
SELECT *
From EventTable
WHERE startdate > DATEADD(HOUR, -1, GETDATE())
GetDate() - Returns the current database system timestamp as a
datetime value.
DATEADD(datepart , number , date ) - Returns a specified date
with the specified number interval (signed integer) added to a
specified datepart of that date.