SELECT IDENTITY(INT) AS Number
INTO Numbers
FROM sysobjects s1
CROSS JOIN sysobjects s2
CREATE UNIQUE CLUSTERED INDEX Number_ind
ON Numbers(number)
WITH IGNORE_DUP_KEY)
SELECT dbo.Licence.LicencingID, dbo.Licence.PropertyID, dbo.Licence.TAG,
LTRIM(RTRIM(SUBSTRING( dbo.Licence.TAG, Number,
CHARINDEX( ',', dbo.Licence.TAG + ',', Number ) - Number ))) as Value
FROM dbo.Licence
INNER JOIN Numbers
ON SUBSTRING( ',' + dbo.Licence.TAG, Number, 1 ) = ','
where Number <= Len(dbo.Licence.TAG) + 1
ASKER
--
use Licence -- change to suit
go
create table dbo.Number(
n int constraint pk_Number primary key clustered
)
;
if object_id( N'tempdb..#digit', N'U' ) is not null
drop table #digit;
create table #digit(
i int
)
insert #digit( i ) values ( 0 ), ( 1 ), ( 2 ), ( 3 ), ( 4 ), ( 5 ), ( 6 ), ( 7 ), ( 8 ), ( 9 )
insert dbo.Number( n )
select
(( h.i * 10 ) + t.i ) * 10 + o.i
from #digit o
cross join #digit t
cross join #digit h
;
drop table #digit;
select count(distinct n) from Number;
(expected result 1000)
ASKER
create table dbo.Number(
n int constraint pk_Number primary key clustered
)
;
if object_id( N'tempdb..#digit', N'U' ) is not null
drop table #digit;
create table #digit(
i int
)
insert #digit( i ) values ( 0 ), ( 1 ), ( 2 ), ( 3 ), ( 4 ), ( 5 ), ( 6 ), ( 7 ), ( 8 ), ( 9 )
insert dbo.Number( n )
select
(( h.i * 10 ) + t.i ) * 10 + o.i
from #digit o
cross join #digit t
cross join #digit h
;
SELECT dbo.Licence.LicencingID, dbo.Licence.PropertyID, dbo.Licence.TAG,
LTRIM(RTRIM(SUBSTRING( dbo.Licence.TAG, n,
CHARINDEX( ',', dbo.Licence.TAG + ',', n ) - n ))) as Value
FROM dbo.Licence
INNER JOIN Number
ON SUBSTRING( ',' + dbo.Licence.TAG, n, 1 ) = ','
where n <= Len(dbo.Licence.TAG) + 1
drop table #digit;
drop table number;
/*select count(distinct n) from Number;*/
ASKER
ASKER
SELECT Value
FROM (SELECT SUBSTRING(dbo.Licence.TAG, dbo.Numbers.Number, CHARINDEX(',', dbo.Licence.TAG + ',', dbo.Numbers.Number) - dbo.Numbers.Number)
AS Value
FROM dbo.Licence INNER JOIN
dbo.Numbers ON SUBSTRING(',' + dbo.Licence.TAG, dbo.Numbers.Number, 1) = ',' AND LEN(dbo.Licence.TAG) + 1 >= dbo.Numbers.Number) AS X
ASKER
CREATE TABLE dbo.Number
(
n int CONSTRAINT pk_Number PRIMARY KEY CLUSTERED
)
;
IF OBJECT_ID(N'tempdb..#digit', N'U') IS NOT NULL
DROP TABLE #digit;
CREATE TABLE #digit
(
i int
)
INSERT #digit
(
i)
VALUES
( 0 ), ( 1 ), ( 2 ), ( 3 ), ( 4 ), ( 5 ), ( 6 ), ( 7 ), ( 8 ), ( 9 )
INSERT dbo.Number
(
n)
SELECT
((h.i * 10) + t.i) * 10 + o.i
FROM #digit o
CROSS JOIN #digit t
CROSS JOIN #digit h
;
DROP TABLE #digit;
then for your view:
CREATE VIEW whatever_v -- choose appropriate name
AS
SELECT
dbo.Licence.LicencingID
, dbo.Licence.PropertyID
, dbo.Licence.TAG
, LTRIM(RTRIM(SUBSTRING(dbo.Licence.TAG, dbo.Number.n,
CHARINDEX(',', dbo.Licence.TAG + ',', dbo.Number.n) - dbo.Number.n))) AS Value
FROM dbo.Licence
INNER JOIN dbo.Number
ON SUBSTRING(',' + dbo.Licence.TAG, dbo.Number.n, 1) = ','
WHERE n <= LEN(dbo.Licence.TAG) + 1
Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.
TRUSTED BY