I have a script where I need today date but from a year ago, however I also need it to be in the following format with the time.
2017-11-07 00:00:00.000
I currently have got to a place where I have last years date just not the 00:00:00.000 timestamp.
select DATEADD(year, -1, GETDATE())
This returns the time of the time the query was ran.
2017-11-07 13:37:10.770
This is for a where clause as I need to get some data from today's date from last year so looking for a where clause that starts at this date last year at midnight and finishes at 23:00:00.000
It's for a client running SQL Server 2005.
Try using:
CONVERT(DateTime, DATEDIFF(DAY, 0, DATEADD(year, -1, GETDATE())))
Here is a query to see this in action:
SELECT
GETDATE() AS today,
DATEADD(year, -1, GETDATE()) AS today_last_year, -- what you already have
CONVERT(DateTime, DATEDIFF(DAY, 0, DATEADD(year, -1, GETDATE()))) AS
today_last_year_midnight;
This returned (as of the time of writing this answer):
Related
I am writing a SQL query in Aginity through Amazon Redshift to extract the last 7 days of data. The Date column which I am calling is in the variable type:
DATE
An example output is this:
5/30/2017 0:00
When I call the below function, does it matter what time of day I run this query or will it always take the full day's worth of data?
WHERE Date >= DATEADD(day,-7, GETDATE())
Yes, this includes the current time component when subtracting 7 days. To get rid of that, convert to a date:
WHERE Date >= CAST(DATEADD(day, -7, GETDATE()) as DATE)
I have a query to pull data from the last 30 days and I am using this function
DATEADD(day, -30, GETDATE())
I am getting the data but it looks like it is missing some Like when I look at the data for specific date it doesn't include all the 24 hours data I am missing some. I need the data to include the 24 hours data for every day for the last 30 days.
select
*
from
Travel R
where
R.s9_date > CONVERT(date, DATEADD(day, -30, GETDATE()))
order by
R.s9_date
Thanks
GETDATE() returns a datetime which includes the hours, minutes, seconds, etc. Therefore when you subtract 30 days from that, the subtracted value will also have the time part.
Assuming you're using SQL Server 2008 or above, you can convert the datetime to a date by wrapping the DATEADD function with a CONVERT function:
CONVERT(date, DATEADD(day, -30, GETDATE()))
I believe CAST was introduced in SQL Server 2005
select
*
from
Travel R
where
R.s9_date > CAST(DATEADD(DAY, -30, GETDATE()) AS DATE)
order by
R.s9_date
I have a Tasks table that contains Date, Description and WorkerID columns. I want to write a stored procedure that selects all tasks from this week and the following week. Can someone help me get to a solution please? I am not so good at this.
If indexing (performance) isn't a concern then it's a simple expression if your week coincides with the SQL Server setup:
where datediff(week, getdate(), [Date]) between 0 and 1
The snippet below is another option that works with both dates and datetimes. It's possible to adapt it to work with any ##datefirst setting but I'm just going to assume that the setting matches with the start of week you're looking to report on.
where
[Date] >= dateadd(day, 1 - datepart(weekday, getdate()), cast(getdate() as date))
and [Date] < dateadd(day, 15 - datepart(weekday, getdate()), cast(getdate() as date))
The following should give you what you want (assuming Monday is the first day of the week, and that the DBMS is SQL Server):-
select *
from Tasks
where [Date]
between
dateadd(dd,-(datepart(dw,[Date])-2),[Date]) --Monday of this week
and
dateadd(dd,13,dateadd(dd,-(datepart(dw,[Date])-2),[Date])) --Sunday of next week
I have the following sql server WHERE clause:
WHERE (DateCreated >= CONVERT(datetime, GETDATE(), 111) - 1)
This gets the date (where today is 2015-06-09) 2015-06-08. I need to add a time to this as well like 2015-06-08 04:00:00 in 24H format. the time will always be the same bat every time the SQL command is executed, it should only be from yesterday at 4 AM to the current date and time.
how can this be achieved?
Try this:
WHERE DateCreated >= dateadd(d, datediff(d, 1, getdate()), '04:00')
I think you are looking for:
WHERE (DateCreated >= DATEADD(HOUR, 4,
CONVERT(datetime,
DATEADD(DAY, -1, CONVERT(date, GETDATE()) )
)
)
)
Converting directly to DATE will take away the hassle of taking care of the hour part. After that, doing a DATEADD with -1 will take you 1 day ago.
After this step, simply convert it back to datetime to create a timestamp part to your date, which is defaulted to 00:00:00.000.
And in the end, simply add 4 hours to this start date, which will always give you 4:00 AM.
How to find the date of the first day of the current week in transact-sql(t-sql)?. What T-SQL statement do we need to use for this?.
This is always be a Sunday. For example if we run the sql statement on today(2015-02-18) or any day of the current week it should give the result 2015-02-15 which is a Sunday.
One of possible way to get the date on Sunday for the current week.
SELECT DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), -1) AS SundayOfCurrentWeek
Using DATEPART
It depends on the settings of the server, but if it is configured to think that weeks start on Sundays, then this would work:
SELECT CAST(DATEADD(day, 1-DATEPART(weekday, GETDATE()), GETDATE()) AS date)
DATEPART with weekday parameter gives the day of the week. Then this number is subtracted from the current date and converted to date to remove the time part.
When datepart is week (wk, ww) or weekday (dw), the return value
depends on the value that is set by using SET DATEFIRST.
To see the current setting of SET DATEFIRST, use the ##DATEFIRST
function. The setting of SET DATEFIRST is set at execute or run time
and not at parse time.
So to play it safe make sure that DATEFIRST is set to 7 (default, U.S. English), which means Sunday.
Using DATEDIFF
Another variant uses DATEDIFF:
SELECT DATEADD(wk, DATEDIFF(wk, '20150104', GETDATE()), '20150104')
It would always return Sunday. Magic date 20150104 can be any date that is Sunday.
Specifying SET DATEFIRST has no effect on DATEDIFF. DATEDIFF always
uses Sunday as the first day of the week to ensure the function is
deterministic.
On the one hand it is good, because it is guaranteed to return Sunday, even if you forget to set DATEFIRST. But, on the other hand it is not flexible and doesn't suit those who consider, say, Monday to be the first day of the week (those that are not in US).
use this
Select dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) as LastWeekStart
Select dateadd(wk, datediff(wk, 0, getdate()), 0) as ThisWeekStart
Select dateadd(wk, datediff(wk, 0, getdate()) + 1, 0) as NextWeekStart
for more information
GETDATE last month