Solved

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

Posted on 2009-07-10
6
276 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

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.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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