如何在表格的特定列中编写用于交换单词的字母位置的查询?
示例:
HAPPY -> PHAPY
TIRED -> RTIED
我该怎么做?
答案 0 :(得分:1)
试试这个:
Declare @YourText NVARCHAR(100) = 'HAPPY'
Select Substring(@yourText,3,1)+STUFF(@YourText,3,1,'')
答案 1 :(得分:0)
如果您想随机播放字词,可以尝试以下查询。您可以将其设为UDF。
DECLARE @Word VARCHAR(20) = 'HAPPY'
DECLARE @NewWord VARCHAR(20) = ''
DECLARE @Count INT = 1
DECLARE @Pos INT
WHILE LEN(@Word) > 0
BEGIN
SET @Pos = CAST(RAND()*10 AS INT) % LEN(@Word) + 1
SET @NewWord = @NewWord + ISNULL(SUBSTRING(@Word, @Pos, 1),'')
SET @Word = STUFF(@Word, @Pos, 1, '')
SET @Count = @Count + 1
END
SELECT @NewWord AS NewWord