Solved

sql script help in using case statment with a select statement

Posted on 2014-12-19
8
197 Views
Last Modified: 2014-12-19
i am new to sql scripting.  Basically all I am trying to do is to output one column called 'Rev' based on the two case statements below.  The scripting works for the most part.  The only problem is that it outputs two columns called 'Rev'.  i just need one column.  So if the first case statement condition is not found, then move to the second case condition etc.

DECLARE @FullName   VARCHAR(100)
SET @FullName = '07000496_C.F2'

SELECT
            case when @FullName like '%_%' and @FullName not like '%.%' then SUBSTRING(@FullName, CHARINDEX('_', @FullName) + 1, LEN(@FullName)) end AS Rev,
            case when @FullName like '%_%' and @FullName like '%.%' then SUBSTRING(@FullName, CHARINDEX('.', @FullName) + 1, LEN(@FullName)) end AS Rev




Thank you.
0
Comment
Question by:sxxgupta
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 18

Accepted Solution

by:
SimonAdept earned 500 total points
ID: 40509068
DECLARE @FullName   VARCHAR(100)
SET @FullName = '07000496_C.F2'

SELECT
            case when @FullName like '%_%' and @FullName not like '%.%' then SUBSTRING(@FullName, CHARINDEX('_', @FullName) + 1, LEN(@FullName)) 
             when @FullName like '%_%' and @FullName like '%.%' then SUBSTRING(@FullName, CHARINDEX('.', @FullName) + 1, LEN(@FullName)) end AS Rev

Open in new window


the syntax for each case is just to separate with ' when ', rather than 'end as Rev,'

For sanity, you should probably include an 'else' case as well.
SELECT
            case when @FullName like '%_%' and @FullName not like '%.%' then SUBSTRING(@FullName, CHARINDEX('_', @FullName) + 1, LEN(@FullName)) 
             when @FullName like '%_%' and @FullName like '%.%' then SUBSTRING(@FullName, CHARINDEX('.', @FullName) + 1, LEN(@FullName)) 
else
'n/a' 
end AS Rev

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40509070
CASE statements support multiple WHEN .. THEN statements, where SQL executions them sequentially until one WHEN expression evaluates to True, then returns what's in the THEN.

SELECT CASE
  WHEN @FullName like '%_%' and @FullName not like '%.%' 
      THEN SUBSTRING(@FullName, CHARINDEX('_', @FullName) + 1, LEN(@FullName))
  WHEN @FullName like '%_%' and @FullName like '%.%' 
      THEN SUBSTRING(@FullName, CHARINDEX('.', @FullName) + 1, LEN(@FullName)) 
  END AS Rev

Open in new window


I have an article out there called SQL Server CASE Statments that illustrates multiple WHEN..THEN, nested statements, and many other features of CASE statements.   If it helps please click the big green 'Was this article helpful?' button at the end of the article.
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40509075
You just need a CASE statement:
SELECT case 
            when @FullName like '%_%' and @FullName not like '%.%' then SUBSTRING(@FullName, CHARINDEX('_', @FullName) + 1, LEN(@FullName)) 
            when @FullName like '%_%' and @FullName like '%.%' then SUBSTRING(@FullName, CHARINDEX('.', @FullName) + 1, LEN(@FullName)) 
    end AS Rev

Open in new window

0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40509082
Damn. I knew I should refresh the page before clicking in the Submit button!
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 18

Expert Comment

by:SimonAdept
ID: 40509085
Also underscore is a wildcard character. You should use [_] to represent the literal.
0
 

Author Closing Comment

by:sxxgupta
ID: 40509087
Thank you everyone.  Much appreciated.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40509088
<Frazier Crane>  When you know the answer it's hard not to hit the button..
0
 
LVL 18

Expert Comment

by:SimonAdept
ID: 40509094
Final version with [ and ] around the underscore (otherwise LIKE treats it as a wildcard representing any single character):
SELECT
            case when @FullName like '%[_]%' and @FullName not like '%.%' then SUBSTRING(@FullName, CHARINDEX('_', @FullName) + 1, LEN(@FullName)) 
             when @FullName like '%[_]%' and @FullName like '%.%' then SUBSTRING(@FullName, CHARINDEX('.', @FullName) + 1, LEN(@FullName)) 
else  -- if you omit this you would get NULL by default for unmatched cases
'n/a' 
end AS Rev

Open in new window


MSDN reference for the LIKE operator
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now