Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql substring from column : nvarchar(255)

Posted on 2012-12-26
11
Medium Priority
?
654 Views
Last Modified: 2012-12-27
I have a column with datatype nvarchar(255)

Size

30 EA
2.5 ML
100 EA
60 EA
100 EA
30 EA
90 EA
60 EA
30 EA
8.5 GM

I need to pick only numerical value


Size

30
2.5
100
60
100
30
90
60
30
8.5
0
Comment
Question by:chokka
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 11

Expert Comment

by:David Kroll
ID: 38722019
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38722053
select left( yourfield, charindex(' ', yourfield)-1) from yourtable
0
 

Author Comment

by:chokka
ID: 38722064
@angelIII, spills error as

Invalid length parameter passed to the LEFT or SUBSTRING function.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 11

Expert Comment

by:David Kroll
ID: 38722070
if you get the invalid length from this:

select left( yourfield, charindex(' ', yourfield)-1) from yourtable

It means the field doesn't have a space in it, that's what it's looking for.
0
 
LVL 12

Expert Comment

by:Jared_S
ID: 38722080
Working off Angel's suggestion...

select left( yourfield, patindex('%[^0-9]%', yourfield)) from yourtable
0
 
LVL 12

Expert Comment

by:Jared_S
ID: 38722085
If this field is an nvarchar(255), then it's either bigger than it needs to be, or you're storing more than one value in the field.

If it's the latter, then you'll need a user defined function to strip the characters out of the entire string. dkrollCTN posted a link to handle that scenario.
0
 

Author Comment

by:chokka
ID: 38722109
Size

100 EA
14.7 GM

Three different Query

select left( Size, patindex('%[^0-9]%', Size)-1)
from PlacedOrderDetails

Output

Size

100
14

select left( Size, patindex('%[^0-9]%', Size))
from PlacedOrderDetails

Size

100
14.


select left( Size, patindex('%[^0-9]%', Size)+1)
from PlacedOrderDetails

Size

100 E
14.7

In my scenario, i expect the output as ..

Size

100
14.7
0
 
LVL 12

Accepted Solution

by:
Jared_S earned 2000 total points
ID: 38722148
The above query looks for the first non-numeric value (which is the decimal). This should get you around it by looking for the first alpha character instead.

left(Size,patindex('%[A-Z]%',Size)-1)
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 38722703
Isn't Size a keyword in SQL?

Try wrapping the field name with brackets:

select left( [Size], patindex('%[^0-9]%', [Size]))
from PlacedOrderDetails

Open in new window

0
 

Author Comment

by:chokka
ID: 38724427
@Jared_S , Thank you !! Your syntax is working.

Thank you :    left(Size,patindex('%[A-Z]%',Size)-1)
0
 

Author Closing Comment

by:chokka
ID: 38724875
Thank you Jared_s, Works great !!
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question