Avatar of Norman Maina
Norman MainaFlag for Kenya asked on

sql query -select if statement

Using the below data as an example

ID    Stage         Reel                GW          NW
141      Intake      610356W007      156.76      158.20
183      Slitting      610356W007/3 32.17      32.77
140      Intake      610356W008       156.26      157.70

I want to select NW if Stage='Intake' or Select GW where Stage='Slitting'

How can i do that in a single SQL Query
Microsoft SQL ServerMicrosoft SQL Server 2005SQL

Avatar of undefined
Last Comment
Bhavesh Shah

8/22/2022 - Mon
beyazlale

Try this.
SELECT (CASE WHEN Stage='Intake'  THEN NW ELSE WHEN Stage='Slitting' GW ELSE 0 END) FROM (TableName)
stergium



Select *,
case when Stage= 'Intake' then NW
       Else GW end
From table
i think that would do the job.
beyazlale

That's the correct one.
SELECT (CASE WHEN Stage='Intake'  THEN NW WHEN Stage='Slitting' GW ELSE 0 END) FROM (TableName)
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
Norman Maina

beyazlale:>am getting "Incorrect syntax near the keyword 'WHEN'."
Sara bhai

Select case stage when 'Intake' then NW when 'Slitting' then GW end as colname from tabname
ASKER CERTIFIED SOLUTION
beyazlale

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Bhavesh Shah

Hi,

Thats is also wrong beyzalate


SELECT
            CASE Stage WHEN 'Intake'  THEN NW
                           WHEN 'Slitting' THEN GW
                           ELSE Stage END as 'New Column'
FROM TableName
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Dinesh Kumar

can you pass the table script containing data also so that I can check the output also in sql editor.
Bhavesh Shah

Regarding your code.


SELECT
            CASE WHEN Stage = 'Intake'  THEN NW
            ELSE CASE WHEN Stage = 'Slitting' THEN GW
            ELSE Stage END END as 'New Column'
FROM TableName
Bhavesh Shah

Hi,

You can use any of below.
SELECT 
      CASE Stage WHEN 'Intake'  THEN NW 
                 WHEN 'Slitting' THEN GW 
                 ELSE 0 END as 'New Column'
FROM TableName


SELECT 
		CASE WHEN Stage = 'Intake'  THEN NW 
		ELSE CASE WHEN Stage = 'Slitting' THEN GW 
		ELSE 0 END END as 'New Column'
FROM TableName

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
Norman Maina

works perfect.Thanks
Bhavesh Shah

Hi Norman,

SELECT (CASE WHEN Stage='Intake'  THEN NW WHEN Stage='Slitting' THEN GW ELSE 0 END) FROM (TableName)


THEN WAS MISSING.



ASKER
Norman Maina

Brichsoft:>hadnt seen your code - but it works.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Bhavesh Shah

Hey Norma....

its ok...if you didnt check....

As this Question might be stored in E.E. History so just telling Then was missing....

I write for newbie person..........