Solved

SQL IF / ELSE IF

Posted on 2013-01-04
5
591 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
  • 2
  • 2
5 Comments
 
LVL 32

Accepted Solution

by:
ewangoya earned 250 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 250 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:ewangoya
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

809 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