15

I have a problem where I want to replace characters

I am using replace function but that is not giving desired output.

Values of column table_value needs to replaced with their fill names like

E - Email
P - Phone
M - Meeting

enter image description here

I am using this query

select table_value, 
       replace(replace(replace(table_value, 'M', 'MEETING'), 'E', 'EMAIL'), 'P', 'PHONE') required_value 
from foobar

so second required_value row should be EMAIL,PHONE,MEETING and so on.

What should I do so that required value is correct?

3
  • You should probably split the table values into E, P and M, then replace and put back together. Commented Mar 19, 2014 at 5:20
  • @Szymon: How to do that? Commented Mar 19, 2014 at 5:22
  • Do you have some kind of unique id for each record as well? Commented Mar 19, 2014 at 5:29

4 Answers 4

35

The below will work (even it's not a smart solution).

select 
    table_value, 
    replace(replace(replace(replace(table_value, 'M', 'MXXTING'), 'E', 'XMAIL'), 'P', 'PHONX'), 'X', 'E') required_value 
from foobar
Sign up to request clarification or add additional context in comments.

Comments

2

You can do it using CTE to split the table values into E, P and M, then replace and put back together.

I assumed each record has a unique identifer Id but please replace that with whatever you have.

;WITH cte
AS
(
    SELECT Id, SUBSTRING(table_value, 1, 1) AS SingleValue, 1 AS ValueIndex
    FROM replacetable

    UNION ALL

    SELECT replacetable.Id, SUBSTRING(replacetable.table_value, cte.ValueIndex + 1, 1) AS SingleValue, cte.ValueIndex + 1 AS ValueIndex
    FROM cte
    INNER JOIN replacetable ON cte.ValueIndex < LEN(replacetable.table_value)
)

SELECT DISTINCT Id, 
    STUFF((SELECT DISTINCT ','+ CASE SingleValue 
            WHEN 'E' THEN 'EMAIL'
            WHEN 'P' THEN 'PHONE'
            WHEN 'M' THEN 'MEETING'
        END
       FROM cte c 
       WHERE c.Id = cte.Id 
       AND SingleValue <> ','
       FOR XML PATH ('')),1,1,'')
FROM cte

3 Comments

Your query gives me an error - Invalid object name 'replacetable'.
Because you didn't replace it with the actual table name?
What does the replacetable have to be like ?
1

Sorry , for mess code, maybe this is not best way to solve this, but what I've tried:

SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
GO
CREATE Function [dbo].[fn_CSVToTable] 
(
    @CSVList Varchar(max)
)
RETURNS @Table TABLE (ColumnData VARCHAR(100))
AS
BEGIN
    DECLARE @S varchar(max),
        @Split char(1),
        @X xml

    SELECT @Split = ','

    SELECT @X = CONVERT(xml,' <root> <s>' + REPLACE(@CSVList,@Split,'</s> <s>') + '</s>   </root> ')

    INSERT INTO @Table
    SELECT CASE  RTRIM(LTRIM(T.c.value('.','varchar(20)'))) WHEN 'M' THEN 'Meeting'
        WHEN 'P' THEN 'Phone'
        WHEN 'E' THEN 'Email'
        End

    FROM @X.nodes('/root/s') T(c)

    RETURN
END
GO

Then When I run this:

Select Main.table_value,
       Left(Main.ColumnData,Len(Main.ColumnData)-1) As ColumnData
From
    (
        Select distinct tt2.table_value, 
            (
                Select tt1.ColumnData+ ',' AS [text()]
                From (
                SELECT 
                    *
                    FROM  dbo.TestTable tt
                    CROSS APPLY dbo.fn_CSVToTable(tt.table_value)
                ) tt1
                Where tt1.table_value = tt2.TABLE_value
                ORDER BY tt1.table_value
                For XML PATH ('')
            ) ColumnData
        From dbo.TestTable tt2
    ) [Main]

I get this:

table_value                                        ColumnData
-------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
E,P                                                Email,Phone
E,P,M                                              Email,Phone,Meeting
P,E                                                Phone,Email
P,M                                                Phone,Meeting

(4 row(s) affected)

Comments

0

You could also use a DECODE or CASE to translate the values.

Comments

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.