使用sql中的函数替换空格 [英] Replace spaces using function in sql
本文介绍了使用sql中的函数替换空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在开发一个程序,我想编写一个接受以下格式值的函数
I'am currently developing a program and i want to write a function which is accept a value in following format
"AAAA BBBB" CCCC DDDD EEEE "FFFF GGGG HHHH"
我想用_"替换上面的空格并需要输出如下格式(请注意,这应该只发生在双引号内的字符串)
I want to replace the spaces in above with "_" and need a output as showed following format (please note that this should happen only for string which is inside double quotes)
"AAAA_BBBB" CCCC DDDD EEEE "FFFF_GGGG_HHHH"
谁能帮我解决这个问题
推荐答案
这是您可以根据需要添加到函数的逻辑.
Here's the logic for it you could add to a function as you wanted.
DECLARE @In VARCHAR(50) = '"AAAA BBBB" CCCC DDDD EEEE "FFFF GGGG HHHH"'
DECLARE @Quote SMALLINT = -1, @Index INT = 1, @Char CHAR(1)
WHILE @Index <= LEN(@In) BEGIN
SET @Char = SUBSTRING(@In, @Index, 1)
IF @Char = '"'
SET @Quote = @Quote * -1
IF @Char = ' ' AND @Quote > 0
SET @In = STUFF(@In, @Index, 1, '_')
SET @Index = @Index + 1
END
PRINT @In
输出
"AAAA_BBBB" CCCC DDDD EEEE "FFFF_GGGG_HHHH"
这篇关于使用sql中的函数替换空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文