Solved

How to strip unneeded characters

Posted on 2014-09-03
5
140 Views
Last Modified: 2014-09-04
How can I strip the unneeded characters from MS SQL table column to another view column based on the following examples:

[IDNo = 201]   to 1

[QuoteNo = 10005]  to 10005

[PONo = 3002] to 3002


Thanks.
0
Comment
Question by:JimiJ13
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 450 total points
ID: 40300707
does this mean you just want the "number" information from a text field?
in which case, a simple search should have given some options already...
for example, this nice example;
http://blogs.lessthandot.com/index.php/datamgmt/datadesign/extracting-numbers-with-sql-server/
Create Function dbo.GetNumbers(@Data VarChar(8000))
Returns VarChar(8000)
AS
Begin	
    Return Left(
             SubString(@Data, PatIndex('%[0-9.-]%', @Data), 8000), 
             PatIndex('%[^0-9.-]%', SubString(@Data, PatIndex('%[0-9.-]%', @Data), 8000) + 'X')-1)
End

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 40300709
You could use:
PATINDEX('%[0-9]%', my_column)

Open in new window

to find the character position that the number starts at, and use CHARINDEX (or PATINDEX again) to find the position of the ] character, then use SUBSTRING with the start and end positions to extract the number.

I'd write an SQL, but it's my bedtime.... zzzz
0
 

Assisted Solution

by:Monty Miller
Monty Miller earned 50 total points
ID: 40301165
You could query the column with  Left(SubString(BrandNum, PatIndex('%[0-9.-]%', BrandNum), 8000), PatIndex('%[^0-9.-]%', SubString(BrandNum, PatIndex('%[0-9.-]%', BrandNum), 8000) + 'X')-1)

Example:

SELECT     BrandNum, LEFT(SUBSTRING(BrandNum, PATINDEX('%[0-9.-]%', BrandNum), 8000), PATINDEX('%[^0-9.-]%', SUBSTRING(BrandNum, PATINDEX('%[0-9.-]%', BrandNum),
                      8000) + 'X') - 1) AS Data
FROM         tbl_Spirit_Brands

RESULTS:

BrandNum      Data
S10      10
S100      100
S1000      1000
S1004      1004
S1005      1005
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40301507
SELECT original_value,
    CASE WHEN pos_of_first_digit = 0 THEN '' ELSE
        SUBSTRING(original_value, pos_of_first_digit, PATINDEX('%[^0-9]%', SUBSTRING(original_value, pos_of_first_digit, 8000) + '~') - 1) END AS first_integer_found
FROM ( --supply test data; replace with your own table in final code
    SELECT '[IDNo = 201]' AS original_value UNION ALL
    SELECT '[QuoteNo = 10005]' UNION ALL
    SELECT '[PONo = 3002]' UNION ALL
    SELECT '[Unfinished = 999' UNION ALL
    SELECT '[all.data = 4125]'
) AS test_data
CROSS APPLY (
    SELECT PATINDEX('%[0-9]%', original_value) AS pos_of_first_digit
) AS assign_alias
0
 

Author Closing Comment

by:JimiJ13
ID: 40303199
Guy Hengel  solution is great and very clean.

Thanks!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now