Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Seems odd to me

Author  Topic 

awilcox1014
Starting Member

3 Posts

Posted - 2012-05-10 : 12:43:43
While I have a great deal of experience with DB2 - I am fairly new to SQL Server. I am working on a project right now trying to help the tester write some SQL QA testing. I just came across a requirement that says that we are to convert the OPTIONS column to binary and if the second right most digit is 0 and the 6th right most digit is 0 then count the record as 1. Otherwise is 0.

Nears as I can tell there is no EASY way to do this. So I am out of my league trying to help here. How do I convert the OPTIONS column - which is defined as a BIGINT so I can do this conversion? I wish the ETL people had just applied a Y/N to the record based on this rule?

As a side - is this common in SQL Server? This query is running inside of a dashboard. Seems like this is an inefficient way to do this to me. SOmeone please explain if you can.

THANKS!!!

sanjnep
Posting Yak Master

191 Posts

Posted - 2012-05-10 : 13:06:02
CREATE FUNCTION udf_bin_me (@IncomingNumber int)
RETURNS varchar(200)
as
BEGIN

DECLARE @BinNumber VARCHAR(200)
SET @BinNumber = ''

WHILE @IncomingNumber <> 0
BEGIN
SET @BinNumber = SUBSTRING('0123456789', (@IncomingNumber % 2) + 1, 1) + @BinNumber
SET @IncomingNumber = @IncomingNumber / 2
END

RETURN @BinNumber

END


select dbo.udf_bin_me(100)

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-05-10 : 13:25:44
You didn't post and sample data or expectged results. But, if I'm understanding yo correctly you are trying to use a bit mask. Maybe this will help:
DECLARE @Foo AS TABLE (Val BIGINT)

INSERT @Foo (Val)
VALUES
(0),
(1),
(2), -- Second Right
(64), -- Sixth Right
(66), -- Second And Sixth Right
(1569918) -- also Second And Sixth Right



SELECT
Val,
Val & POWER(2, 1) AS SecondRight,
Val & POWER(2, 6) AS SixthRight,
CASE
WHEN
Val & POWER(2, 1) > 0
AND Val & POWER(2, 6) > 0
THEN 1
ELSE 0
END AS IsSecondAndSixth
FROm @Foo
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2012-05-10 : 13:27:22
quote:
Originally posted by awilcox1014

While I have a great deal of experience with DB2 - I am fairly new to SQL Server. I am working on a project right now trying to help the tester write some SQL QA testing. I just came across a requirement that says that we are to convert the OPTIONS column to binary and if the second right most digit is 0 and the 6th right most digit is 0 then count the record as 1. Otherwise is 0.

Nears as I can tell there is no EASY way to do this. So I am out of my league trying to help here. How do I convert the OPTIONS column - which is defined as a BIGINT so I can do this conversion? I wish the ETL people had just applied a Y/N to the record based on this rule?

As a side - is this common in SQL Server? This query is running inside of a dashboard. Seems like this is an inefficient way to do this to me. SOmeone please explain if you can.

THANKS!!!



Just confirming: are you supposed to test the digits at those positions, or the bits at those positions? Typically data packed like this is done via bits, not digits, but just making sure.


- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

awilcox1014
Starting Member

3 Posts

Posted - 2012-05-10 : 13:52:47
OK - Sorry - I had no sample when I started. So I used something similar to the post by Sanjep and created a function to the the conversion - this works great on my local instance. I do not have the authority to create in prod though. Will have to find a DBA if I go that route. To clarify - I am to convert to binary - if the second digit from the right is a 0 and the 6th most right digit is a 0 then I am to add a 1 to the count column, else 0. Is this possible without creating the function (which works great).
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2012-05-10 : 14:30:22
quote:
Originally posted by awilcox1014

OK - Sorry - I had no sample when I started. So I used something similar to the post by Sanjep and created a function to the the conversion - this works great on my local instance. I do not have the authority to create in prod though. Will have to find a DBA if I go that route. To clarify - I am to convert to binary - if the second digit from the right is a 0 and the 6th most right digit is a 0 then I am to add a 1 to the count column, else 0. Is this possible without creating the function (which works great).



Lamprey gave you your answer.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

sanjnep
Posting Yak Master

191 Posts

Posted - 2012-05-10 : 15:31:54
ALTER FUNCTION udf_bin_me (@IncomingNumber int)
RETURNS varchar(200)
as
BEGIN

DECLARE @BinNumber VARCHAR(200)
DECLARE @flag BIT
SET @BinNumber = ''
SET @flag = 0

WHILE @IncomingNumber <> 0
BEGIN
SET @BinNumber = SUBSTRING('0123456789', (@IncomingNumber % 2) + 1, 1) + @BinNumber
SET @IncomingNumber = @IncomingNumber / 2
END

IF LEN(@BinNumber) > = 6
BEGIN
IF (LEFT(RIGHT(@BinNumber,2),1)) = 0 AND (LEFT(RIGHT(@BinNumber,6),1)) = 0
SET @flag = 1

ELSE
SET @flag = 0

END



RETURN @flag

END


GO

SELECT dbo.udf_bin_me(64);
Go to Top of Page
   

- Advertisement -