Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Get integer part from a string

Author  Topic 

baburk
Posting Yak Master

108 Posts

Posted - 2009-11-10 : 06:36:12

How can I able to get only the integer part from a string

suppose my string is babu1981

the query should produce 1981

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-10 : 07:17:57
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/18/extract-only-numbers-from-a-string.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

ranganath
Posting Yak Master

209 Posts

Posted - 2009-11-10 : 07:43:25

try with this also

DECLARE @STR VARCHAR(128)
SET @STR = 'BABU1981'
SELECT REPLACE(@STR,LEFT(@STR, COALESCE(NULLIF(PATINDEX('%[0123456789]%', @STR) - 1, -1), LEN(@STR))),'')
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-10 : 07:51:37
quote:
Originally posted by ranganath


try with this also

DECLARE @STR VARCHAR(128)
SET @STR = 'BABU1981'
SELECT REPLACE(@STR,LEFT(@STR, COALESCE(NULLIF(PATINDEX('%[0123456789]%', @STR) - 1, -1), LEN(@STR))),'')


It will fail for the input BABU1981asd

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

ranganath
Posting Yak Master

209 Posts

Posted - 2009-11-10 : 08:01:37
Hi

try with this

http://www.kf7.co.uk/sql-server-function-get-numeric.aspx

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-10 : 08:06:40
quote:
Originally posted by ranganath

Hi

try with this

http://www.kf7.co.uk/sql-server-function-get-numeric.aspx




Yes. It is similar to the one I suggested in my first reply

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

baburk
Posting Yak Master

108 Posts

Posted - 2009-11-11 : 21:52:10
quote:
Originally posted by madhivanan

quote:
Originally posted by ranganath

Hi

try with this

http://www.kf7.co.uk/sql-server-function-get-numeric.aspx




Yes. It is similar to the one I suggested in my first reply

Madhivanan

Failing to plan is Planning to fail



Thanks for all of your valid reply.
Go to Top of Page
   

- Advertisement -