Solved

How to strip unneeded characters

Posted on 2014-09-03
5
141 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

777 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