October 26, 2015 at 9:53 am
Hello All,
I have some code that works for my situation. However it is kind of clunky. Is there a better method of doing this?
Here is the requirement.
November 6 of the year prior to the Report year
Here is my current code which gives the correct answer just looking for a more simplified or elegant solution.
DECLARE @StartDate DATE = '01/01/15'
SELECT '11/06/' + CAST(DATEPART(YEAR,DATEADD(YEAR, -1, @StartDate)) AS VARCHAR (10))
Thanks!
***SQL born on date Spring 2013:-)
October 26, 2015 at 10:00 am
Just to be clear: if @StartDate is November 28th, 2015 - you still want 11/6/2014? I think that's the case, just double-checking...
This is a little cleaner:
DECLARE @StartDate DATE = '01/01/15';
SELECT '11/06/' + CAST(YEAR(@StartDate)-1 AS char(4));
-- Itzik Ben-Gan 2001
October 26, 2015 at 10:02 am
Yes sir that is correct. Wow, I love it I knew their was a better way!
Thanks Alan!!
***SQL born on date Spring 2013:-)
October 26, 2015 at 10:14 am
Alan.B (10/26/2015)
Just to be clear: if @StartDate is November 28th, 2015 - you still want 11/6/2014? I think that's the case, just double-checking...This is a little cleaner:
DECLARE @StartDate DATE = '01/01/15';
SELECT '11/06/' + CAST(YEAR(@StartDate)-1 AS char(4));
Is this ddmmyyyy or mmddyyyy? I know that 'yyyymmdd' is unambiguously cast to a date(time) but this format may be sensitive to regional settings.
SELECT CAST('06/31/2015' AS datetime)
-- Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 26, 2015 at 10:21 am
Hi Allan,
The format in the date column I am filtering on is like this YYYYMMDD
Your code seems to be working perfectly though in my report query.
Thanks!
***SQL born on date Spring 2013:-)
October 26, 2015 at 10:23 am
Here's a different approach with 2 variants:
DECLARE @StartDate DATE = '01/01/15';
SELECT DATEADD( YY, DATEDIFF( YY, 0, @StartDate), '18991106'),
DATEADD( YY, DATEDIFF( YY, 0, @StartDate), -56),
CAST( CAST( '18991106' AS datetime) AS int)
October 26, 2015 at 11:54 am
Don't think DATEFROMPARTS() was available in SQL 2008? but for later versions I would use
DECLARE @StartDate DATE = '01/01/15';
SELECT DATEFROMPARTS(YEAR(@StartDate)-1, 11, 6);
which avoids all the ambiguity of "nn/nn/nn" style string dates which parse differently depending on the settings of the currently logged on user.
SET LANGUAGE 'JAPANESE'
will parse '01/01/2015' as expect but '01/01/15' as 2001-01-15 !
October 26, 2015 at 12:26 pm
Does this work (can't test it, on a flight to Seattle)?
declare @startdate datetime = "20151126";
select dateadd(day,5,dateadd(month,10,dateadd(year,year(@startdate) - 1900 - 1),0)));
October 26, 2015 at 12:32 pm
Lynn Pettis (10/26/2015)
Does this work (can't test it, on a flight to Seattle)?
declare @startdate datetime = "20151126";
select dateadd(day,5,dateadd ( month,10,dateadd ( year,year(@startdate) - 1900 - 1),0)));
Corrected version for Lynn's code:
declare @startdate datetime = '20151126';
select dateadd(day,5,dateadd( month,10,dateadd( year,year(@startdate) - 1900 - 1,0)));
Enjoy your time in Seattle. ๐
October 30, 2015 at 6:11 pm
Just another country heard from... this one returns a DATETIME datatype and works in all versions of SQL Server.
DECLARE @StartDate DATE = '01/01/2015';
SELECT DATEADD(yy,YEAR(@StartDate)-1901,'19001106');
I still like Luis' method better though. Here's his entry again...
DECLARE @StartDate DATE = '01/01/2015';
SELECT DATEADD( YY, DATEDIFF( YY, 0, @StartDate), '18991106')
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply