SQL Server 2008 - A clever way to fill a column with rising integers - sql

I'm working on a database and what I want to do is create a table with an ID (auto increment) and another column: "Number" (I realize it sounds useless but bear with me here please), and I need to fill this "Number" column with values from 1 to 180, each time adding 1 to the previous.
What would be a clever "automatic" way of doing that?

Create a table with the columns you want (ID,Number) and set ID to auto increment. Once your done, use a while to load up to 180.
Here is the CREATE TABLE
CREATE TABLE [dbo].[<YOUR TABLE>](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Number] [int] NULL
) ON [PRIMARY]
Here is the INSERT
INSERT INTO <YOUR TABLE> (Number) VALUES (1);
WHILE SCOPE_IDENTITY() < 180
BEGIN
INSERT INTO <YOUR TABLE> (Number) VALUES (SCOPE_IDENTITY()+1);
END

Make the column you want to update 0 then you can simply:
DECLARE #id INT = 0
UPDATE tbl
SET #id = id = (#id % 180 + 1)
To just to increment remove % 180.

In transact SQL a simple update should give you want you want
create table table_x ( A char(1), B int NULL )
declare #i int
select #i = 1
update table_x
set B=#i, #x=#i+1
from table_x
select * from table_x

If I understood you correctly ROW_NUMBER() should solve your problem.
Example:
select ROW_NUMBER() OVER(ORDER BY ID) from sysobjects
If you want 1 to 180 and then again 1 to 180:
select ((ROW_NUMBER() OVER(ORDER BY ID)) - 1) % 180 + 1 from sysobjects
Update:
update tablename
set number =
(select number
from
(select
id,
((ROW_NUMBER() OVER(ORDER BY ID)) - 1) % 180 + 1 number
from tablename) u
where u.id = tablename.id)

WITH n AS(SELECT 0 nUNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9)
SELECT n1.n * 100 + n2.n * 10 + n3.n + 1
FROM n n1 CROSS JOIN n n2 CROSS JOIN n n3
WHERE n1.n * 100 + n2.n * 10 + n3.n + 1 <= 180

Itzik Ben-Gan has a great row-number generation method which does not use tables but only constants and cross join. Very elegant in my eyes.
from http://www.projectdmx.com/tsql/tblnumbers.aspx#Recur
;WITH Nbrs_3( n ) AS ( SELECT 1 UNION SELECT 0 ),
Nbrs_2( n ) AS ( SELECT 1 FROM Nbrs_3 n1 CROSS JOIN Nbrs_3 n2 ),
Nbrs_1( n ) AS ( SELECT 1 FROM Nbrs_2 n1 CROSS JOIN Nbrs_2 n2 ),
Nbrs_0( n ) AS ( SELECT 1 FROM Nbrs_1 n1 CROSS JOIN Nbrs_1 n2 ),
Nbrs ( n ) AS ( SELECT 1 FROM Nbrs_0 n1 CROSS JOIN Nbrs_0 n2 )
SELECT n
FROM ( SELECT ROW_NUMBER() OVER (ORDER BY n)
FROM Nbrs ) D ( n )
WHERE n <= 180 ; -- or any number
You can replace the 180 with any number. instead of the select you can use the generated sequence for your insert.

This query will update all records in table no need of % 180
update tableName
set columnName=
(select number
from
(select id, ((ROW_NUMBER() OVER(ORDER BY ID))) number
from TableName ) t
where t.id = tableName.id)
go
select * from tableName order by id
go

Related

How to complete and fill in gaps between dates in SQL?

I have data in Redshift that I'm aggregating to the Year-Quarter level i.e. number of items by Year-Quarter
I need to show a continuous trend and hence I need to fill-in the gaps in Year-Quarter. The picture below should give a clearer idea of my current data and desired output.
How can I achieve this in Redshift SQL?
A query like this should do the trick:
create table test (yq int, items int);
INSERT INTO test Values (20201,10),(20204, 15),(20213, 25),(20222, 30);
with recursive quarters(q) as (
select min(yq) as q
from test
union all
select decode(right(q::text, 1), 4, q + 7, q + 1) as q
from quarters
where q < (select max(yq) from test)
)
select q as yq, decode(items is null, true,
lag(items ignore nulls) over (order by q), items) as items
from test t
right join quarters q
on t.yq = q.q
order by q;
It uses a recursive CTE to generate the quarters range needed, right joins this with the source data, and then uses a LAG() window function to populate the items if the value is NULL.
This is known as forward filling values:
CREATE TABLE #Temp
(
[YQ] nvarchar(5),
[items] int
)
INSERT INTO #Temp Values ('20201',10),('20204', 15),('20213', 25),('20222', 30)
---------------------------------------------------------------------------------
DECLARE #start int, #end int, #starty int, #endy int
SELECT #start=1, #end=4
SELECT #starty=MIN(Substring(YQ,0,5)), #endy=MIN(Substring(YQ,0,5)) from #Temp
;With cte1(y) as
(
Select #starty as y
union all
Select y + 1
from cte1
where y <= #endy + 1
)
, cte2(n) as
(
Select #start as n
union all
Select n + 1
from cte2
where n < #end
)
SELECT t1.YQ AS 'Year-Quarter',
CASE WHEN t2.items is null then (SELECT TOP 1 MAX(items) from #Temp WHERE items is not null and YQ < t1.YQ) ELSE t2.items END AS '# Items'
FROM
(
SELECT CAST(cte1.y AS nvarchar(4)) + CAST(cte2.n AS nvarchar(1)) AS YQ
FROM cte1, cte2
) t1
LEFT JOIN #Temp t2 ON t2.YQ = t1.YQ
WHERE t1.YQ <= (SELECT MAX(YQ) FROM #Temp)
ORDER BY t1.YQ, t2.items

SQL split-string as (key-identity,value)

I've added a function to my DB that splits a comma separated string into separate rows.
Now in my string I have: 1,55,2,56,3,57,etc... where (1) is the rowID and (55) the value I want to enter into row 1 of my table.
How can I modify this function to pull the 1st,3rd,5th,etc... values and 2nd,4th,6th,etc... values into two different columns?
CREATE FUNCTION dbo.SplitStringToValues
(
#List NVARCHAR(MAX),
#Delimiter NVARCHAR(255)
)
RETURNS TABLE
WITH SCHEMABINDING AS
RETURN
WITH E1(N) AS ( SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1),
E2(N) AS (SELECT 1 FROM E1 a, E1 b),
E4(N) AS (SELECT 1 FROM E2 a, E2 b),
E42(N) AS (SELECT 1 FROM E4 a, E2 b),
cteTally(N) AS (SELECT 0 UNION ALL SELECT TOP (DATALENGTH(ISNULL(#List,1)))
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E42),
cteStart(N1) AS (SELECT t.N+1 FROM cteTally t
WHERE (SUBSTRING(#List,t.N,1) = #Delimiter OR t.N = 0))
SELECT Item = SUBSTRING(#List, s.N1, ISNULL(NULLIF(CHARINDEX(#Delimiter,#List,s.N1),0)-s.N1,8000))
FROM cteStart s;
go
-------------- Update
Thanks everyone for your examples. I'm going to try out each of these until I get something working. I will accept once i figure out which on I can make work.
Thank you,
Alexp
An attempt to help with batch script; please try it out:
DECLARE #List NVARCHAR(MAX) = '1,55,2,56,3,57,10,65,11,88';
DECLARE #Delimiter NVARCHAR(255) = ',';
DECLARE #ListDataTable TABLE
(
ID INT IDENTITY (1, 1)
,DataKey INT
,DataValue INT
)
INSERT INTO #ListDataTable (DataKey, DataValue)
SELECT
value
,LEAD(value, 1, 0) OVER(ORDER BY (SELECT 1))
FROM STRING_SPLIT(#List, #Delimiter) WHERE RTRIM(value) <> '';
-- To get odd key values
SELECT * FROM
(
SELECT DataKey, DataValue FROM #ListDataTable WHERE ID % 2 = 1
) Temp WHERE DataKey % 2 = 1;
-- To get even key values
SELECT * FROM
(
SELECT DataKey, DataValue FROM #ListDataTable WHERE ID % 2 = 1
) Temp WHERE DataKey % 2 = 0;
Modify your function to return two columns: the position and the value. This is easy enough and keeps the function general purpose. Just change the select to:
SELECT Item = SUBSTRING(#List, s.N1, ISNULL(NULLIF(CHARINDEX(#Delimiter, #List, s.N1), 0) - s.N1, 8000)),
ItemNum = row_number() over (order by s.N1)
FROM cteStart s;
Then you can use to get the information you want. Here is one method:
select max(case when ItemNum % 2 = 1 then Item end) as rownum,
max(case when ItemNum % 2 = 0 then Item end) as value
from dbo.SplitStringToValues('1,55,2,56,3,57', ',')
group by (ItemNum - 1) / 2
#Macwise was on to something with LEAD - you could do this:
SELECT rownum = item, value
FROM
(
SELECT itemnumber, item, value = LEAD(item,1) OVER (ORDER BY itemnumber)
FROM dbo.SplitStringToValues('1,44,2,55,3,456,4,123,5,0', ',')
) split
WHERE 1 = itemnumber%2;
Gordon's solution is the best, most elegant pre-2012 solution. Here's another pre-2012 solution that does not require a sort in the execution plan:
SELECT rownum = s1.Item, value = s2.Item
FROM DelimitedSplit8K(#string, ',') s1
INNER MERGE JOIN SplitStringToValues('1,44,2,55,3,456,4,123,5,0', ',') s2
ON 1 = s1.itemNumber % 2 AND s1.ItemNumber = s2.ItemNumber-1;
Instead of changing that function, to get the next row's value next to the id use the LEAD function introduced in SQL SERVER 2012:
SELECT Id, Value
FROM (SELECT
ROW_NUMBER() over (order by(select 1)) as cnt,
t.item AS Id,
Lead(t.item)
OVER (
ORDER BY (SELECT 1)) Value
FROM dbo.Splitstringtovalues('10,20,30,40,50,10,20,30,40,50,60,70', ',')
t)
keyValue
WHERE keyValue.value IS NOT NULL
and cnt % 2 = 1

SQL Select Concat between 2 numbers

I work with SQL Server 2012 and need a concatenate between 2 different columns.
eg:
3 and 7 = 34567
or 1 and 4 = 1234
or 2 and 2 = 2
When I use the Concat Function, I am just able to Concate the first and the last number. But I need the numbers between, too.
Try this query. Here firstcolumn =3 and secondcolumn=7
SELECT t.Id,
,STUFF((SELECT '' + CAST( n AS VARCHAR(50)) [text()]
FROM (SELECT DISTINCT n = number
FROM master..[spt_values]
WHERE number >= firstcolumn AND number <= secondcolumn
)a
FOR XML PATH(''), TYPE)
.value('.','NVARCHAR(MAX)'),1,0,'') List_Output
FROM tablename t
There are multiple ways to generate sequences in sql-server. Here is a simple that doesn't need a number-table:
WITH Numbers AS
(
SELECT TOP (2000) n = ROW_NUMBER() OVER (ORDER BY object_id)
FROM sys.all_objects ORDER BY n
)
SELECT n FROM Numbers
WHERE n BETWEEN 3 AND 7
Here's a recursive query that will go from start to end recursively and generate the string you want or an INTEGER value:
DECLARE #start INT = 3
DECLARE #end INT = 7
DECLARE #int_value INT = 0
DECLARE #str_value VARCHAR(100) = '';
WITH rec AS (
SELECT #start AS val
UNION ALL
SELECT val + 1
FROM rec
WHERE val < #end
)
SELECT #str_value = CONCAT(#str_value, val),
#int_value = #int_value * 10 + val
FROM rec
SELECT #str_value, #int_value
This is Itzik's style
declare #values varchar(100)='', #from int, #to int
select #from=3, #to=7
;WITH
n0 AS (SELECT 0 AS number UNION ALL SELECT 0),
n1 AS (SELECT 0 AS number FROM n0 AS a CROSS JOIN n0 AS b),
n2 AS (SELECT 0 AS number FROM n1 AS a CROSS JOIN n1 AS b),
n3 AS (SELECT 0 AS number FROM n2 AS a CROSS JOIN n2 AS b)
select #values=#values+ltrim(sno) from
(select row_number() over (order by number) as sno from n3) as t
where sno between #from and #to
select #values as [values]
Thank you for the Answer.
I am going to use the answer from #Mukesh Kalgude.
So, my full query is the follow:
select
DayFrom,DayTo,
STUFF((SELECT TOP 7'' + CAST( n AS VARCHAR(50)) [text()]
FROM (SELECT DISTINCT n = number
FROM master..[spt_values]
WHERE number >= DayFrom AND DayTo <= 7
)a
FOR XML PATH(''), TYPE)
.value('.','NVARCHAR(MAX)'),1,0,'') List_Output
from SwitchProfilePairs
The result is
dayFrom = 1 day To = 1 But the List_Output is 1234567
Try this using SUBSTRING() function(Fiddle example):
--Declare sample table
DECLARE #T TABLE (id int identity, numCol1 int, numCol2 int)
--Add some values
INSERT #T (numCol1, numCol2)
VALUES (3, 7), (1, 4), (2, 2)
--Actual Query
SELECT *, SUBSTRING('123456789', numCol1, numCol2 - numCol1 + 1) Number
FROM #T
Above query works only with single digit numbers. Modified version (below) to work with numbers like 34, 78
SELECT *,
SUBSTRING('123456789', CONVERT(int, LEFT(numCol1,1)),
CONVERT(int, RIGHT(numCol2, 1)) - convert(int, LEFT(numCol1,1)) + 1) YourNumber
FROM #T
Note: Number column is returning a string, can be converted to an int using convert() function

Single text value which get unique value from all rows

Assume I have these rows:
ROW 1 apple,watermelon,pineapple
ROW 2 apple,pineapple,orange
ROW 3 apple,blue berry
I want to create single text value which get unique value from all rows.
How do we do it with query?
Expected Result:
apple,watermelon,pineapple,orange,blue berry
Try this:
select array_agg(val) from (
select distinct unnest(string_to_array(my_column, ',')) val from my_table) x
A breakdown of what's going on:
string_to_array() splits the string, using the specified delimiter, into a true array
unnest() turns an array into separate rows - one for each element
distinct removes duplicate rows
array_agg() joins all rows into a single CSV string (typically you would us a group by clause, but no need here as there's only one group)
You can use Following function to Split the row values by ','
CREATE FUNCTION [dbo].[ConvertToTable]
(
#delimiter char(1),
#string nvarchar(MAX)
)
RETURNS #Values TABLE ( VALUE NVARCHAR(MAX) )
AS BEGIN
SET #string = #string + #delimiter ;
WITH Nbrs_3 ( n ) AS ( SELECT 1 UNION SELECT 0 ) ,
Nbrs_2 ( n ) AS ( SELECT 1 FROM Nbrs_3 n1 CROSS JOIN Nbrs_3 n2 ) ,
Nbrs_1 ( n ) AS ( SELECT 1 FROM Nbrs_2 n1 CROSS JOIN Nbrs_2 n2 ) ,
Nbrs_0 ( n ) AS ( SELECT 1 FROM Nbrs_1 n1 CROSS JOIN Nbrs_1 n2 ) ,
Nbrs ( n ) AS ( SELECT 1 FROM Nbrs_0 n1 CROSS JOIN Nbrs_0 n2 )
INSERT #Values ( [VALUE] )
SELECT SUBSTRING(#string, n + 1, CHARINDEX(#delimiter, #string, n + 1) - n - 1)
FROM ( SELECT 0 AS 'n' UNION ALL
SELECT TOP ( LEN(#string) - 1 ) ROW_NUMBER() OVER ( ORDER BY n ) AS 'n' FROM Nbrs
) x
WHERE SUBSTRING(#string, n, 1) = #delimiter
OR n = 0
RETURN
END
And Use following Code to Get the result..
DECLARE #unique_value NVARCHAR(MAX)
WITH cte AS (
SELECT 1 AS id, 'apple,watermelon,pineaple' AS String
UNION
SELECT 2 ,'apple,pineaple,orange'
UNION
SELECT 3 ,'apple,blue berry'
)
SELECT #unique_value= COALESCE(#unique_value+',','')+[VALUE] FROM [cte]
CROSS APPLY [dbo].[ConvertToTable](',',[String]) AS CTT
GROUP BY [VALUE]
SELECT #unique_value
UPDATE: I didn't notice this is about postgresql. i have gave the answer for MSSQL, If you could do the same in postgresql. this method would help you..
try this:
SELECT ARRAY(SELECT distinct regexp_split_to_table(myTable.columns, E',') AS split_columns FROM myTable);

split comma separated values into distinct rows

I have a table which looks like this:
id fk_det userid
3 9 name1,name2
6 1 name3
9 2 name4,name5
12 3 name6,name7
I have already learned to regret having the values of the userid in comma separated values, so i want to split the rows up and end up with something that looks like
id fk_det userid
3 9 name1
x 9 name2
6 1 name3
9 2 name4
x 2 name5
12 3 name6
x 3 name7
i had been looking at something like this:
select fk_det, det, LEFT(userid, CHARINDEX(',',userid+',')-1),
STUFF(userid, 1, CHARINDEX(',',userid+','), '')
from global_permissions
but i'm not sure how to make it work when userid contains more than 2 items (which it might, some might have none, some might have multiple, just depends)
this is what I tend to use:
IF EXISTS (
SELECT 1
FROM dbo.sysobjects
WHERE id = object_id(N'[dbo].[ParseString]')
AND xtype in (N'FN', N'IF', N'TF'))
BEGIN
DROP FUNCTION [dbo].[ParseString]
END
GO
CREATE FUNCTION dbo.ParseString (#String VARCHAR(8000), #Delimiter VARCHAR(10))
RETURNS TABLE
AS
/*******************************************************************************************************
* dbo.ParseString
*
* Creator: magicmike
* Date: 9/12/2006
*
*
* Outline: A set-based string tokenizer
* Takes a string that is delimited by another string (of one or more characters),
* parses it out into tokens and returns the tokens in table format. Leading
* and trailing spaces in each token are removed, and empty tokens are thrown
* away.
*
*
* Usage examples/test cases:
Single-byte delimiter:
select * from dbo.ParseString2('|HDI|TR|YUM|||', '|')
select * from dbo.ParseString2('HDI| || TR |YUM', '|')
select * from dbo.ParseString2(' HDI| || S P A C E S |YUM | ', '|')
select * from dbo.ParseString2('HDI|||TR|YUM', '|')
select * from dbo.ParseString2('', '|')
select * from dbo.ParseString2('YUM', '|')
select * from dbo.ParseString2('||||', '|')
select * from dbo.ParseString2('HDI TR YUM', ' ')
select * from dbo.ParseString2(' HDI| || S P A C E S |YUM | ', ' ') order by Ident
select * from dbo.ParseString2(' HDI| || S P A C E S |YUM | ', ' ') order by StringValue
Multi-byte delimiter:
select * from dbo.ParseString2('HDI and TR', 'and')
select * from dbo.ParseString2('Pebbles and Bamm Bamm', 'and')
select * from dbo.ParseString2('Pebbles and sandbars', 'and')
select * from dbo.ParseString2('Pebbles and sandbars', ' and ')
select * from dbo.ParseString2('Pebbles and sand', 'and')
select * from dbo.ParseString2('Pebbles and sand', ' and ')
*
*
* Notes:
1. A delimiter is optional. If a blank delimiter is given, each byte is returned in it's own row (including spaces).
select * from dbo.ParseString3('|HDI|TR|YUM|||', '')
2. In order to maintain compatibility with SQL 2000, ident is not sequential but can still be used in an order clause
If you are running on SQL2005 or later
SELECT Ident, StringValue FROM
with
SELECT Ident = ROW_NUMBER() OVER (ORDER BY ident), StringValue FROM
*
*
* Modifications
*
*
********************************************************************************************************/
RETURN (
SELECT Ident, StringValue FROM
(
SELECT Num as Ident,
CASE
WHEN DATALENGTH(#delimiter) = 0 or #delimiter IS NULL
THEN LTRIM(SUBSTRING(#string, num, 1)) --replace this line with '' if you prefer it to return nothing when no delimiter is supplied. Remove LTRIM if you want to return spaces when no delimiter is supplied
ELSE
LTRIM(RTRIM(SUBSTRING(#String,
CASE
WHEN (Num = 1 AND SUBSTRING(#String,num ,DATALENGTH(#delimiter)) <> #delimiter) THEN 1
ELSE Num + DATALENGTH(#delimiter)
END,
CASE CHARINDEX(#Delimiter, #String, Num + DATALENGTH(#delimiter))
WHEN 0 THEN LEN(#String) - Num + DATALENGTH(#delimiter)
ELSE CHARINDEX(#Delimiter, #String, Num + DATALENGTH(#delimiter)) - Num -
CASE
WHEN Num > 1 OR (Num = 1 AND SUBSTRING(#String,num ,DATALENGTH(#delimiter)) = #delimiter)
THEN DATALENGTH(#delimiter)
ELSE 0
END
END
)))
End AS StringValue
FROM dbo.Numbers
WHERE Num <= LEN(#String)
AND (
SUBSTRING(#String, Num, DATALENGTH(ISNULL(#delimiter,''))) = #Delimiter
OR Num = 1
OR DATALENGTH(ISNULL(#delimiter,'')) = 0
)
) R WHERE StringValue <> ''
)
You would use it like this:
SELECT id, pk_det, V.StringValue as userid
FROM myTable T
OUTER APPLY dbo.ParseString(T.userId) V
The UDF requires a 'tally' or Number table which assumes the following schema:
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Numbers')
BEGIN
CREATE TABLE dbo.Numbers
(
Num INT NOT NULL
CONSTRAINT [PKC__Numbers__Num] PRIMARY KEY CLUSTERED (Num) on [PRIMARY]
)
;WITH Nbrs_3( n ) AS ( SELECT 1 UNION SELECT 0 ),
Nbrs_2( n ) AS ( SELECT 1 FROM Nbrs_3 n1 CROSS JOIN Nbrs_3 n2 ),
Nbrs_1( n ) AS ( SELECT 1 FROM Nbrs_2 n1 CROSS JOIN Nbrs_2 n2 ),
Nbrs_0( n ) AS ( SELECT 1 FROM Nbrs_1 n1 CROSS JOIN Nbrs_1 n2 ),
Nbrs ( n ) AS ( SELECT 1 FROM Nbrs_0 n1 CROSS JOIN Nbrs_0 n2 )
INSERT INTO dbo.Numbers(Num)
SELECT n
FROM ( SELECT ROW_NUMBER() OVER (ORDER BY n)
FROM Nbrs ) D ( n )
WHERE n <= 50000 ;
END
Numbers tables are an invaluable addition to your toolset. To quote Adam Machanic:
Numbers tables are truly invaluable. I use them all of the time for
string manipulation, simulating window functions, populating test
tables with lots of data, eliminating cursor logic, and many other
tasks that would be incredibly difficult without them.
Is using a table of numbers a hack, as I've seen some people claim?
No. Show me another way to efficiently do all of the things a numbers
table can. Does it waste space? No. The script below will use up
around 900 KB of disk space in each database. That's absolutely
nothing. You'll end up getting millions, maybe billions of times the
disk space investment back in terms of ease of development and time
saved.
http://dataeducation.com/you-require-a-numbers-table/
Try this :)
DECLARE #Name TABLE
(
id INT NULL ,
fk_det INT NULL ,
userid NVARCHAR(100) NULL
)
INSERT INTO #Name
( id, fk_det, userid)
VALUES (3,9,'name1,name2' )
INSERT INTO #Name
( id, fk_det, userid)
VALUES (6,1,'name3' )
INSERT INTO #Name
( id, fk_det, userid)
VALUES (9,2,'name4,name5' )
INSERT INTO #Name
( id, fk_det, userid)
VALUES (12,3,'name6,name7' )
SELECT *
FROM #Name
SELECT id,A.fk_det,
Split.a.value('.', 'VARCHAR(100)') AS String
FROM (SELECT id,fk_det,
CAST ('<M>' + REPLACE(userid, ',', '</M><M>') + '</M>' AS XML) AS String
FROM #Name) AS A CROSS APPLY String.nodes ('/M') AS Split(a);
As an alternative to the standard sproc call you see everywhere:
with temp as(
select id,fk_det,cast('<comma>'+replace(userid,',','</comma><comma>')+'</comma>' as XMLcomma
from global_permissions
)
select id,fk_det,a.value('comma[1]','varchar(512)')
cross apply temp.XMLcomma.nodes('/comma') t(a)