Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Query HELP - CASE & REPLACE

Author  Topic 

pknox
Starting Member

2 Posts

Posted - 2014-11-11 : 10:42:21
We are trying to write a query in MS SQL 2012 with a CASE statement per the below example. If the SSTTC30755 is in the table, it will return the customer # listed in the Cust_Part column. If not, it returns the original part number of SSTTC30755.
We also want to replace the 'SSTTC' with 'SS', within the same query. If the part number returned from the CASE statement contains 'SSTTC', I want to replace with 'SS'. How do we write this query???? Thanks in advance!

SELECT DISTINCT CASE WHEN PART_CODE = 'SSTTC30755'
THEN CUST_PART
ELSE 'SSTTC30755'
END
FROM cust_part

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-11-11 : 11:08:18
select replace('SSTTC30755', 'SSTTC', 'SS')
Go to Top of Page

pknox
Starting Member

2 Posts

Posted - 2014-11-11 : 12:51:03
Thanks for the feedback, but I'm still unclear how to do the CASE statement and REPLACE statement within the same query. Any insight would be appreciated.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-11-11 : 13:05:57
SELECT DISTINCT CASE WHEN PART_CODE = 'SSTTC30755'
THEN REPLACE(CUST_PART, 'SSTTC', 'SS')
ELSE PART_CODE
END
FROM cust_part

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -