大约2年前,写过一篇《字符串中去除多余的空格保留一个(c#)》
今天,insus.net使用ms sql server来实现相同的功能。现insus.net已经把它写成一个自定义函数。
set ansi_nulls on go set quoted_identifier on go -- ============================================= -- author: insus.net -- create date: 2019-05-13 -- update date: 2019-05-13 -- description: 保留一个空格 -- ============================================= create function [dbo].[svf_leave_a_space] ( @inputstring nvarchar(max) ) returns nvarchar(max) as begin declare @index int = 1 declare @currentchar char(1) declare @previouschar char(1) declare @str nvarchar(max) = n'' while @index <= len(@inputstring) begin set @currentchar = substring(@inputstring, @index, 1) set @previouschar = substring(@inputstring, @index - 1, 1) if @currentchar != ' ' or @currentchar != @previouschar set @str = @str + cast(@currentchar as nvarchar(max)) set @index = @index + 1 end return @str end go
使用一个例子来说明: