Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Simple CLR request

$
0
0
I'm trying to create what I believe should be a very simple Table Valued Function CLR. Here's a T-SQL version of what I'm trying to create: [code="sql"]CREATE FUNCTION dbo.NGLoop(@string varchar(8000), @N int)RETURNS @NG TABLE (Position int, Token varchar(8000)) ASBEGIN DECLARE @token varchar(8000), @position int = 0; WHILE @position < LEN(@string) - (@N-1) AND (@N > 0 AND @N <= LEN(@string)) BEGIN SELECT @token = SUBSTRING(@string,@position+1,@N), @position = @position+1; INSERT @NG VALUES(@position, @token) END RETURN;ENDGO[/code]Here's some examples of what the results should look like (note the comments):[code="sql"]DECLARE @string varchar(8000) = 'abc123';SELECT * FROM dbo.NGLoop(@string,1); -- should return 6 rows: (1,a), (2,b), (3,c), etc...SELECT * FROM dbo.NGLoop(@string,2); -- should return 5 rows: (1,ab), (2,bc), (3,c1), etc...SELECT * FROM dbo.NGLoop(@string,3); -- should return 4 rows: (1,abc), (2,bc1), (3,c12), etc...SELECT * FROM dbo.NGLoop(NULL,1); -- should return nothingSELECT * FROM dbo.NGLoop('xxx',-1); -- should return nothingSELECT * FROM dbo.NGLoop('xxx',NULL); -- should return nothingSELECT * FROM dbo.NGLoop('xxx',10); -- should return nothing[/code]Hopefully this is simple and self-explanatory enough to understand what I'm trying to do. a C# or VB.Net version should be fine. I've been racking my brain here, any help would be greatly appreciated. Thanks!

Viewing all articles
Browse latest Browse all 3145

Trending Articles