Solved

sql substring from column : nvarchar(255)

Posted on 2012-12-26
11
635 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 142

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Generate Scripts Fails 5 36
Help in Bulk Insert 9 33
abs operation in sql server 15 13
SQL Function NOT ROUND 9 9
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

770 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