SQL Server datetime in query - sql-server-2005

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.

Related

Calculate future date based on date field

How I can calculate a date in the future based on the date of a field?
For example, I have a field in my dataset called SPS_START_DATE and I need to create a second field which is 20 weeks on from this date. What SQL would be required to calculate the date 20 weeks into the future?
Anyway, use DATEADD
SELECT
DATEADD(week, 20, [SPS_START_DATE])
FROM
[My DataSet];
DATEADD() is perfect for this.
For example:
SELECT GETDATE() AS DateTime, --Get the date/time from today
CAST(GETDATE() AS DATE) AS DateOnly, --Cast date/time value as date only
DATEADD(wk, 20, GETDATE()) AS TwentyWeeksDateTime, --Get the date/time of 20 weeks from now
CAST(DATEADD(wk, 20, GETDATE()) AS DATE) AS TwentyWeeksDate --Cast date/time value as date only

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!

Convert nvarchar date to datetime

I have dates stored in a sql server database as nvarchar but I need to create a report and pull out data from the last day base on the date.
I use this when the data type is a DateTime:
SELECT *
FROM [table]
WHERE timein >= DateAdd(hh, -24, GETDATE())
I think I need to convert the GETDATE() -24 to a string to compare it to the db
The format needs to be like this:
April-30-15
Can anyone help me create a query that will select records for the past 24 hours using this date format?
Convert your timeIn string to a date and compare using dates not strings. If you replace the hyphens with spaces it will be able to cast to a date. I assume you want values since the start of previous day (ignoring the current time) so I cast that to a date also.
SELECT *
FROM [table]
WHERE cast(replace(timein, '-', ' ') as date) >= cast(DateAdd(dd, -1, GETDATE()) as date)

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

Get tomorrows date

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.