0

I have a string like this "Planck, Albert, Bohr"

I want the output as "Bohr Albert Planck"

I want this done using SQL server. I saw many string split functions all return the data as a table and in the forward order. These don't server my purpose.

Any pointers will be helpful.

4

3 に答える 3

2

このような分割文字列関数を使用すると、次のようなことができます。

declare @S varchar(20) = 'Planck, Albert, Bohr'

select
(
  select ltrim(s)+' '
  from dbo.Split(',', @S)
  order by pn desc
  for xml path(''), type
).value('.', 'varchar(max)')

SQL フィドル

于 2012-09-01T07:40:53.457 に答える
2

これはあなたが望むものです:

DECLARE @source VARCHAR(MAX)
DECLARE @dest VARCHAR(MAX)
DECLARE @lenght INT 

SET @source = 'Planck, Albert, Bohr'
SET @dest = ''

WHILE LEN(@source) > 0
BEGIN
    IF CHARINDEX(' ', @source) > 0
    BEGIN
        SET @dest = SUBSTRING(@source,0,CHARINDEX(' ', @source)) + ' ' + @dest
        SET @source = LTRIM(RTRIM(SUBSTRING(@source,CHARINDEX(' ', 
                      @source)+1,LEN(@source))))
    END
    ELSE
    BEGIN
        SET @dest = @source + ' ' + @dest
        SET @source = ''
    END
END

SELECT REPLACE(REPLACE(@dest,Char(44),''),Char(13), '')

Char(44)のASCII値な,ので、ついにその文字を置き換えています。

これは印刷されますBohr Albert Planck

于 2012-09-01T07:01:26.293 に答える
0
Declare @inStr varchar(1000)='Planck, Albert, Bohr'
;WITH CTE AS(
select ltrim(rtrim(reverse(left(reverse(@instr),CHARINDEX(',',reverse(@instr),1)-1)))) as strg,RIGHT(reverse(@instr),LEN(reverse(@instr))-CHARINDEX(',',reverse(@instr),1)) as rem
union all
select CASE WHEN CHARINDEX(',',c.rem,1)>0 then ltrim(rtrim(reverse(left(rem,CHARINDEX(',',rem,1)-1))))  else reverse(rem) end,
       CASE WHEN CHARINDEX(',',c.rem,1)>0  then RIGHT(c.rem,LEN(rem)-CHARINDEX(',',rem,1)) else '' end
from CTE c
where len(rem) > 0 --CHARINDEX(',',c.rem,1)>0

)
select stuff((select ' '+strg from CTE for xml path('')),1,1,'')
于 2012-09-03T07:18:27.310 に答える