Solved

In SQL, how can I separate characters from numeric values in a field?

Posted on 2009-07-10
6
270 Views
Last Modified: 2012-06-22
I have a column called "invoice_identifiers" that has data as follows:

123456
AB123456
A123456

What is need to write a SQL statement that

1) separates the alpha from the numeric
2) places them into individual fields

For example, the numeric part would go into a field invoice_number and the alpha part would go into invoice_letters.

Any help and examples are greatly appreciated.
0
Comment
Question by:bryan_z
  • 2
  • 2
  • 2
6 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24826846
Are the letter portions always at the front?
0
 

Author Comment

by:bryan_z
ID: 24826874
Yes, the letters are always at the front.
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24826891
You can create a funtion whcih will retrun the results. Here is an example whihc will extract all the numbers from a given string:
Declare @s varchar(100),@result varchar(100)set @s='as4khd0939sdf78' 
set @result=''
 
select
    @result=@result+case when number like '[0-9]' then number else '' end from 
    (
         select substring(@s,number,1) as number from 
        (
            select number from master..spt_values where type='p' and number between 1 and len(@s)
        ) as t
    ) as t 
select @result as only_numbers 

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 24826897
Try like this:
select left(invoice_identifiers, patindex('%[0-9]%', invoice_identifiers)-1) as invoice_letters
, substring(invoice_identifiers, patindex('%[0-9]%', invoice_identifiers), len(invoice_identifiers)) as invoice_numbers
from your_table_name

Open in new window

0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24826931
yes, so if the numbers are always in the begining then mwvisa1's solution is better.
P.
0
 

Author Closing Comment

by:bryan_z
ID: 31602248
Thanks, this was very helpful and its great to know about the patterns.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

809 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