If you really need to work with varbinary data, you can just cast it back to nvarchar:
DECLARE @bin VARBINARY(MAX)
SET @bin = 0x5468697320697320612074657374
SELECT CAST(@bin as VARCHAR(MAX))
-- gives This is a test
Once you've got it into that format, you can use a split function to turn it into a table. Don't ask me why there isn't a built-in split function in SQL Server, given that it's such a screamingly obvious oversight, but there isn't. So create your own with the code below:
CREATE FUNCTION [dbo].[fn_splitDelimitedToTable] ( @delimiter varchar(3), @StringInput VARCHAR(8000) )
RETURNS @OutputTable TABLE ([String] VARCHAR(100), [Hierarchy] int )
AS
BEGIN
DECLARE @String VARCHAR(100)
DECLARE @row int = 0
WHILE LEN(@StringInput) > 0
BEGIN
SET @row = @row + 1
SET @String = LEFT(@StringInput,
ISNULL(NULLIF(CHARINDEX(@delimiter, @StringInput) - 1, -1),
LEN(@StringInput)))
SET @StringInput = SUBSTRING(@StringInput,
ISNULL(NULLIF(CHARINDEX(@delimiter, @StringInput), 0),
LEN(@StringInput)) + 1, LEN(@StringInput))
INSERT INTO @OutputTable ( [String], [Hierarchy] )
VALUES ( @String, @row )
END
RETURN
END
Put it all together:
select CAST('one,two,three' as VARBINARY)
-- gives 0x6F6E652C74776F2C7468726565
DECLARE @bin VARBINARY(MAX)
SET @bin = 0x6F6E652C74776F2C7468726565
select * from fn_splitDelimitedToTable(',', CAST(@bin as VARCHAR(MAX)))
gives this result:
string hierarchy
================
one 1
two 2
three 3
And of course, you can get the result into a temp table to work with if you so wish:
select * into #myTempTable
from fn_splitDelimitedToTable(',', CAST(@bin as VARCHAR(MAX)))
'value1;value2;value3\r\nvalue4;value5;value6\r\n'. This may be a simple question but how do I parse that column without writing to much custom code?