0

The table project contains

projectid|description|statuscode|path|....

I have a SQL clause which should return the following (pseudocode):

SELECT ...,  
     CASE   
     WHEN statusCode IN ('C', 'N', 'F') 
       IF (path contains 'CLOSED PROJECTS')
       THEN   
        REPLACE(path,'CLOSED PROJECTS','DEAD PROJECTS') 
       ELSE
        REPLACE(path,'ACTIVE PROJECTS','DEAD PROJECTS') 
,
.....
from Project where projectId=..... 

How do I implement this 'conditional' REPLACE?

I am using SQL Server 2008.

1 Answer 1

3

May be like this..

SELECT ...,  
 CASE   
 WHEN statusCode IN ('C', 'N', 'F') 
   and path like '%CLOSED PROJECTS%'
   THEN   
    REPLACE(path,'CLOSED PROJECTS','DEAD PROJECTS') 
   ELSE
    REPLACE(path,'ACTIVE PROJECTS','DEAD PROJECTS') 
,
.....
from Project where projectId=..... 
Sign up to request clarification or add additional context in comments.

5 Comments

I think the contains predicate can only be used in the WHERE clause, so only the latter solution works.
@NickyvV - Yes you are right. Removed it from the answer
Just to make sure, this can be understood in two ways. One: You want to replace CLOSED projects on rows where statusCode is C/N/F, and replace ACTIVE projects on all other rows regardless of statusCode (which is what the answer above does). Two: You you only want to do ANY kind of replacing on rows where statusCode is C/N/F, but what you replace path with, depends on whether it contains CLOSED or not. There is a difference. :)
@Kahn I agree with you and even i had the same question but by looking at the question and pseudo-code he/she just want to add the condition before replacing. I can't sit and assume things!!
@Pradeep no that wasn't criticism, your answer is solid. I just noticed this question and wanted to make sure we're interpreting this the way OP wanted it to. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.