Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
T-SQL (SS2K5)
»
CASE WHEN Statement when creating a VIEW
CASE WHEN Statement when creating a VIEW
Rate Topic
Display Mode
Topic Options
Author
Message
SteveEClarke
SteveEClarke
Posted Monday, December 03, 2012 2:55 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: 2 days ago @ 5:22 AM
Points: 108,
Visits: 292
Hi Guys,
I am having a few issues with some code and wanted some guidance ;
I am creating a view based on a table, and need to create a "Identify" column, based on the data in the table ;
Simple - 3 columns
StockCode, MorB, Warehouse
StockA,M,AA
StockB,B,AA
StockC,B,BB
StockD,B,CC
StockE,M,BB
My Logic;
Select
StockCode, MoRB, ,Warehouse,
CASE
WHEN Warehouse = 'CC' then 'Cars'
WHEN MorB = 'B' then 'Bought'
WHEN Warehouse = 'AA' then 'Available'
ELSE 'xxx'
END
as StockCategory
Reseult:
I would hope to see the following;
StockD,B,CC,'Cars'
StockB,B,AA,'Bought'
StockA,M,AA,'Available'
StockC,B,BB,'xxx'
StockE,M,BB,'xxx'
My question is; Do I understand the CASE logic correctly and does it do one condition and then drop to the CASE...END statement after satisfying one of the conditions? or does it do EVERY CASE WHEN check and hold the last one as the description ?
Post #1391820
Eugene Elutin
Eugene Elutin
Posted Monday, December 03, 2012 3:35 AM
SSCrazy
Group: General Forum Members
Last Login: 2 days ago @ 10:59 AM
Points: 2,525,
Visits: 4,324
... does it do one condition and then drop to the CASE...END statement after satisfying one of the conditions?
That is correct.
_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
Post #1391831
SteveEClarke
SteveEClarke
Posted Monday, December 03, 2012 3:39 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: 2 days ago @ 5:22 AM
Points: 108,
Visits: 292
Eugene Elutin (12/3/2012)
... does it do one condition and then drop to the CASE...END statement after satisfying one of the conditions?
That is correct.
Sorry;
My question is;
Do I understand the CASE logic correctly and does it do one condition and then drop to the CASE...END statement after satisfying one of the conditions?
OR
Does it do EVERY CASE WHEN check and hold the last one as the description ?
So if I put an initial check in saying ... CASE StockCode like 'Stock%' Then 'Yippee'
ALL Records would fall into this and no other categories would be highlighted.
Post #1391832
anthony.green
anthony.green
Posted Monday, December 03, 2012 3:41 AM
SSCertifiable
Group: General Forum Members
Last Login: Friday, April 12, 2013 3:51 AM
Points: 5,075,
Visits: 4,831
It hits the first true condition then terminates the case statement.
Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1
&
Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger
Post #1391833
SteveEClarke
SteveEClarke
Posted Monday, December 03, 2012 3:43 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: 2 days ago @ 5:22 AM
Points: 108,
Visits: 292
anthony.green (12/3/2012)
It hits the first true condition then terminates the case statement.
Thank you - I did feel this was the case but the data does not reflect the logic!?
Will now dig deeper.
Thank you
Post #1391834
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.