August 3, 2009 at 3:06 pm
Hi Folks
I am trying to incorporate a CAST statment into my CASE statements and am not having any luck and do not know if that is even possible.
This is just a quick example of what I have and what i would like to see
My example has this:
DECLARE @TestVal INT
SET @TestVal = 3
SELECT
CASE @TestVal
WHEN 1 THEN 'First One'
WHEN 2 THEN 'Second One'
WHEN 3 THEN 'Third One'
ELSE 'Other'
END
go
Output:
----------
Third One
Notice there is no heading whch I need and cannot figure out how to get
Second Example:
I want to incorporate a CAST statement into the mix
so that it comes out with a heading name and under it the abbreviated value
Something like:
Value
-----
Third
Thanks
Jim
August 3, 2009 at 3:16 pm
JC (8/3/2009)
Hi FolksI am trying to incorporate a CAST statment into my CASE statements and am not having any luck and do not know if that is even possible.
This is just a quick example of what I have and what i would like to see
My example has this:
DECLARE @TestVal INT
SET @TestVal = 3
SELECT
CASE @TestVal
WHEN 1 THEN 'First One'
WHEN 2 THEN 'Second One'
WHEN 3 THEN 'Third One'
ELSE 'Other'
END
go
Output:
----------
Third One
Notice there is no heading whch I need and cannot figure out how to get
Second Example:
I want to incorporate a CAST statement into the mix
so that it comes out with a heading name and under it the abbreviated value
Something like:
Value
-----
Third
Thanks
Jim
Try this as you need a column alias:
DECLARE @TestVal INT
SET @TestVal = 3
SELECT
CASE @TestVal
WHEN 1 THEN 'First One'
WHEN 2 THEN 'Second One'
WHEN 3 THEN 'Third One'
ELSE 'Other'
END as Value
August 3, 2009 at 3:33 pm
Hi Lynn
that will work 🙂
is there anyway to interject a CAST statement to manipulate the length of the value ?
Thanks
Jim
August 4, 2009 at 8:20 am
Can you provide an example of what you want to do with the CAST? I would venture to say that you can do it, but I'm not sure what you want to do.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
August 4, 2009 at 9:27 am
Hi Jack
lets say my example is this:
DECLARE @TestVal INT
SET @TestVal = 3
SELECT
CASE @TestVal
WHEN 1 THEN 'First One XXXXXXXXXXXXXXXX'
WHEN 2 THEN 'Second One XXXXXXXXXXXXXXXX'
WHEN 3 THEN 'Third One XXXXXXXXXXXXXXXX'
ELSE 'Other'
END TestVal;
go
If i just wanted to see the first 10 characters of Testval, how could I incorporate a CAST statement like this one in the above sql ?
CAST (TestVal as varchar(10)) TestValExp
August 4, 2009 at 9:32 am
JC,
I wouldn't use CAST, I'd use either the LEFT() or SUBSTRING() function. This will work:
DECLARE @TestVal INT
SET @TestVal = 3
SELECT
LEFT(CASE @TestVal
WHEN 1 THEN 'First One XXXXXXXXXXXXXXXX'
WHEN 2 THEN 'Second One XXXXXXXXXXXXXXXX'
WHEN 3 THEN 'Third One XXXXXXXXXXXXXXXX'
ELSE 'Other'
END, 10) TestVal ;
--OR
SELECT
SUBSTRING(CASE @TestVal
WHEN 1 THEN 'First One XXXXXXXXXXXXXXXX'
WHEN 2 THEN 'Second One XXXXXXXXXXXXXXXX'
WHEN 3 THEN 'Third One XXXXXXXXXXXXXXXX'
ELSE 'Other'
END, 1, 10) TestVal ;
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
August 4, 2009 at 10:10 am
Thanks Jack
i would not have figureed that out especially when putting the LEFT statement on two lines
LEFT(CASE @TestVal
xx
xx
xx
END, 10) TestVal ;
is that the way all functions work with CASE statements ?
Thanks
jim
August 4, 2009 at 10:15 am
SQL Server doesn't care about white space so yeah you can do that with any function as long as the CASE or inner function returns the right data type for the outer function.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
August 4, 2009 at 10:59 am
Hi Jack
okay..
thanks
just seems peculiar
appreciate the assist
Jim
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply