• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 608
  • Last Modified:

SQL IF / ELSE IF

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
CipherIS
Asked:
CipherIS
  • 2
  • 2
2 Solutions
 
Ephraim WangoyaCommented:
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
 
Simone BCommented:
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
 
CipherISAuthor Commented:
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
 
Ephraim WangoyaCommented:
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
 
CipherISAuthor Commented:
Figured it out - thx
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now