Select a date without time (SQL Server (+) [duplicate] - sql-server

This question already has answers here:
How to return only the Date from a SQL Server DateTime datatype
(46 answers)
Closed 6 years ago.
I would like to retrieve a date minus 1 year, but without time notation.
The following query
SELECT DATEADD(year, -1, GETDATE())
Output:
2015-03-30 10:48:04.220
What I want is 2015-03-30 00:00:00:000
Similar to:
(DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0))
Which results in: 2016-03-30 00:00:00.000
What is the correct or easiest way to do this?

On SQL Server 2008 and higher, you should convert to date:
SELECT CONVERT(date, (DATEADD(year, -1, GETDATE())))
On older versions, you can do the following:
SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, DATEADD(year, -1, GETDATE())))

Related

Subtract 1 year and 1 day at SQL Server

In order to subtract 1 year I use
SELECT DATEADD(year, -1, GETDATE())
Likewise for the day.
What is the syntax if I want the combination??
ex from 30/05/2020 ---> 29/05/2019
Wrap that in another DATEADD call.
SELECT DATEADD(DAY, -1, DATEADD(YEAR, -1, GETDATE()))
You could do the following, as SQL Server adds/subtracts days by default:
SELECT DATEADD(year, -1, GETDATE()-1)

SQL Query to find the last day of the month in 6 months time

I am trying to write a bit of SQL that will give the last day of the month in 6 months time.
E.g. if I have of a date of inspection 15-07-2015 I want the next inspection date to be 31-01-2016
The date of inspection could be any day of any month
Any advise would be appreciated.
if i understand. that's it
declare #d date = '2015-07-15'
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#d)+7,0))
If you are using SQL Server 2012+ you can use the eomonth function to get the last day of a month:
declare #d date = '2015-07-15'
select eomonth(#d,6)
result: 2016-01-31
The function takes a date and an optional integer that specifies the number of months to add to the date parameter.
try this
declare #FindLastDay datetime
set #FindLastDay=CONVERT(varchar(10),DATEADD(M,6,'2015-07-15'),120)
SELECT CAST(DATEADD(ms, -3, DATEADD(mm, DATEDIFF(m, 0, #FindLastDay) + 1, 0)) as DATE)
output:
2016-01-31
Select DateAdd(D, Day(DateAdd(Month, 7, '2015-07-15')) * -1, DateAdd(M, 7, '2015-07-15'))
Yields the answer you expect.

Query to get the first day of the month not working with DATEADD

I've been writing queries to truncate the time from a given datetime for years now, based on answers like this one and this one (and many other places), that groups a couple functions like
SELECT DATEADD(day, 0, DATEDIFF(day, 0, getdate()))
and it always gives the right answer.
I figured I could translate the same logic to finding the first of the current month by using month instead of day, but it's giving me a weird date for the result: 1903-10-17, instead of 2015-05-01.
My parameters have always been in the wrong order.
It turns out the format for DATEADD I've been using all these years is wrong, and it's only been working because it's using the day datepart. Casting an int to a date increments the day:
SELECT CAST(0 AS datetime) = '1900-01-01 00:00:00.000'
SELECT CAST(1 AS datetime) = '1900-01-02 00:00:00.000'
SELECT CAST(2 AS datetime) = '1900-01-03 00:00:00.000'
I should be using DATEADD(d, DATEDIFF(d, 0, getdate()), 0) - the parameters are (datepart, number, date), as laid out here at MSDN.
Writing it as SELECT DATEADD(month, DATEDIFF(month, 0, getdate()), 0) gives the expected result of 2015-05-01.

Date Range of the x-th week in SQL Server?

I know the year and the index of the week, i.e. 2014, the 5th week. how can I find out the starting and ending date of this week in SQL Server? I do not really care whether the week starting with Monday or Sunday.
In mySQL, it has a MakeDate which may be able to do this. Is there an existing way to do this in SQL Server?
Thanks
Try this.
DECLARE #date DATE='2014-01-01'
SELECT Dateadd(dd, -( Datepart(dw, Dateadd(wk, 5, #date) - 1) ), Dateadd(wk, 5, #date)) [WeekStart],
Dateadd(dd, 7 - ( Datepart(dw, Dateadd(wk, 5, #date)) ), Dateadd(wk, 5, #date)) [WeekEnd]
SELECT
dateadd(wk,5,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)) as firstdayof5thweek2014,
dateadd(dd,6,dateadd(wk,5,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))) as lastdayof5thweek2014
SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) ==>gives you start day of the current year
SELECT dateadd(wk,5,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)) ==> On top add 5 weeks to get the start date of 5th week in 2014
SELECT dateadd(dd,6,dateadd(wk,5,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))) ==> On top add 6 days to get the last date of 5th week in 2014

Construct DateTime using today's date at a specific time

I'd like to get 4:30 PM of the current day. Hard-coding this way doesn't work:
SELECT '07242012 16:30:00.000'
This is proving to be more difficult than I thought it would be. How do I approach this?
SQL Server 2000 / 2005:
SELECT DATEADD(MINUTE, 30, DATEADD(HOUR, 16, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP)));
-- or
SELECT DATEADD(MINUTE, (16*60) + 30, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP))
-- or
SELECT CONVERT(DATETIME, CONVERT(CHAR(9), CURRENT_TIMESTAMP, 112) + '16:30');
SQL Server 2008+:
SELECT CONVERT(DATETIME, CONVERT(DATE, CURRENT_TIMESTAMP)) + '16:30';
SQL Server 2012:
SELECT SMALLDATETIMEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), DAY(GETDATE()), 16, 30);
Probably the easiest thing to do is to cast the current date/time to a date (stripping the time off), cast it back to a datetime to allow use of datetime's overloaded + (plus) and, finally cast your desired textual time to a datetime. As follows:
select cast(cast(sysutcdatetime() as date) as datetime) + cast('16:30' as datetime)
returns (when run on 11th Jan 2018):
2018-01-11 16:30:00.000
You can construct this as you like with day, hour, minute etc.
SELECT CURDATE() - interval 1 DAY + interval 2
select(dateadd(day, datediff(day, 0, getdate()), 0) + '20:00') as specified_date
specified_date - Output Column name
20:00 - Specified time(24 hr Format -Default)
getdate() - To get Today's date.

Resources