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)
 How to get numeric part of the string?

Author  Topic 

zeeshan13
Constraint Violating Yak Guru

347 Posts

Posted - 2008-08-15 : 13:05:23
Hi All,

I have a table called Table1 with a field called Packsize [nvarchar(50)]. The records are like this.

Packsize
10.5 OZ
12 OZ
.250 OZ
5.2 OZ
22.5 OZ

I just need a select query that can return the numeric part only without any spaces. The Select query in above example should return the following.

Packsize
10.5
12
.250
5.2
22.5


How to do this?

Thanks for you prompt help.

Zee

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-15 : 13:09:47
SELECT LTRIM(RTRIM(REPLACE(Packsize,'OZ',''))) FROm YourTable
Go to Top of Page

zeeshan13
Constraint Violating Yak Guru

347 Posts

Posted - 2008-08-15 : 13:35:04
Visakh16,

It wont be OZ all the time, it can be any thing FZ,OZZ,MD, etc.

Please help.

Thanks,

Zee
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-15 : 13:37:28
SELECT SUBSTRING(@s, 1, CHARINDEX(' ', @s) - 1)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

zeeshan13
Constraint Violating Yak Guru

347 Posts

Posted - 2008-08-15 : 13:57:24
tkizer thanks for your help.

But your script returns some results inclduing string. For example the following is returning as it is:
12-5.5OZ
4-4.1
4-5.75OZ
4-4.1
24-5.5OZ


The following script runs well and have return no string so far.
Select rtrim(ltrim(left(packsize,len(packsize)-2))) from product

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-15 : 13:57:38
or use LEFT()

LEFT(Packsize,CASE WHEN CHARINDEX(' ',Packsize)>0 THEN CHARINDEX(' ',Packsize)-1 ELSE LEN(Packsize) END)
Go to Top of Page

zeeshan13
Constraint Violating Yak Guru

347 Posts

Posted - 2008-08-15 : 14:01:49
Now what if I have to do the opposite & just want to return the string and not the numeric part?
For example 4-5.75OZ should be return as OZ, and 23.5 ZZ should return ZZ

How can I do this.

Thanks for the help.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-08-15 : 15:06:55
SELECT LEFT(Col1, COALESCE(NULLIF(PATINDEX('%[^0123456789-.]%', Col1) - 1, -1), LEN(Col1))
FROM Table1


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

BalochDude
Starting Member

1 Post

Posted - 2009-08-02 : 08:20:44
I had a same problem and I was searching for the solution. I think I have found solution to my problem. May be it will also help you.

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

The solution on this page looks more like a sql server 2000 script. I guessed it from "varchar(7999)". I have changed it to "text" in my function.

Anyways, happy coding guys.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-08-02 : 08:46:32
VARCHAR(MAX) would be a better substitute.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

TranceVibes
Starting Member

1 Post

Posted - 2010-07-13 : 05:20:17
If I were u, I would use the REGEXP_SUBSTR function if available on your database...
http://psoug.org/reference/regexp.html
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-07-13 : 05:35:07
quote:
Originally posted by TranceVibes

If I were u, I would use the REGEXP_SUBSTR function if available on your database...
http://psoug.org/reference/regexp.html


No it isn't available in SQL Server.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-07-16 : 12:02:07
See if this also helps
http://beyondrelational.com/blogs/madhivanan/archive/2010/04/22/extracting-numbers-part-2.aspx

Madhivanan

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

- Advertisement -