Solved

SQL query to retun only part of a piece of data

Posted on 2011-03-08
3
236 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
ID: 35072890
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
ID: 35072894


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
ID: 35072931
Brilliant.  Thanks!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

920 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

16 Experts available now in Live!

Get 1:1 Help Now