all groups > sql server programming > april 2007 >
You're in the

sql server programming

group:

Is there an equivalent to a VB CHOOSE statement in SQL?


Re: Is there an equivalent to a VB CHOOSE statement in SQL? ML
4/17/2007 2:52:04 PM
sql server programming:
Well... almost... :)
There should be only one 1. ;)

SELECT ...,
[DayOfWeek],
CASE [DayOfWeek]
WHEN 1 THEN 'Monday'
WHEN 2 THEN 'Tuesday'
WHEN 3 THEN 'Wednesday'
WHEN 4 THEN 'Thursday'
WHEN 5 THEN 'Friday'
WHEN 6 THEN 'Saturday'
WHEN 7 THEN 'Sunday'
ELSE 'Unknown'
END AS DescriptionOfDayOfWeek
FROM T1;


ML

---
Re: Is there an equivalent to a VB CHOOSE statement in SQL? ML
4/17/2007 2:56:01 PM
If only Copy/Paste were smarter... ;)


ML

---
Re: Is there an equivalent to a VB CHOOSE statement in SQL? SQL Menace
4/17/2007 3:07:58 PM
[quoted text, click to view]

of course a CASE is not needed in this case ;-)

select datename(dw,DayOfWeek)

this assumes your data is clean and not junk like this: select
datename(dw,1), datename(dw,666), datename(dw,-1)

Denis the SQL Menace
http://sqlservercode.blogspot.com/
Re: Is there an equivalent to a VB CHOOSE statement in SQL? SQL Menace
4/17/2007 3:16:17 PM
[quoted text, click to view]

Oc course if I had any brains I would have posted this instead

SELECT ...,
[DayOfWeek],
CASE [DayOfWeek]
WHEN BETWEEN 1 and 7 THEN DATENAME(dw,[DayOfWeek] )
ELSE 'Unknown'
END AS DescriptionOfDayOfWeek
FROM T1;


Denis the SQL Menace
http://sqlservercode.blogspot.com/
Re: Is there an equivalent to a VB CHOOSE statement in SQL? SQL Menace
4/17/2007 3:54:35 PM
On Apr 17, 6:28 pm, "Aaron Bertrand [SQL Server MVP]"
[quoted text, click to view]

I butchered that didn't I?
Note to self always create a table with data and test the code before
replying

Re: Is there an equivalent to a VB CHOOSE statement in SQL? Itzik Ben-Gan
4/17/2007 4:45:18 PM
Rico,

You can use a CASE expression for this purpose:

SELECT ...,
[DayOfWeek],
CASE [DayOfWeek]
WHEN 1 THEN 'Monday'
WHEN 1 THEN 'Tuesday'
WHEN 1 THEN 'Wednesday'
WHEN 1 THEN 'Thursday'
WHEN 1 THEN 'Friday'
WHEN 1 THEN 'Saturday'
WHEN 1 THEN 'Sunday'
ELSE 'Unknown'
END AS DescriptionOfDayOfWeek
FROM T1;

--
BG, SQL Server MVP
www.SolidQualityLearning.com
www.insidetsql.com


[quoted text, click to view]

Re: Is there an equivalent to a VB CHOOSE statement in SQL? Itzik Ben-Gan
4/17/2007 4:48:08 PM
And even better when you use the right one. ;-)

SELECT ...,
[DayOfWeek],
CASE [DayOfWeek]
WHEN 1 THEN 'Monday'
WHEN 2 THEN 'Tuesday'
WHEN 3 THEN 'Wednesday'
WHEN 4 THEN 'Thursday'
WHEN 5 THEN 'Friday'
WHEN 6 THEN 'Saturday'
WHEN 7 THEN 'Sunday'
ELSE 'Unknown'
END AS DescriptionOfDayOfWeek
FROM T1;

--
BG, SQL Server MVP
www.SolidQualityLearning.com
www.insidetsql.com


[quoted text, click to view]

Re: Is there an equivalent to a VB CHOOSE statement in SQL? Itzik Ben-Gan
4/17/2007 5:00:59 PM
Yeah. :)

--
BG, SQL Server MVP
www.SolidQualityLearning.com
www.insidetsql.com


[quoted text, click to view]

Re: Is there an equivalent to a VB CHOOSE statement in SQL? Aaron Bertrand [SQL Server MVP]
4/17/2007 5:40:29 PM
[quoted text, click to view]

You're looking for a case expression, e.g.

DECLARE @InputValue INT;
SET @InputValue = 2;
SELECT CASE WHEN @InputValue = 1 THEN 'foo' WHEN @InputValue = 2 THEN 'bar'
ELSE 'blat' END

[quoted text, click to view]

Well, I suppose you mean to use this for a less trivial example. But just
in case, you can do this with DATENAME and without getting the numeric
constant for the day of the week, assuming you can trust your datefirst
settings...

SELECT DATENAME(DW, GETDATE());

--
Aaron Bertrand
SQL Server MVP
http://www.sqlblog.com/
http://www.aspfaq.com/5006

Re: Is there an equivalent to a VB CHOOSE statement in SQL? Aaron Bertrand [SQL Server MVP]
4/17/2007 5:53:36 PM
[quoted text, click to view]

You don't really expect to move to a completely different language and
platform, and not have to change your syntax? :-)

Re: Is there an equivalent to a VB CHOOSE statement in SQL? Aaron Bertrand [SQL Server MVP]
4/17/2007 6:28:35 PM
[quoted text, click to view]

You can't use WHEN before the comparison target when using a range, only
when testing for equality.

SELECT
CASE 5
WHEN BETWEEN 1 and 7 THEN DATENAME(dw,5)
ELSE 'Unknown'
END AS DescriptionOfDayOfWeek

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'BETWEEN'.

Needs to be:

SELECT
CASE WHEN 5 BETWEEN 1 and 7 THEN DATENAME(dw,5)
ELSE 'Unknown'
END AS DescriptionOfDayOfWeek

Of course, DATENAME(dw, 5) is like saying DATENAME(dw, '1900-01-06')...

(Since 5 is interpreted as a number of days offset from 0, not a number on
its own.)

What day is today? Is it Thursday or Tuesday?

DECLARE @dp INT;
SELECT @dp = DATEPART(DW, GETDATE());
SELECT DATENAME(DW, @dp); -- Thursday
SELECT DATENAME(DW, GETDATE()); -- Tuesday



Is there an equivalent to a VB CHOOSE statement in SQL? Rico
4/17/2007 9:31:44 PM
Hello,

For anyone that's a VB programmer out there, I'm looking for the evivalent
of a Choose statemetn in SQL Server. For those that aren't VB guys, a
choose statement will return a value, based on the numeric value of the
input. for instance;

Choose(InputValue,[Return if InputValue=1],[Return if InputValue=2],[Return
if InputValue=3],[Return if InputValue=4],...etc)

Example;
Choose(DayOfWeek,"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")

Any direction is greatly appreciated.

Thanks!
Rick

Re: Is there an equivalent to a VB CHOOSE statement in SQL? Rico
4/17/2007 9:50:07 PM
Thanks folks. I was afraid of that, was just hoping that there would be an
easier way.

Thanks again!
Rick

[quoted text, click to view]

AddThis Social Bookmark Button