I would like to get a substring 403162 from the given string Praveen(403162) in sql?
SELECT SUBSTRING(column_name, CHARINDEX('(', column_name)+1, CHARINDEX(')', column_name)-CHARINDEX('(', column_name)-1) from table_name
DEMO
You can do it like this:
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
If you want to do this in one line:
DECLARE @searchString NVARCHAR(20) = 'Praveen(403162)'
SELECT SUBSTRING(@searchstring,
CHARINDEX('(',@searchstring) + 1,
CHARINDEX(')',@searchstring) - CHARINDEX('(',@searchstring) - 1) AS RESULT
Declare @Name VARCHAR(100)
Select @Name = 'Praveen(403162)'
Select @Name = SUBSTRING(@name,PATINDEX('%[0-9]%',@Name),LEN(@Name))
Select SUBSTRING (@Name,1,6)