SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


convert binary string to table value construct


convert binary string to table value construct

Author
Message
Erin Ramsay
Erin Ramsay
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2053 Visits: 1119
I'm about to start on this but my search revealed nothing.

Anyone have a code snippet for changing '00001001001' into a table value construct containing (64,8,1)?

Erin
Lowell
Lowell
SSC Guru
SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)

Group: General Forum Members
Points: 69510 Visits: 40917
Erin i have this saved in my snippets, that will get you 90% there;
this is enumerating the bits as to wether they are true or false.


;WITH myStrings (val)
AS (
SELECT '00001001001' UNION ALL
SELECT '10101' UNION ALL
SELECT '1010100' UNION ALL
SELECT '010111111' )
,
FormattedStrings AS (
SELECT right('0000000000000000000000000000000' + val ,31) As sval
FROM myStrings),
MiniTally AS (
SELECT TOP 31 row_number() OVER (order by name) As N from sys.columns order by name )

select
sval,
MiniTally.N,
CONVERT(int,SUBSTRING(REVERSE(sval),MiniTally.N,1)) As bval,
POWER(2,N-1) As thePower,
CASE
WHEN (CONVERT(int,SUBSTRING(REVERSE(sval),MiniTally.N,1)) & 1) = 0
THEN 'False'
ELSE 'True'
END As [True?]
FROM FormattedStrings
CROSS JOIN MiniTally
order by sval,MiniTally.N



from there, if you filtered it for only true, and used FOR XML to concatenate the [thePower] column, you'd have what you were asking for.

does that help?
i can look at making the concat work if you are a bit weak on that too.

Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Lowell
Lowell
SSC Guru
SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)

Group: General Forum Members
Points: 69510 Visits: 40917
ok this got interesting so i did the whole thing.
enjoy!

/*
val Skills
00001001001 1,8,64
010111111 1,2,4,8,16,32,128
10101 1,4,16
1010100 4,16,64
*/
;WITH myStrings (val)
AS (
SELECT '00001001001' UNION ALL
SELECT '10101' UNION ALL
SELECT '1010100' UNION ALL
SELECT '010111111' )
,
FormattedStrings AS (
SELECT val,
right('0000000000000000000000000000000' + val ,31) As sval
FROM myStrings),
MiniTally AS (
SELECT TOP 31 row_number() OVER (order by name) As N from sys.columns order by name ),
StagedData
AS
(
select
val,
sval,
MiniTally.N,
CONVERT(int,SUBSTRING(REVERSE(sval),MiniTally.N,1)) As bval,
POWER(2,N-1) As thePower,
CASE
WHEN (CONVERT(int,SUBSTRING(REVERSE(sval),MiniTally.N,1)) & 1) = 0
THEN 'False'
ELSE 'True'
END As [True?]
FROM FormattedStrings
CROSS JOIN MiniTally
--order by sval,MiniTally.N
)

SELECT val,stuff(( SELECT ',' + convert(varchar,ThePower)
FROM StagedData s2
WHERE s2.val= s1.val --- must match GROUP BY below
And [True?]='True'
ORDER BY N
FOR XML PATH('')
),1,1,'') as [Skills]
FROM StagedData s1
GROUP BY s1.val --- without GROUP BY multiple rows are returned
ORDER BY s1.val



Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40792 Visits: 19815
Here's another option. I was thinking more of creating it for an iTVF (but leaving that part to you).

DECLARE @String varchar(20) = '00001001001';

WITH e1(N) AS(
SELECT N FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0))e(N)
),
cteTally(N) AS(
SELECT TOP(LEN(@String)) ROW_NUMBER() OVER(ORDER BY(SELECT NULL)) - 1 FROM e1 a, e1 b
)
SELECT STUFF((SELECT ',' + CAST( POWER(2,N) AS varchar(5))
FROM cteTally
WHERE SUBSTRING( @String, LEN(@String) - N, 1) = 1
ORDER BY N DESC
FOR XML PATH('')), 1, 1, '')



By the way, it was a surprise that ^ is not power in SQL and I had to use the POWER function instead. I haven't used it in a long, long time (if I ever used it before).


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Erin Ramsay
Erin Ramsay
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2053 Visits: 1119
Thank you both! I found out the same thing about the power() function and was a little surprised but I haven't used it in a while either. I'll try them both out.

Regards,
Erin
Erin Ramsay
Erin Ramsay
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2053 Visits: 1119
Well this is interesting..

Hey, Lowell, try using this in your function and change the int to bigint. I must be missing something because it's not returning everything.

'111111101100010000111100000001'
Erin Ramsay
Erin Ramsay
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2053 Visits: 1119
Yeah, I'm an idiot, Lowell.. It's all there.. lol

Thanks again!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search