?
Solved

SQL IF / ELSE IF

Posted on 2013-01-04
5
Medium Priority
?
603 Views
Last Modified: 2013-01-04
I need to use an IF / ELSE IF in SQL.  Below is my example I am trying to accomplish

Select ID,  F1,
    IF CONTAINS( F2, 'Check')
        BEGIN
            Replace(F2, 'Check', '')
        END
    ELSE IF CONTAINS( F2, 'Ck')
        BEGIN
            Replace(F2, 'Ck', '')
        END
    END IF AS MyField

Open in new window

0
Comment
Question by:CipherIS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 1000 total points
ID: 38744245
use this

Select ID,  F1,
    case 
		when F2 LIKE '%Check%' then
			Replace(F2, 'Check', '')
		when F2 LIKE '%Ck%' then
			Replace(F2, 'Ck', '')
		else
			F2
    END AS MyField

Open in new window

0
 
LVL 11

Assisted Solution

by:Simone B
Simone B earned 1000 total points
ID: 38744259
Either one of these will give you what you're looking for:

SELECT ID, F1,
      CASE WHEN F2 = 'Check' THEN REPLACE(F2,'Check','')
            WHEN F2 = 'Ck' THEN REPLACE(F2,'Ck','')
                                ELSE F2
      END AS MyField
      
SELECT ID, F1,
      REPLACE(REPLACE(F2,'Check',''),'Ck','') AS MyField
0
 
LVL 1

Author Comment

by:CipherIS
ID: 38744294
Ok, it works but having one problem.  F2 can contain 'Ck#' and can't seem to delete the #.

Select ID,  F1,
    case 
		when F2 LIKE '%Check%' then
			Replace(F2, 'Check', '')
		when F2 LIKE '%Ck%' then
			Replace(F2, 'Ck', '')
		when F2 LIKE '%Ck#%' then
			Replace(F2, 'Ck#', '')
		else
			F2
    END AS MyField

Open in new window


Any idea how to resolve?
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 38744309
Put Ck# before Ck

Select ID,  F1,
    case 
		when F2 LIKE '%Check%' then
			Replace(F2, 'Check', '')
		when F2 LIKE '%Ck#%' then
			Replace(F2, 'Ck#', '')
		when F2 LIKE '%Ck%' then
			Replace(F2, 'Ck', '')
		else
			F2
    END AS MyField

Open in new window

0
 
LVL 1

Author Comment

by:CipherIS
ID: 38744311
Figured it out - thx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

777 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