SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Generate a number list from a string value
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

gjack0519
Starting Member

6 Posts

Posted - 10/22/2013 :  16:28:51  Show Profile  Reply with Quote
I have a single column that has string values like ['61000-61055']. I am trying to get the list of numbers that would start with 61000, and end with 61055. The actual data has the beginning and ending quotes as part of the string. My output would have a number on each row.
61000
61001
61002...

Using SQL Server 2008 R2

Thanks,
gjack

James K
Flowing Fount of Yak Knowledge

3559 Posts

Posted - 10/22/2013 :  16:52:38  Show Profile  Reply with Quote
Are all the rows in the table consistently of the same pattern - i.e., a left square bracket, followed by a single quote, followed by a number, followed by a hyphen, followed by a number then single quote and square bracket?

Edited by - James K on 10/22/2013 16:52:50
Go to Top of Page

gjack0519
Starting Member

6 Posts

Posted - 10/22/2013 :  18:39:52  Show Profile  Reply with Quote
They are all consistent with a single quote, number, hyphen, number, then single quote'
exp. '61000-61055'

Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 10/23/2013 :  02:07:51  Show Profile  Reply with Quote

SELECT StartVal + v.number
FROM
(
SELECT REPLACE(LEFT(Col,CHARINDEX('-',Col)-1),'[''','') AS StartVal,
REPLACE(STUFF(Col,1,CHARINDEX('-',Col),''),''']','') AS EndVal
FROM table 
)t
CROSS JOIN master..spt_values v
WHERE v.type = 'p'
AND StartVal + v.number <= EndVal


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

gjack0519
Starting Member

6 Posts

Posted - 10/23/2013 :  16:42:40  Show Profile  Reply with Quote
Thanks,

I'll give that a try.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000