I have a Table whose Column has an Expression as its Value. I want to write a Single Select Query to check if the Expression in that Column holds TRUE.
Here is My Table:
CREATE TABLE #TEMP (FinYear VARCHAR(9), TaxType VARCHAR(50), Rate NUMERIC(10,4), SlabExpression VARCHAR(50))
INSERT INTO #TEMP
VALUES ('2014-2015', 'SURCHARGE', 0.00, '#<=10000000'),
('2014-2015', 'SURCHARGE', 10.00, '#>10000000'),
('2014-2015', 'Education Cess', 3.00, '#<=10000000'),
('2014-2015', 'Education Cess', 3.00, '#>10000000')
SELECT * FROM #TEMP
DROP TABLE #TEMP
My Income value now is 1200000 which obviously falls under the slab - '#>10000000' on which I intend to apply Surcharge and Education Cess rates.
But, How do I find the Correct Slab with a Single Query.
I know I can do that using a Cursor. But, I do not want to use a Cursor here. I need a Single Query.
Note: This Table has Two SlabExpression Values in one Column
ie. '#<=10000000' AND '#>10000000'
I want to Write a Single SELECT Query to get the two Rows for which the SlabExpression Holds True.
ie. I want the Result as the two Rows with SlabExpression '#>10000000'
Basically, I want a Query something Like
SELECT * FROM #TEMP WHERE SlabExpression Holds True
Here is one way to solve this.
Tested and verified on Sql server 2012, for some reason I can't seem to make it work on sql fiddle. However, to the best on my knowledge, I didn't use anything that is not supported on Sql server 2008.
Results:
Explanations: I've used the first cte to break down the SlabExpression to operator and value, and the second cte to use that operator and value against the variable value.
I think these SQL useful to you.
Thank you.