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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to use multiple Cases in Sql

Author  Topic 

Ehtesham Siddiqui
Starting Member

10 Posts

Posted - 2011-11-11 : 05:48:55
Hi,
Im using Sql server 2005,I have two tables.Say table1 and table2
All the data is fetched from Table1 only.table two is used for confirmation.
Like There is a Column say StudentMobile in table2 which hold the values either True or False.table 2 also has a Registration Number Column,now if this StudentMobile value is true then only the data of that particular student should be fetched from Table1.
Please Have a look at my Query.I have successfully done with using single Case Statement but m not able to aplly multiple Cases.
Please Guide.

SELECT Table1.RegNo,Table1.MobileNo,
case when Table2.UseStudentMobile='True' then Table1.StudentMobile
end
//Im having issue when i use another Case Here
case when Table2.ParentStudentMobile='True' then Table1.ParentMobile
end

FROM Table1 INNER JOIN
Table2 ON Table1.RegNo = Table2.Regno

The code in Bold is the Line that is giving me Error.If i remove that case than im not getting any error.

SMK
Software Engineer
India

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-11-11 : 05:52:10
SELECT Table1.RegNo,Table1.MobileNo,
case when Table2.UseStudentMobile='True' then Table1.StudentMobile
end ,
case when Table2.ParentStudentMobile='True' then Table1.ParentMobile
end
FROM Table1 INNER JOIN
Table2 ON Table1.RegNo = Table2.Regno

or maybe you want

SELECT Table1.RegNo,Table1.MobileNo,
case when Table2.UseStudentMobile='True' then Table1.StudentMobile
when Table2.ParentStudentMobile='True' then Table1.ParentMobile
end
FROM Table1 INNER JOIN
Table2 ON Table1.RegNo = Table2.Regno

Is ParentMobile on Table1 or Table2?

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Devart
Posting Yak Master

102 Posts

Posted - 2011-11-11 : 05:55:45
Hello,

Try it:

SELECT Table1.RegNo,Table1.MobileNo,
CASE WHEN Table2.UseStudentMobile='True' THEN Table1.StudentMobile
END AS StudentMobile,
CASE WHEN Table2.ParentStudentMobile='True' THEN Table1.ParentMobile
END AS ParentMobile
FROM Table1 INNER JOIN Table2 ON Table1.RegNo = Table2.Regno

Devart,
SQL Server Tools:
dbForge Data Studio
dbForge Schema Compare
dbForge Data Compare
dbForge SQL Complete
Go to Top of Page
   

- Advertisement -