Query to get only numbers from a string in SQL Query

declare @getstringvalue nvarchar(20)
set @getstringvalue = N’pod123fgfd4h5h6h78h6h9hh4gg45hg5gh45g’

WHILE PATINDEX(‘%[^0-9]%’, @getstringvalue ) > 0 SET @getstringvalue = REPLACE(@getstringvalue , SUBSTRING(@getstringvalue , PATINDEX(‘%[^0-9]%’, @getstringvalue ), 1), ” )

SELECT @getstringvalue as output

or

short & simple process:-

select MAX(CAST(REPLACE(REPLACE(column_name , ‘string part in column’, ”), ”, ”) as int)) from table_name

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s