小编典典

如何从格式化的字符串中获取子字符串

sql

我想从给定的字符串 Praveen(403162)* 在sql中获得子字符串 403162*


阅读 168

收藏
2021-04-15

共1个答案

小编典典

您可以这样做:

DECLARE @searchString NVARCHAR(20) = 'Praveen(403162)'
DECLARE @startFrom INT= CHARINDEX('(',@searchstring) + 1
DECLARE @length INT = CHARINDEX(')',@searchstring) - @startFrom
SELECT SUBSTRING(@searchstring,@startFrom, @length) AS RESULT

如果要一行执行此操作:

DECLARE @searchString NVARCHAR(20) = 'Praveen(403162)'
SELECT SUBSTRING(@searchstring,
       CHARINDEX('(',@searchstring) + 1, 
       CHARINDEX(')',@searchstring) - CHARINDEX('(',@searchstring) - 1) AS RESULT
2021-04-15