I'm trying to achieve the last possible time of a particular day eg for Date of 2008-01-23 00:00:00.000 i would need 2008-01-23 23:59:59.999 perhaps by using the dateadd function on the Date field?
The answer is SELECT DATEADD(ms, -3, '2008-01-24'), the explanation is below.
From Marc's blog:
But wait, Marc... you said you like to use BETWEEN, but that query doesn't have one... that's because BETWEEN is inclusive, meaning it includes the end-points. If I had an Order that was due at midnight of the first day of the next month it would be included. So how do you get the appropriate value for an end-of-period? It's most certainly NOT by using date-parts to assemble one (but is you must, please remember that it's 23:59:59.997 as a maximum time... don't forget the milliseconds). To do it right, we use the incestuous knowledge that Microsoft SQL Server DATETIME columns have at most a 3 millisecond resolution (something that is not going to change). So all we do is subtract 3 milliseconds from any of those end-of-period formulas given above. For example, the last possible instant of yesterday (local time) is:
SELECT DATEADD(ms, -3, DATEADD(dd, DATEDIFF(dd, 0, GetDate()), 0))
So to do the orders due this month as a BETWEEN query, you can use this:
SELECT [ID]
FROM [dbo].[Orders]
WHERE [ShipDue] BETWEEN DATEADD(mm, DATEDIFF(mm, 0, GetUTCDate()), 0)
AND DATEADD(ms, -3, DATEADD(mm, DATEDIFF(mm, 0, GetUTCDate()) + 1, 0))
Remember, always make sure that you do math against input parameters, NOT columns, or you will kill the SARG-ability of the query, which means indexes that might have been used aren't.
SELECT DATEADD(ms, -2, DATEADD(dd, 1, DATEDIFF(dd, 0, GetDate())))
I thought you had c# at first.. I will leave this here in case anyone else stumbles across this.
DateTime now = DateTime.Now;
DateTime endofDay = now.Date.AddDays(1).AddMilliseconds(-1);
You can replace the 'now' variable with whatever day you are trying to figure out
Add -1 milliseconds to the start of the next day (DateAdd even supports nanoseconds, if you want to get real fine).
But most likely you just want to use this value in a comparison, and in that case it's even simpler.
Rather than something like this:
AND #CompareDate <= [LastTimeforThatday]
or this:
#compareDate BETWEEN [StartDate] AND [LastTimeforThatday]
Do it like this:
AND #CompareDate < [BeginningOfNextDay]
or this:
AND (#CompareDate >= [StartDate] AND #CompareDate < [BeginningOfNextDay])
Why back into it?
SELECT DATEADD(ms, 86399997, *yourDate*)
I was able to use:
select {fn curdate()} + ' 23:59:59.000'
or
select DATEADD(ss,-1,DATEADD(DAY,1,CAST({fn curdate()} as DATETIME)))
to get the end of a day
Related
How can I show records that are due to expire in two months?
I have tried using DATEADD but I appear to have the logic code incorrect.
GETDATE() >= DATEADD(MONTH,-2, cycles.[NEXT-DATE])
Any code on how I can do this?
Thanks,
Your code gives you records that have expiry date equal 2 months or more. You should change your condition. Draw a time line to visualize it (it's easier then to understand how it should look like).
GETDATE() >= DATEADD(MONTH,-2, cycles.[NEXT-DATE])
and GETDATE() <= cycles.[NEXT-DATE] --checking if date is in the future
I guess it should be sth like this
DATEDIFF(DAY, DATEADD(MONTH, 2, GETDATE()), cycles.[NEXT-DATE]) < 0
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.
I am trying to add days to the current date and it's working fine but when I add 360 days to the current date it gives me wrong value.
eg: Current Date is 11/04/2014
And I am adding 360 Days to it, it should give me 11/04/2015, but it is showing the same date 11/04/2014. the year is not changing.
Here is my code:
select dateadd(dd,360,getdate())
Just do-
Select (Getdate()+360) As MyDate
There is no need to use dateadd function for adding or subtracting days from a given date. For adding years, months, hours you need the dateadd function.
select dateadd(dd,360,getdate()) will give you correct date as shown below:
2017-09-30 15:40:37.260
I just ran the query and checked:
Dateadd(datepart,number,date)
You should use it like this:
select DATEADD(day,360,getdate())
Then you will find the same date but different year.
From the SQL Server 2017 official documentation:
SELECT DATEADD(day, 360, GETDATE());
If you would like to remove the time part of the GETDATE function, you can do:
SELECT DATEADD(day, 360, CAST(GETDATE() AS DATE));
In SQL Server 2008 and above just do this:
SELECT DATEADD(day, 1, Getdate()) AS DateAdd;
can try this
select (CONVERT(VARCHAR(10),GETDATE()+360,110)) as Date_Result
Two or three ways (depends what you want), say we are at Current Date is
(in tsql code) -
DECLARE #myCurrentDate datetime = '11Apr2014 10:02:25 AM'
(BTW - did you mean 11April2014 or 04Nov2014 in your original post? hard to tell, as datetime is culture biased. In Israel 11/04/2015 means 11April2014. I know in the USA 11/04/2014 it means 04Nov2014. tommatoes tomatos I guess)
SELECT #myCurrentDate + 360 - by default datetime calculations followed by + (some integer), just add that in days. So you would get 2015-04-06 10:02:25.000 - not exactly what you wanted, but rather just a ball park figure for a close date next year.
SELECT DateADD(DAY, 365, #myCurrentDate) or DateADD(dd, 365, #myCurrentDate)
will give you '2015-04-11 10:02:25.000'. These two are syntatic sugar (exacly the same). This is what you wanted, I should think. But it's still wrong, because if the date was a "3 out of 4" year (say DECLARE #myCurrentDate datetime = '11Apr2011 10:02:25 AM') you would get '2012-04-10 10:02:25.000'. because 2012 had 366 days, remember? (29Feb2012 consumes an "extra" day. Almost every fourth year has 29Feb).
So what I think you meant was
SELECT DateADD(year, 1, #myCurrentDate)
which gives 2015-04-11 10:02:25.000.
or better yet
SELECT DateADD(year, 1, DateADD(day, DateDiff(day, 0, #myCurrentDate), 0))
which gives you 2015-04-11 00:00:00.000 (because datetime also has time, right?). Subtle, ah?
This will give total number of days including today in the current month.
select day(getDate())
Add Days in Date in SQL
DECLARE #NEWDOB DATE=null
SET #NEWDOB= (SELECT DOB, DATEADD(dd,45,DOB)AS NEWDOB FROM tbl_Employees)
SELECT DateAdd(5,day(getdate()) this is for adding 5 days to current days.
for eg:today date is 23/08/2018 it became 28/08/2018 by using the above query
I'm trying to run a query that returns values based on yesterday's date. In this case how many items were shipped out yesterday. This is my query:
shh.SHIP_DATE = dateadd(dd, -1, getdate())
when I run it it doesn't return anything, but when I plug in a date it gives me values:
shh.SHIP_DATE = '2013-12-09'
Is the SHIP_DATE column a date or a datetime? I'm assuming it's a datetime column, but storing only a date value, so when you use getdate() you are going to get the time value as well. You could easily get it working by using the predicate (shh.SHIP_DATE = cast(dateadd(dd, -1, getdate()) as date))
GETDATE() includes the current time as well. You need to truncate the time portion. There's lost of ways to do that - here's one:
shh.SHIP_DATE = DATEADD(dd, DATEDIFF(dd, 0, getdate())-1, 0)
translated - add one less than the number of days between date "0" and today to date "0"
I have a table with columns
TypeExame, DateExame
How can I select all records with (Now-DateExame) > = 365 days?
select *
from MyTable
where datediff (day, DateExame, getdate()) >= 365
See DATEDIFF and GETDATE for further details.
If you have an index on DateExame, putting the condition like this will enable its usage:
SELECT *
FROM atable
WHERE DateExame <= DATEADD(DAY, -365, CAST(GETDATE() AS date))
;
The above references the date data type, which means you'd need at least SQL Server 2008 to run that. To make it work in earlier versions, you could rewrite the condition like this:
WHERE DateExame <= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) - 365, 0)
Basically, the modified version uses the DATEADD/DATEDIFF method of truncating a datetime value with a minor tweak to also subtract 365 days along the way.
However, if all your DateExame values are dates without the time part, this simpler version should work just as well:
WHERE DateExame <= DATEADD(DAY, -365, GETDATE())
That is, removal of GETDATE()'s result's time part would be perfectly unnecessary.
Try this one :
select *
from MyTable
where datediff (day, DateExame, getdate()) >= 365
By Using Datediff function you can subtract two dates . you can pass first argument as minute , day , month , year etc .