Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
PeterG
Posting Yak Master
156 Posts |
Posted - 2002-04-29 : 19:18:47
|
| What is the equivalent of a select...case VB statement in T-SQL? I've looked at CASE statement, but it is not what I'm looking for. What I want to do is this:declare @varA int, @varB varchar(12)Select @varA = fieldname from tablename;if @varA = 1then @varB = "Variable A"else if @varA = 2then @varB = "Variable B"else if @varA = 3then @varB = "Variable C".... and so on until @varA = 10I could have 10 if statements if I want to, but can I use a cleaner syntax like the select...case block? |
|
|
motokevin
Starting Member
36 Posts |
Posted - 2002-04-29 : 19:38:16
|
| I think CASE may work the way you want it to. Run this code and play with the value of @MyVar2 and you will see how it works.Declare @MyVar1 Varchar(5)Declare @MyVar2 Integer Set @MyVar2 = 3Set @MyVar1 = Case @MyVar2 WHEN 1 THEN 'one' WHEN 2 THEN 'two' WHEN 3 THEN 'three' WHEN 4 THEN 'four' ELSE 'Big'ENDSelect @MyVar1 As TheResult |
 |
|
|
byrmol
Shed Building SQL Farmer
1591 Posts |
Posted - 2002-04-29 : 21:41:27
|
Peter,We don't even have to bother with a CASE statement...DECLARE @varA int, @varB VARCHAR(12) SET @varA = 4Select @varB = 'Variable ' + UPPER(CHAR(96 + @varA))Select @varB HTHDavidMTomorrow is the same day as Today was the day before. |
 |
|
|
|
|
|