Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (2.6k points)

I read that there is a function equivalent to the standard function TRANSLATE under DB2 under SQL Server 2017. But how to do under earlier versions?

1 Answer

0 votes
by (7.2k points)

I learned that there is a functional equivalent to the regular function TRANSLATE under DB2 under SQL Server 2017.  And We can easily use the TRANSLATE function in SQL SERVER. 

Using below code:

DECLARE

  @string varchar(7000)  = 'xyz',

  @fromChar varchar(200) = 'xyz', -- note: no mutation

  @toChar varchar(100)   = 'yza';

SELECT newString = 

(

  SELECT CASE WHEN x>z THEN '' WHEN x>0 THEN s ELSE t END+''

  FROM dbo.ngrams8k(@string,1) ng

  CROSS APPLY (VALUES (charindex(ng.token,@fromChar),len(@toChar),ng.token)) x(x,z,t)

  CROSS APPLY (VALUES (ng.position, substring(@toChar,x.x,1))) xx(p,s)

  ORDER BY xx.p

  FOR XML PATH(''), TYPE

).value('(text())[1]', 'varchar(8000)');

Returns > yza

Related questions

0 votes
1 answer
asked Jan 17, 2021 in SQL by dev_sk2311 (45k points)
0 votes
1 answer
asked Dec 30, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
asked Dec 5, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer

Browse Categories

...