Solved

SQL IF / ELSE IF

Posted on 2013-01-04
5
595 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

840 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