在MS SQL中转换ascii代码

时间:2016-10-05 04:38:27

标签: sql sql-server tsql ascii shift

我想在列中移动(添加)4个值。基本上每个字符的ASCII数加4。 F.e:

'ABCD' -> 'EFGH'
'1234' -> '5678'
'A1B2' -> 'E5F6'

我可以将所有字符更改为十六进制代码,

SELECT CONVERT(VARBINARY(MAX), CONVERT(VARCHAR(MAX), Column1))
FROM Table1

但我不知道如何为每个角色添加4个。

查询可以吗?

样品:

Z -> ^
z -> ~
W -> [
9 -> =

CHAR(ASCII('z') + 4) = ~

这样的东西

3 个答案:

答案 0 :(得分:1)

这件事可以使用函数(UDF)

来完成

样本

create  function Charchange(@inputstring varchar(max))
returns varchar(max)
AS
BEGIN
  DECLARE @i int, 
          @Results varchar(max)
  SET @Results=''
  SET @i = 1
  WHILE @i <= DATALENGTH(@inputstring)
    BEGIN
           SET @Results =  @Results + char(ASCII(SUBSTRING(@inputstring,@i,1))+4)

      SET @i=@i + 1
    END
RETURN @Results
END


select dbo.Charchange('abc123xyz')

 or 

  select dbo.Charchange(coulmn1) FROM Table1

答案 1 :(得分:1)

-- Sample table
declare @T table(S varchar(10));

-- Sample data
insert into @T(S) values ('ABCD'),('1234'),('A1B2'),('WXYZ');

-- Split the string using a numbers table and
-- Rebuild the string with for xml path
select T.S, 
       (
       select char(ascii(substring(T.S, N.N, 1)) + 4)
       from dbo.Number as N
       where N.N >= 1 and 
             N.N <= len(T.S) 
       order by N.N
       for xml path(''),  type
       ).value('text()[1]', 'varchar(10)')
from @T as T;

SQL, Auxiliary table of numbers

Concatenate many rows into a single text string

答案 2 :(得分:0)

我得到了递归CTE和FOR XML PATH的解决方案:

DECLARE @n int = 4 --Add 4 for each ASCII
--Here I simulate your table, hope you have ids in it
-- because I used to join it with values
;WITH YourTable AS (
SELECT  CAST(SomeString as nvarchar(max)) as SomeString
FROM (VALUES ('ABCD'),('1234'),('A1B2'),('WXYZ')
) as t(SomeString)
), cte AS (--Here we replace chars
SELECT  CHAR(ASCII(SUBSTRING(SomeString,1,1))+@n) as d,
        1 as [level],
        LEN(SomeString) as l,
        SomeString as OrigString
FROM YourTable
UNION ALL
SELECT  CHAR(ASCII(SUBSTRING(OrigString,[level]+1,1))+@n),
        [level]+1,
        l,
        OrigString
FROM cte
WHERE l >= [level]+1)
--Final output
SELECT DISTINCT c.OrigString,
                (SELECT d+''
                FROM cte
                WHERE c.OrigString = OrigString
                FOR XML PATH('')
                ) as NewString
FROM cte c
OPTION (MAXRECURSION 0)

输出:

OrigString  NewString
1234        5678
A1B2        E5F6
ABCD        EFGH
WXYZ        [\]^

修改

使用VARBINARY(MAX)转换的解决方案:

DECLARE @x xml,
        @n int = 4 --Add 4 for each ASCII
--Here I simulate your table, hope you have ids in it
-- because I used to join it with values
;WITH YourTable AS (
SELECT  CAST(SomeString as nvarchar(max)) as SomeString
FROM (VALUES ('ABCD'),('1234'),('A1B2'),('WXYZ')
) as t(SomeString)
)
SELECT @x = (
    SELECT CAST('<row str="'+SomeString+'"><p>'+REPLACE(REPLACE(CONVERT(nvarchar(max),CONVERT(VARBINARY(MAX),SomeString),1),'0x',''),'00','</p><p>')+'</p></row>' as xml)
    FROM YourTable
    FOR XML PATH('')
)
;WITH cte AS(
SELECT  t.c.value('../@str','nvarchar(max)') as OrigString,
        CHAR(CAST(CONVERT(VARBINARY(2),'0x'+t.c.value('.','nvarchar(2)'),1) as int)+@n) as NewValues
FROM @x.nodes('/row/p') as t(c)
)

SELECT  DISTINCT 
                c.OrigString,
                LEFT((
                SELECT NewValues +''
                FROM cte
                WHERE OrigString = c.OrigString
                FOR XML PATH('')
                ),LEN(c.OrigString)) as NewString
FROM cte c

相同的输出。