Solved

SQL query to retun only part of a piece of data

Posted on 2011-03-08
3
235 Views
Last Modified: 2012-05-11
I have a table in a database that contains data that looks like this:

10-25-665-123456
10-25-666-123548

The format of the data is always the same number of digits with the dashes always in the same place.  I am looking for a query that will only return certain parts of the data.  For example, I want the query to return only the first two digits, then then next two digits, then the next digits and so on so that my result from the query would contain separate columns with each of those pieces of data that is separated by the dashes.  Is this possible?  I know you can probably somehow use the Replace command but not sure how.
0
Comment
Question by:jwmillerill
3 Comments
 
LVL 9

Accepted Solution

by:
rajeevnandanmishra earned 125 total points
Comment Utility
Hi,
You can use the substring command.
Like:
SELECT SUBSTRING(myColumn,1,2) col1, SUBSTRING(myColumn,4,2) col2, SUBSTRING(myColumn,7,2) col3,
             SUBSTRING(myColumn,11,6) col4
FROM myTable
0
 
LVL 32

Expert Comment

by:ewangoya
Comment Utility


SELECT SUBSTRING('10-25-665-123456', 1, 2) 'Part1',
       SUBSTRING('10-25-665-123456', 4, 2) 'Part2',
       SUBSTRING('10-25-665-123456', 7, 3) 'Part3',
       SUBSTRING('10-25-665-123456', 11, 6) 'Part4'
0
 
LVL 1

Author Closing Comment

by:jwmillerill
Comment Utility
Brilliant.  Thanks!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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 video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

762 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

7 Experts available now in Live!

Get 1:1 Help Now