Solved

sql script help in using case statment with a select statement

Posted on 2014-12-19
8
198 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:
Simon 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 46

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 46

Expert Comment

by:Vitor Montalvão
ID: 40509082
Damn. I knew I should refresh the page before clicking in the Submit button!
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 18

Expert Comment

by:Simon
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:Simon
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

16 Experts available now in Live!

Get 1:1 Help Now