SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Seems odd to me
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

awilcox1014
Starting Member

USA
3 Posts

Posted - 05/10/2012 :  12:43:43  Show Profile  Reply with Quote
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

USA
190 Posts

Posted - 05/10/2012 :  13:06:02  Show Profile  Visit sanjnep's Homepage  Reply with Quote
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)


Edited by - sanjnep on 05/10/2012 15:50:09
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

3833 Posts

Posted - 05/10/2012 :  13:25:44  Show Profile  Reply with Quote
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

USA
7423 Posts

Posted - 05/10/2012 :  13:27:22  Show Profile  Visit jsmith8858's Homepage  Reply with Quote
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

USA
3 Posts

Posted - 05/10/2012 :  13:52:47  Show Profile  Reply with Quote
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

USA
7423 Posts

Posted - 05/10/2012 :  14:30:22  Show Profile  Visit jsmith8858's Homepage  Reply with Quote
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

USA
190 Posts

Posted - 05/10/2012 :  15:31:54  Show Profile  Visit sanjnep's Homepage  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.16 seconds. Powered By: Snitz Forums 2000