我想从给定的字符串 Praveen(403162)* 在sql中获得子字符串 403162 ? *
您可以这样做:
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