两个分隔符之间的字符串 ('_')
String Between Two delimiters ('_')
我正在努力将字符串拆分为其基本组件。我已经弄清楚了第一部分并且工作正常;
SELECT(SUBSTRING(Field,0,CHARINDEX('_',Field,0))) AS POS1
我目前遇到的问题是第二和第三部分。整个字符串的格式是; character_character_character(其中每个字段都可以有不同数量的字符)。
SUBSTRING(Field, CHARINDEX('-',Field)+1, CHARINDEX('_',Field, CHARINDEX('_',Field)+1 - CHARINDEX('_',Field)-1)) AS POS2
这在某些情况下有效,但在其他情况下会被截断。我一直盯着这个看很久,以至于我对解决方案视而不见。
还有,准备去攻打三号位。
我们欢迎任何关于为什么这会以它应该的方式工作的建议。
如果它总是三个部分,你可以使用 replace
和 parsename
的技巧:
SELECT PARSENAME(val, 3) As col1,
PARSENAME(val, 2) As col2,
PARSENAME(val, 1) As col3
FROM Table
CROSS APPLY
(
SELECT REPLACE(Col, '_', '.') As val
) x
我之前用 REVERSE 对路径和文件做类似的事情 names.There 也是一些例子 Here。
DECLARE @full VARCHAR(MAX)
SET @full = 'c:\windows\system\sub-folder\somefile_file-stuff.qqq'
SELECT SUBSTRING(@full, 1, LEN(@full)-(CHARINDEX('\',REVERSE(@full))-1)) AS Path
试试这个,
我倾向于使用变量来获取位置和简单的过程
declare @field varchar(300) = 'character1_character2_character3'
declare @char1Pos int = CHARINDEX('_',@field,0)
--select @char1Pos
declare @char2Pos int = (CHARINDEX('_',@field)+@char1Pos + 1) -1
--select @char2Pos
select (SUBSTRING(@field,0,@char1Pos)) AS POS1,
SUBSTRING(@field, -- field
@char1Pos+ 1,--starting position for POS2
(@char2Pos -1) - @char1Pos) --ENDING POSITION FOR POS2
AS POS2,
substring(@field,-- field
((@char2Pos +1) ),--starting position for POS3
len(@field) - ((@char2Pos -1) - @char1Pos))--ENDING POSITION FOR POS2
as POS3
这应该有效:
SELECT Field, SUBSTRING(Field,0,CHARINDEX('_',Field,0)) AS POS1,
SUBSTRING(SUBSTRING(Field, CHARINDEX('_',Field)+1, LEN(Field)), 0, CHARINDEX('_',SUBSTRING(Field, CHARINDEX('_',Field)+1, LEN(Field)),0)) AS POS2 ,
SUBSTRING(Field, 3 + LEN(SUBSTRING(Field,0,CHARINDEX('_',Field,0))) + LEN(SUBSTRING(SUBSTRING(Field, CHARINDEX('_',Field)+1, LEN(Field)), 0, CHARINDEX('_',SUBSTRING(Field, CHARINDEX('_',Field)+1, LEN(Field)),0))), LEN(Field)) AS POS3
FROM YOUR_TABLE
最终工作代码;
SELECT (SUBSTRING([field],0,CHARINDEX('_',[field],0))) AS POS1
, SUBSTRING(SUBSTRING([field], CHARINDEX('_',[field])+1, LEN([field])), 0, CHARINDEX('_',SUBSTRING([field], CHARINDEX('_',[field])+1, LEN([field])),0)) AS POS2
, RIGHT([field],CHARINDEX('_',REVERSE([field]))-1) AS POS3
FROM TableName
谢谢大家,尤其是 Sorix。
我正在努力将字符串拆分为其基本组件。我已经弄清楚了第一部分并且工作正常;
SELECT(SUBSTRING(Field,0,CHARINDEX('_',Field,0))) AS POS1
我目前遇到的问题是第二和第三部分。整个字符串的格式是; character_character_character(其中每个字段都可以有不同数量的字符)。
SUBSTRING(Field, CHARINDEX('-',Field)+1, CHARINDEX('_',Field, CHARINDEX('_',Field)+1 - CHARINDEX('_',Field)-1)) AS POS2
这在某些情况下有效,但在其他情况下会被截断。我一直盯着这个看很久,以至于我对解决方案视而不见。
还有,准备去攻打三号位。
我们欢迎任何关于为什么这会以它应该的方式工作的建议。
如果它总是三个部分,你可以使用 replace
和 parsename
的技巧:
SELECT PARSENAME(val, 3) As col1,
PARSENAME(val, 2) As col2,
PARSENAME(val, 1) As col3
FROM Table
CROSS APPLY
(
SELECT REPLACE(Col, '_', '.') As val
) x
我之前用 REVERSE 对路径和文件做类似的事情 names.There 也是一些例子 Here。
DECLARE @full VARCHAR(MAX)
SET @full = 'c:\windows\system\sub-folder\somefile_file-stuff.qqq'
SELECT SUBSTRING(@full, 1, LEN(@full)-(CHARINDEX('\',REVERSE(@full))-1)) AS Path
试试这个, 我倾向于使用变量来获取位置和简单的过程
declare @field varchar(300) = 'character1_character2_character3'
declare @char1Pos int = CHARINDEX('_',@field,0)
--select @char1Pos
declare @char2Pos int = (CHARINDEX('_',@field)+@char1Pos + 1) -1
--select @char2Pos
select (SUBSTRING(@field,0,@char1Pos)) AS POS1,
SUBSTRING(@field, -- field
@char1Pos+ 1,--starting position for POS2
(@char2Pos -1) - @char1Pos) --ENDING POSITION FOR POS2
AS POS2,
substring(@field,-- field
((@char2Pos +1) ),--starting position for POS3
len(@field) - ((@char2Pos -1) - @char1Pos))--ENDING POSITION FOR POS2
as POS3
这应该有效:
SELECT Field, SUBSTRING(Field,0,CHARINDEX('_',Field,0)) AS POS1,
SUBSTRING(SUBSTRING(Field, CHARINDEX('_',Field)+1, LEN(Field)), 0, CHARINDEX('_',SUBSTRING(Field, CHARINDEX('_',Field)+1, LEN(Field)),0)) AS POS2 ,
SUBSTRING(Field, 3 + LEN(SUBSTRING(Field,0,CHARINDEX('_',Field,0))) + LEN(SUBSTRING(SUBSTRING(Field, CHARINDEX('_',Field)+1, LEN(Field)), 0, CHARINDEX('_',SUBSTRING(Field, CHARINDEX('_',Field)+1, LEN(Field)),0))), LEN(Field)) AS POS3
FROM YOUR_TABLE
最终工作代码;
SELECT (SUBSTRING([field],0,CHARINDEX('_',[field],0))) AS POS1
, SUBSTRING(SUBSTRING([field], CHARINDEX('_',[field])+1, LEN([field])), 0, CHARINDEX('_',SUBSTRING([field], CHARINDEX('_',[field])+1, LEN([field])),0)) AS POS2
, RIGHT([field],CHARINDEX('_',REVERSE([field]))-1) AS POS3
FROM TableName
谢谢大家,尤其是 Sorix。