如何在SQL SERVER中逐字反转字符串

时间:2017-05-30 03:41:07

标签: mysql sql sql-server

您好我正在尝试逐字翻译,假设我有输入

  • I / P:'我的名字是sachin'
  • O / P:' sachin名为My'

我创建了这段代码,但它一直在运行:

declare @I varchar(20)
declare @O varchar(20)
declare @T varchar(20)
set @I='My Name is sachin'
set @O=''
while CHARINDEX(' ',@I)>0
begin
set @T=substring(REVERSE(@I),1,charindex(' ',REVERSE(@I))-1)
set @O=@O+' '+REVERSE(@T)
set @I=reverse(STUFF(REVERSE(@I),1,CHARINDEX(' ',REVERSE(@I))-1,''))
end

任何人都可以帮助我吗?

5 个答案:

答案 0 :(得分:0)

在SQL Server中 1)分割从http://www.sqlservercentral.com/blogs/querying-microsoft-sql-server/2013/09/19/how-to-split-a-string-by-delimited-char-in-sql-server/复制的字符串的功能  
2)将拆分字符串填充为反向字符串

CREATE FUNCTION [dbo].[usr_funSplitString] 
( 
    @string NVARCHAR(MAX), 
    @delimiter CHAR(1) 
) 
RETURNS @output TABLE(splitdata NVARCHAR(MAX), pos smallint identity(1,1)   -- added position field
) 
BEGIN 
    DECLARE @start INT, @end INT 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
    WHILE @start < LEN(@string) + 1 BEGIN 
        IF @end = 0  
            SET @end = LEN(@string) + 1

        INSERT INTO @output (splitdata)  
        VALUES(SUBSTRING(@string, @start, @end - @start)) 
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)

    END 
    RETURN 
END


select result = stuff((select ' ' + splitdata from dbo.[usr_funSplitString]('My Name is sachin',' ') order by pos desc
FOR XML PATH ('')), 1, 1, '')

答案 1 :(得分:0)

稍微改变您的查询,试试这个。

 declare @I varchar(20)
 declare @O varchar(20)
 declare @T varchar(20)
 set @I='My Name is sachin'
 set @O=''
 while CHARINDEX(' ',@I)>0
 begin
     set @T=left(reverse(@i),charindex(' ',reverse(@i))-1)
     set @O=@O+' '+REVERSE(@T)
     set @I=left(@i,len(@i)-(1+len(@t)))
     If CHARINDEX(' ',@I)=0
        set @O=@O+' '+@i
     select @o
 end

'如果'块'被添加以附加最后一个单词,因为'空格'将无法附加句子中的最后一个单词。

答案 2 :(得分:0)

在这里,您需要自定义拆分字符串UDF。或者您需要拆分字符串UDF,它返回行号。

所以如果你创建了UDF,请不要使用master..spt_values.use数字表

试试这个,

declare @t table(col1  varchar(50))
insert into @t values ('My Name is sachin')
declare @Delimiter as varchar(10)=' '

;

WITH CTE
AS (
    SELECT col1
        ,ca.number
    FROM @t t
    CROSS APPLY (
        SELECT 1 number

        UNION ALL

        SELECT DISTINCT number + 1
        FROM master..spt_values
        WHERE number > 0
            AND (SUBSTRING(col1, number, 1) = @Delimiter)
            AND number <= len(col1)
        ) ca
    )
    ,cteLen (
    N1
    ,col1
    ,L1
    )
AS (
    SELECT s.number
        ,col1
        ,ISNULL(NULLIF(CHARINDEX(@Delimiter, col1, s.number), 0) - s.number, 4000)
    FROM cte s
    )
    ,CTE1
AS (
    SELECT ItemNumber = ROW_NUMBER() OVER (
            ORDER BY l.N1
            )
        ,n1
        ,l1
        ,Item = SUBSTRING(col1, l.N1, l.L1)
    FROM cteLen l
    )
SELECT TOP 1 (
        SELECT @Delimiter + item
        FROM cte1
        ORDER BY itemnumber DESC
        FOR XML path('')
        )
FROM cte1

答案 3 :(得分:0)

一个简单的解决方案(但远非完美)是使用sys.dm_fts_parser函数(参考here)因此:

SELECT LTRIM(
(SELECT N' ' + x.display_term
FROM sys.dm_fts_parser('"Your Name is Georgel", 0, 0, 0) AS x
ORDER BY x.occurence DESC
FOR XML PATH('')))

答案 4 :(得分:0)

已修改:


SELECT LTRIM(
(SELECT N' ' + x.display_term
FROM sys.dm_fts_parser('"Your Name is Georgel"', 0, 0, 0) AS x
ORDER BY x.occurrence DESC
FOR XML PATH('')))