SQL Query

I want to make every that satisfy my condition to = 1400.

I wrote:

Select GLDV_Alt
Case
When gldv_alt_div_n = '1209', '1120' , '1401'
then '1400'
Else 0
End As 'Region South
from gldv


This works if my list is just one division, but not when it's a list of divisions.

Do I have to do a bunch of When operators?

Please help.
LVL 1
isamesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Here you go ... also made multiple syntax corrections
Select GLDV_Alt, 
Case When gldv_alt_div_n IN ( '1209', '1120' , '1401') then '1400' Else 0 End As [Region South]
from gldv

Open in new window


For more info on CASE blocks check out my image and demo-heavy tutorial called SQL Server CASE Solutions
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ste5anSenior DeveloperCommented:
Yup, that's a possibility. Or use the IN clause:

SELECT  GLDV_Alt ,
        CASE WHEN gldv_alt_div_n IN ( '1209', '1120', '1401' ) THEN '1400'
             ELSE 0
        END AS 'Region South'
FROM    gldv;

Open in new window

0
Scott PletcherSenior DBACommented:
The final choice would be to use a lookup table.  The only real advantage of this method is less chance for error if the list of numbers changes and/or becomes more complex, as the query itself doesn't change, just the values loaded into the table.  For a very large gldv table, you might even notice some performance improvement.

CREATE TABLE #GLDV_Div (
    gldv_alt_div_n varchar(4) NOT NULL,
    result varchar(4) NOT NULL
    )
INSERT INTO #GLDV_Div VALUES('1209' , '1400')
INSERT INTO #GLDV_Div VALUES('1120' ,  '1400')
INSERT INTO #GLDV_Div VALUES('1401'  , '1400')


Select GLDV_Alt, isnull(lookup.result, '0') As 'Region South'
from gldv
left outer join #GLDV_Div lookup on lookup.gldv_alt_div_n = gldv.gldv_alt_div_n
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
isames, do you still need help with this question?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.