SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert and CAst


Convert and CAst

Author
Message
sharonsql2013
sharonsql2013
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1290 Visits: 1265
I am trying to get the begining and end of a fiscal year based on User input date.
@para is a date in table based on which the fiscal year as to be calculated.
User date will be compared with @para and then the fiscal year calculated.

Scenario :
If @para is 2014-02-01 then
Begfiscal will be 2013-02-01
Endfiscal will be 2012-01-31

I am able to get the begining year but the End year is causing a problem.
Here is logic which gets me to Fiscal End year as 2012-02-01 . Don't know how to remove another day....
Please advise

CASE
WHEN CONVERT ( DATETIME, CONVERT ( VARCHAR(5), @para, 1 ) + '/' + CAST ( YEAR ( @UserInputDt ) AS VARCHAR ), 101 ) <= @UserInputDt
THEN
CONVERT ( DATETIME, CONVERT ( VARCHAR(5), @para, 1 ) + '/' + CAST ( YEAR ( @UserInputDt ) + 1 AS VARCHAR ), 101 )

ELSE
CONVERT ( DATETIME, CONVERT ( VARCHAR(5), @para, 1 ) + '/' + CAST ( YEAR ( @UserInputDt ) AS VARCHAR ), 101 )
END AS FiscalEndDt
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62814 Visits: 13298
Your scenario isn't entirely clear to me.
You do not explain what the value @UserInputDt has to do with @para (=2014-02-01).
Why is the result for Begfiscal 2013-02-01 and for Endfiscal 2012-01-31?

Wouldn't it be easier to use datetime functions (such as dateadd) instead of manipulating strings?


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42157 Visits: 19829
As Koen said, date functions are much better for this than manipulating strings.
Based on your example, this is a way to calculate it.

DECLARE @para date = '20140201'

SELECT @para,
DATEADD( YEAR, -1, @para) Begfiscal,
DATEADD( DAY, -1, DATEADD( YEAR, -2, @para)) Endfiscal




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search