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.
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' ENDFROM cust_part |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-11-11 : 11:08:18
|
select replace('SSTTC30755', 'SSTTC', 'SS') |
|
|
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. |
|
|
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_CODEENDFROM cust_partTara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
|
|
|