Solved

sql substring from column : nvarchar(255)

Posted on 2012-12-26
11
637 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 500 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

856 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