我在列中出现了几次差异字符串,例如
'dsasdasdsd'+'ewewewew'+'45454545'+(avg('uuuuuuu'))
我需要将此字符串拆分为多个列,其中的子字符串介于两列之间 aphostropes
像这样:Column 1 = dsasdasdsd
Column 2 = ewewewew
Column 3 = 45454545
Column 4 = uuuuuuu
外观数量是随机的,原始列的长度也不固定(从50个字符到> 1000个)
答案 0 :(得分:2)
DECLARE @InStr VarChar(1000) = '''dsasdasdsd''+''ewewewew''+''45454545''+(avg(''uuuuuuu''))'''
DECLARE @intStart INT = 0
DECLARE @intEnd INT = 1
DECLARE @ColNo INT = 1
DECLARE @MyString VARCHAR(2000)
DECLARE @SelectString VARCHAR(8000) = 'SELECT '
WHILE(@intStart < LEN(@InStr) )
BEGIN
SELECT @intStart = CHARINDEX(CHAR(39), @InStr, 0) + 1
SELECT @intEnd = CHARINDEX(CHAR(39), @InStr, @intStart)
SELECT @SelectString = @SelectString + CHAR(39) + SUBSTRING(@InStr, @intStart, @intEnd - @intStart) + CHAR(39) + ' As [Column ' + CAST(@ColNo As Varchar) + '],'
SELECT @InStr = SUBSTRING(@InStr, @intEnd + 1, LEN(@InStr)-@intEnd )
SET @ColNo = @ColNo +1
END
SELECT @SelectString = LEFT(@SelectString, Len(@SelectString) -1)
EXEC (@SelectString)
我一直在玩这个并且这确实有效,但不幸的是我现在没有时间继续使用它但也许你可以改进这个?
HTH
答案 1 :(得分:2)
你可以试试这个:
create table tSqlStrings (sText nvarchar(1000))
insert tSqlStrings values('''dsasdasdsd''+''ewewewew''+''45454545''+(avg(''uuuuuuu''))')
create table tResults (
sColumn1 nvarchar(1000)
,sColumn2 nvarchar(1000)
,sColumn3 nvarchar(1000)
,sColumn4 nvarchar(1000)
)
和
DELETE tResults
DECLARE @sText nvarchar(1000) = (
SELECT
sText
FROM
tSqlStrings
)
DECLARE @lBegin int = CHARINDEX('''',@sText)
DECLARE @lEnd int = charindex('''',
substring(@sText,
CHARINDEX('''',@sText)+1,
len(@sText)))
DECLARE @sText0 nvarchar(1000)
DECLARE @sColumn1 nvarchar(1000)
DECLARE @sColumn2 nvarchar(1000)
DECLARE @sColumn3 nvarchar(1000)
DECLARE @sColumn4 nvarchar(1000)
DECLARE @iCnt int = 1
while @iCnt<=4
--(0<len(@sText) and 0<@lBegin and 0<@lEnd)
BEGIN
SET @sText0 = substring(@sText,@lBegin+1,@lEnd-2)
IF @iCnt=1 begin SET @sColumn1=@sText0 end
IF @iCnt=2 begin SET @sColumn2=@sText0 end
IF @iCnt=3 begin SET @sColumn3=@sText0 end
IF @iCnt=4 begin SET @sColumn4=@sText0 end
set @sText = substring(@sText,@lBegin + @lEnd+2,len(@sText))
SET @lBegin = CHARINDEX('''',@sText)
SET @lEnd = charindex('''',
substring(@sText,
CHARINDEX('''',@sText)+1,
len(@sText)))
SET @iCnt = @iCnt+1
END
INSERT
tResults (sColumn1,sColumn2,sColumn3,sColumn4)
VALUES (@sColumn1,@sColumn2,@sColumn3,@sColumn4)
SELECT * FROM tResults
on sql fiddle
答案 2 :(得分:1)
您将能够使用CHARINDEX()和SUBSTRING()实现此目的 以下示例显示了拆分为2列。当它有更多列时,查询将变得更加复杂。但是,您可以按照此操作构建查询。
SELECT OriginalColumn
, SUBSTRING(OriginalColumn, 1,CHARINDEX('x',OriginalColumn,1)-1) AS Column1
, SUBSTRING(OriginalColumn, CHARINDEX('x',OriginalColumn,1) + 1 ,CHARINDEX('x',OriginalColumn,CHARINDEX('x',OriginalColumn,1)-1)) AS Column2
FROM YourTable
我在示例中使用了“x”作为分隔符。以下是样本结果
答案 3 :(得分:1)
试试这个:
declare @delim char
set @delim = ''''
declare @str nvarchar(max)
declare @substr nvarchar(max)
declare @newstr nvarchar(max)
declare @tmpTable table (partStrings nvarchar(max))
declare @count int
set @count = 0
select @str = <***Your String***>
while(charindex(@delim,@str) != 0)
begin
set @count = @count + 1
Select @substr = substring(@str,1,charindex(@delim,@str)-1)
if((@count % 2) = 0)
begin
insert into @tmpTable values(@substr)
end
Set @newstr = substring(@str,charindex(@delim,@str)+1,len(@str)-charindex(@delim,@str))
set @str = @newstr
end
select partStrings from @tmpTable