如果SQL中存在某些字符,则从字符串中提取子字符串
我有一个字符串:如果SQL中存在某些字符,则从字符串中提取子字符串,sql,sql-server,Sql,Sql Server,我有一个字符串: DECLARE @UserComment AS VARCHAR(1000) = 'bjones marked inspection on system UP for site COL01545 as Refused to COD won''t pay upfront :Routeid: 12 :Inspectionid: 55274' “Inspectionid:'只留下要保存到变量中的Inspectionid”之后,我是否可以从字符串中提取所有内容?您的示例工作不太正确。
DECLARE @UserComment AS VARCHAR(1000) = 'bjones marked inspection on system UP for site COL01545 as Refused to COD won''t pay upfront :Routeid: 12 :Inspectionid: 55274'
“Inspectionid:'只留下要保存到变量中的Inspectionid”之后,我是否可以从字符串中提取所有内容?您的示例工作不太正确。您将变量定义为varchar(100),但字符串中的字符比这个多 这应该基于您的示例数据
DECLARE @UserComment AS VARCHAR(1000) = 'bjones marked inspection on system UP for site COL01545 as Refused to COD won''t pay upfront :Routeid: 12 :Inspectionid: 55274'
select right(@UserComment, case when charindex('Inspectionid: ', @UserComment, 0) > 0 then len(@UserComment) - charindex('Inspectionid: ', @UserComment, 0) - 13 else len(@UserComment) end)
你的例子不太正确。您将变量定义为varchar(100),但字符串中的字符比这个多 这应该基于您的示例数据
DECLARE @UserComment AS VARCHAR(1000) = 'bjones marked inspection on system UP for site COL01545 as Refused to COD won''t pay upfront :Routeid: 12 :Inspectionid: 55274'
select right(@UserComment, case when charindex('Inspectionid: ', @UserComment, 0) > 0 then len(@UserComment) - charindex('Inspectionid: ', @UserComment, 0) - 13 else len(@UserComment) end)
我会这样做:
select stuff(@UserComment, 1, charindex(':Inspectionid: ', @UserComment) + 14, '')
即使找不到字符串,也可以这样做——尽管它将返回整个字符串。要在这种情况下获取空字符串,请执行以下操作:
select stuff(@UserComment, 1, charindex(':Inspectionid: ', @UserComment + ':Inspectionid: ') + 14, '')
我会这样做:
select stuff(@UserComment, 1, charindex(':Inspectionid: ', @UserComment) + 14, '')
即使找不到字符串,也可以这样做——尽管它将返回整个字符串。要在这种情况下获取空字符串,请执行以下操作:
select stuff(@UserComment, 1, charindex(':Inspectionid: ', @UserComment + ':Inspectionid: ') + 14, '')
首先,我要说的是@UserComment变量不够长,无法包含要输入的文本。增加第一个的大小 下面的SQL将提取该值:
DECLARE @UserComment AS VARCHAR(1000); SET @UserComment = 'bjones marked inspection on system UP for site COL01545 as Refused to COD won''t pay upfront :Routeid: 12 :Inspectionid: 55274'
DECLARE @pos int
DECLARE @InspectionId int
DECLARE @IdToFind varchar(100)
SET @IdToFind = 'Inspectionid: '
SET @pos = CHARINDEX(@IdToFind, @UserComment)
IF @pos > 0
BEGIN
SET @InspectionId = CAST(SUBSTRING(@UserComment, @pos+LEN(@IdToFind)+1, (LEN(@UserComment) - @pos) + 1) AS INT)
PRINT @InspectionId
END
如果需要,您可以将上述代码转换成SQL函数。首先,我要说的是@UserComment变量不够长,无法包含您输入的文本。增加第一个的大小 下面的SQL将提取该值:
DECLARE @UserComment AS VARCHAR(1000); SET @UserComment = 'bjones marked inspection on system UP for site COL01545 as Refused to COD won''t pay upfront :Routeid: 12 :Inspectionid: 55274'
DECLARE @pos int
DECLARE @InspectionId int
DECLARE @IdToFind varchar(100)
SET @IdToFind = 'Inspectionid: '
SET @pos = CHARINDEX(@IdToFind, @UserComment)
IF @pos > 0
BEGIN
SET @InspectionId = CAST(SUBSTRING(@UserComment, @pos+LEN(@IdToFind)+1, (LEN(@UserComment) - @pos) + 1) AS INT)
PRINT @InspectionId
END
如有必要,您可以将上述代码转换为SQL函数。如果检查ID始终为5位数字,则子字符串函数(长度)的最后一个参数可以为5,即
SELECT SUBSTRING(@UserComment,PATINDEX('%Inspectionid:%',@UserComment)+14,5)
如果检查ID不同(但总是在末尾-您的问题略微暗示了这一点),那么可以通过从字符串的总长度中减去'InspectionID'的位置来导出最后一个参数。像这样:
SELECT SUBSTRING(@UserComment,PATINDEX('%Inspectionid:%',@UserComment)+14,LEN(@usercomment)-(PATINDEX('%Inspectionid:%',@UserComment)+13))
如果检验ID始终为5位数字,则子字符串函数(长度)的最后一个参数可以为5,即
SELECT SUBSTRING(@UserComment,PATINDEX('%Inspectionid:%',@UserComment)+14,5)
如果检查ID不同(但总是在末尾-您的问题略微暗示了这一点),那么可以通过从字符串的总长度中减去'InspectionID'的位置来导出最后一个参数。像这样:
SELECT SUBSTRING(@UserComment,PATINDEX('%Inspectionid:%',@UserComment)+14,LEN(@usercomment)-(PATINDEX('%Inspectionid:%',@UserComment)+13))
我继续往前走,把柱子修好了。尽管如此,你的回答还是很有效。谢谢我继续往前走,把柱子修好了。尽管如此,你的回答还是很有效。谢谢