Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 DAte conversion

Author  Topic 

Jbalbo
Starting Member

10 Posts

Posted - 2013-02-14 : 13:35:12
Sorry if its in the wrong place

Getting a copnversion error on this
DECLARE @FISCALDATE DATE
DECLARE @ENDDATE DATE

set @ENDdate = '02/01/2012'

SELECT CASE
WHEN datepart(MM,@ENDdate)<= 6
THEN
CONVERT(datetime, '01-' + '07-'+ (DATEPART(YY, @ENDDATE) -1), 103)
--DATEPART(YY, @ENDDATE) -1
ELSE
CONVERT(datetime, '01-' + '07-'+ (DATEPART(YY,@ENDDATE)), 103)

END AS fiscalDATE


BTW what Im trying to do is look at a paramter date (Enddate) then calculate the fiscal year start date from it

Thanks IN Advance
Joe

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-14 : 14:10:01
[code]DECLARE @FISCALDATE DATE
DECLARE @ENDDATE DATE

SET @ENDdate = '02/01/2012'

SELECT CASE
WHEN DATEPART(MM, @ENDdate) <= 6 THEN CONVERT(DATETIME, '01-' + '07-' + CAST((DATEPART(YY, @ENDDATE) -1) AS CHAR(4)), 103)
--DATEPART(YY, @ENDDATE) -1
ELSE CONVERT(DATETIME, '01-' + '07-' + CAST((DATEPART(YY, @ENDDATE) -1) AS CHAR(4)), 103)
END AS fiscalDATE
[/code]But it might be simpler to do it this way:[code]DECLARE @ENDDATE DATE

SET @ENDdate = '02/01/2012'

SELECT DATEADD(mm,(DATEDIFF(mm,0,@ENDdate)-6)/12*12+6,0) AS fiscalDate[/code]
Go to Top of Page

Jbalbo
Starting Member

10 Posts

Posted - 2013-02-14 : 16:32:01
That is much nicer way than I was thinking

Thanks
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-14 : 18:47:04
You are very welcome - glad to help.
Go to Top of Page
   

- Advertisement -