views:

258

answers:

5

Hello All

I have a huge problem with the REPLACE SQL function in Microsoft SQL Server Management Studio Express.

When I do following query

SELECT     REPLACE('ArticleNumber', 'S401', 'I0010')
SELECT     REPLACE('ArticleNumber', 'S302', 'I0020')
SELECT     REPLACE('ArticleNumber', 'S303', 'I0030')    
SELECT     REPLACE('ArticleNumber', 'S304', 'I0040')    
SELECT     REPLACE('ArticleNumber', 'S305', 'I0050')    
SELECT     REPLACE('ArticleNumber', 'S306', 'I0060')    
FROM       tbl.Products

Then Studio Express Response with that SELECT wasn't recognised.

What am I doing wrong?

+3  A: 

Depending on what you are trying to do...

You need either a nested REPLACE...

SELECT
     REPLACE
          REPLACE
               REPLACE
                    REPLACE
                         REPLACE
                              REPLACE(ArticleNumber, 'S401', 'I0010'),
                         'S302', 'I0020'),
                    'S303', 'I0030'),
               'S304', 'I0040'),
          'S305', 'I0050'),
     'S306', 'I0060')    
FROM       tbl.Products

..or multiple output columns

SELECT
     REPLACE(ArticleNumber, 'S401', 'I0010'),
     REPLACE(ArticleNumber, 'S302', 'I0020'),
     REPLACE(ArticleNumber, 'S303', 'I0030'),   
     REPLACE(ArticleNumber, 'S304', 'I0040'),    
     REPLACE(ArticleNumber, 'S305', 'I0050'),    
     REPLACE(ArticleNumber, 'S306', 'I0060')  
FROM       tbl.Products

Note: the error comes from the database engine that tries to run this. SSMS is a glorified text editor with some useful extras..

gbn
Thanks for your detailed response, I tried the second code in your post. But when I let Studio Express run the code it will hang an "AS Expr" after each closed exclamation mark with up counting numbers. The output I get is a bunch of columns each named "expr" with up counting numbers. Each column holds the same original table just that one value has been altered. when I go into normal view nothing has been altered. I just want that all the old values get replaced by new ones. Do you now maybe how I can achieve this result?
elhombre
+1  A: 

maybe i jsut dont get what you are trying to do but from what i can read here you are trying to replace 'S401' in the string 'ArticleNumber' by 'I0010' so the select result will always be 'ArticleNumber' .... and the

FROM tbl.Products is useless

if u want to replace the value of the field ArticleNumber from table produtcts then you should try ArticleNumber with no quote jsut refer to GBN answer for the exact syntax

Lil'Monkey
+3  A: 

Should articlenumber be in quotes? Your replace is not going to replace anything as S401, S302, etc... are definitely not in 'ArticleNumber.'

You'll want to follow gbn's advice of using a nested statement, but with the actual column.

SELECT
 REPLACE(
      REPLACE(
           REPLACE(
                REPLACE(
                     REPLACE(
                          REPLACE(ArticleNumber, 'S401', 'I0010'),
                     'S302', 'I0020'),
                'S303', 'I0030'),
           'S304', 'I0040'),
      'S305', 'I0050'),
 'S306', 'I0060')
FROM tbl.Products

Kris

KSimons
good point, I corrected my answer
gbn
+1  A: 

This is a slightly different way of doing it.

SELECT COALESCE(ch.New, pr.ArticleNumber) AS ArticleNumber
FROM tbl.Products pr
   LEFT JOIN (
   SELECT 'S401' AS Old, 'I0010' as New
   UNION
   SELECT 'S302' AS Old, 'I0020' AS New
   UNION ... etc
) ch ON ch.Old = pr.ArticleNumber
Darrel Miller
ON CHARINDEX(ch.Old,pr.ArticleNumber) > 0? Then add a DISTINCT?
Peter
@Peter I'm afraid you lost me with that suggestion. What problems does it solve?
Darrel Miller
If the article number is something like Q45-S401, REPLACE() would return Q45-I0010. CHARINDEX() in the JOIN plus DISTINCT REPLACE() in the SELECT would also return Q45-I0010. JOIN...COALESCE would return the original value, Q45-S401.
Peter
This will not work at all unless ArticleNumber is *exactly* "old" and you want to replace the whole ArticleNumber with "new". REPLACE searches ArticleNumber and replaces all contained sequences of "Old" with "New"
gbn
@gbn Yes, you are right. It was kind of hard to figure out the original poster's intent. I assumed that he was replacing old numbers with new numbers.
Darrel Miller
@Darren Miller Yes that's what I am up to, replacing old numbers with new ones
elhombre
+1  A: 

Remove the quotes around ArticleNumber and nest the REPLACEs:

SELECT  
  ArticleNumber = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(ArticleNumber 
    , 'S401', 'I0010')    
    , 'S302', 'I0020')    
    , 'S303', 'I0030')    
    , 'S304', 'I0040')    
    , 'S305', 'I0050')    
    , 'S306', 'I0060')    

FROM tbl.Products
Peter