Solved

SQL IF / ELSE IF

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

762 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

26 Experts available now in Live!

Get 1:1 Help Now