July 1, 2014 at 10:11 am
HOW TO CONVERT THE BELOW FROM PLSQL TO TSQL
YTD_Deprn2=DECODE(SUBSTR(Period_Name,-2),SUBSTR(:Date,-2),DECODE(NVL(Prior Fy Expense,0),0,NVL(Ytd_ Deprn,0),Ytd Less Pfye),0)
YTD Deprn=DECODE(TRUNC(PYE Deprn Reserve+YTD Deprn2,2),0,0,YTD Deprn2)--
July 1, 2014 at 10:24 am
Use
Oracle DECODE = T-SQL:
CASE WHEN [condition 1] THEN [expression 1]
WHEN [condition 2] THEN [expression 2]
....
ELSE [expression]
END
SUBSTR = T-SQL: SUBSTRING
NVL = T-SQL: ISNULL
July 1, 2014 at 10:25 am
STYLES (7/1/2014)
HOW TO CONVERT THE BELOW FROM PLSQL TO TSQLYTD_Deprn2=DECODE(SUBSTR(Period_Name,-2),SUBSTR(:Date,-2),DECODE(NVL(Prior Fy Expense,0),0,NVL(Ytd_ Deprn,0),Ytd Less Pfye),0)
YTD Deprn=DECODE(TRUNC(PYE Deprn Reserve+YTD Deprn2,2),0,0,YTD Deprn2)--
DECODE can be replaced by a case statement as it is a "if then else" function.
😎
July 1, 2014 at 10:25 am
First, are you using SQL Server 2012 or SQL Server 2005? You have double posted in two different forums.
Second, please don't double post, it fragments answers you may get.
Third, you want to convert the DECODE to a CASE clause.
July 1, 2014 at 10:46 am
OK
this is where i am at and stuck
declare @period_name as date = 'DEC-10'
case when substring(period_name,5,2) = '@periodname' THEN SUBSTRING('@periodname' ,5,2)
the period names are in format DEC-01, JAN-01, FEB-01 etc
July 1, 2014 at 10:58 am
STYLES (7/1/2014)
OKthis is where i am at and stuck
declare @period_name as date = 'DEC-10'
case when substring(period_name,5,2) = '@periodname' THEN SUBSTRING('@periodname' ,5,2)
the period names are in format DEC-01, JAN-01, FEB-01 etc
Have you read BoL about SUBSTRING function?
substring(period_name,5,2) will return string of 2 characters length starting from 5th character of period_name...
Placing @periodname variable in quotes makes it literal string constant.
July 1, 2014 at 11:01 am
Remove the quotes from '@periodname'
declare @period_name as date = 'DEC-10'
SELECT CASE WHEN substring(period_name,5,2) = @periodname THEN SUBSTRING(@periodname ,5,2)
July 1, 2014 at 1:24 pm
I think you'll need to change the way you deal with this slightly.
I suspect that your period_name column is a char or varchar, so declaring @periodname as a date is a wee bit pointless and won't work in a SUBSTRING without a explicit conversion.
Taking your first statement to convert you would get something like
-- YTD_Deprn2=DECODE(SUBSTR(Period_Name,-2),SUBSTR(:Date,-2),DECODE(NVL(Prior Fy Expense,0),0,NVL(Ytd_ Deprn,0),Ytd Less Pfye),0)
-- assuming column for comparison is char or varchar
-- if it is a date datatype use YEAR(...)
DECLARE @periodName varchar(7) = 'DEC-10'
SELECT
CASE
-- If date use WHEN YEAR(Period_Name) = YEAR(@periodName) THEN
WHEN SUBSTRING(Period_Name,5,2) = SUBSTRING(@periodName,5,2) THEN
CASE
WHEN ISNULL([Prior Fy Expense],0) = 0 THEN ISNULL([Ytd_ Deprn],0)
ELSE [Ytd Less Pfye]
END
ELSE 0
END
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply