Solved

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

Posted on 2009-07-10
6
251 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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

19 Experts available now in Live!

Get 1:1 Help Now