如何将字符串数据“Test123”拆分为“Test”和“123”两列。
提前致谢。
我为此创建了以下函数,如果有其他简单的方法,请在此处更新
/*
SELECT * FROM [dbo].[SPLIT_NUMERIC_STRING] ('TEST1235745DFGBFVNYT HGEFNEYGV @ADBSD H# CHSC5ae23674523')
*/
CREATE FUNCTION [dbo].[SPLIT_NUMERIC_STRING]
( @ColumnName VARCHAR(MAX)
)
RETURNS @TABLEOFVALUES TABLE
( NUMERICDATA VARCHAR(MAX),
STRINGDATA VARCHAR(MAX)
)
AS
BEGIN
DECLARE @Column1 VARCHAR(100)
DECLARE @Column2 VARCHAR(100)
DECLARE @StartPosition INT
DECLARE @String VARCHAR(100)
DECLARE @NUMBER INT
DECLARE @ENDPosition INT
SET @Column1 = ''
SET @Column2 = ''
SET @StartPosition = 1
SELECT @ENDPosition = LEN(@ColumnName)
WHILE @ENDPosition > 0
BEGIN
SELECT @String = SUBSTRING(@ColumnName,@StartPosition,1)
SELECT @NUMBER = ISNUMERIC(@String)
IF (@NUMBER = 1)
BEGIN
SET @Column1 = @Column1 + @String
END
ELSE
SET @Column2 = @Column2 + @String
SET @StartPosition = @StartPosition + 1
SET @ENDPosition = @ENDPosition - 1
END
INSERT INTO @TABLEOFVALUES (NUMERICDATA,STRINGDATA)
SELECT @Column1 AS NUMERICDATA,@Column2 AS STRINGDATA
RETURN
END
一些想法: