Friday, September 6, 2013

Translate Equivalent in Sqlserver

translate function in Sqlserver

source - http://www.dbforums.com/microsoft-sql-server/1216565-oracle-translate-function-equivalent-sql-server.html

CREATE FUNCTION dbo.Thicky_Pants 
 (
 @Input AS VarChar(1000),
 @Find AS VarChar(100),
 @Replace AS VarChar(100)
 )
 RETURNS VarChar(1000)
AS
BEGIN
 
 DECLARE @i AS TinyInt
 
 SELECT @i = 1
 
 WHILE @i <= LEN(@Find) BEGIN
  
  SELECT @Input = REPLACE(@Input, SUBSTRING(@Find, @i, 1), SUBSTRING(@Replace, @i, 1))
  SELECT @i = @i + 1
 
 END
 RETURN @Input
END
GO
 
DECLARE @String AS VarChar(1000)
 
SELECT @String = 'pootle_flump'
SELECT @String = dbo.Thicky_Pants(@String, 'pt', 'xz')
 
PRINT  @String

No comments:

Post a Comment

web stats