Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

Back to basics Expand / Collapse
Author
Message
Posted Tuesday, July 2, 2013 5:07 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, February 6, 2014 4:15 AM
Points: 1,242, Visits: 1,546
raulggonzalez (7/2/2013)
Danny Ocean (7/1/2013)
Good question. But i never feel to use "Bitwise AND" (&) in real working scenario. It will good, if anyone come with some real working example.


Hi, another example where BIT comparison is useful, msdb..sysschedules keeps the freq_interval in bitwise value :)

use [msdb]
go
create table #DaysOfWeekBitWise(
[bitValue] [tinyint] NOT NULL PRIMARY KEY,
[name] [varchar](10) NULL,
)
go
insert into #DaysOfWeekBitWise ([bitValue], [name])
values (1, N'Sunday')
, (2, N'Monday')
, (4, N'Tuesday')
, (8, N'Wednesday')
, (16, N'Thursday')
, (32, N'Friday')
, (64, N'Saturday')
go

select j.name
, case when j.enabled = 1 then 'Yes' else 'No' end as enabled
, jsch.next_run_date
, jsch.next_run_time
--, jst.*
, s.freq_interval
, ISNULL( STUFF( (SELECT N', ' + name FROM #DaysOfWeekBitWise AS B WHERE B.bitValue & s.freq_interval = B.bitValue FOR XML PATH('') ), 1, 2, '' ), 'None' ) AS backup_schedule
from msdb.dbo.sysjobs as j
left join msdb.dbo.sysjobschedules as jsch
on jsch.job_id = j.job_id
left join msdb.dbo.sysschedules as s
on s.schedule_id = jsch.schedule_id
order by j.name
go
drop table #DaysOfWeekBitWise
go

Cheers


Thanks raulggonzalez




Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
www.GrowWithSql.com

Post #1469395
Posted Tuesday, July 2, 2013 7:01 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Friday, September 12, 2014 4:52 AM
Points: 8,813, Visits: 9,370
Good question, genuinely back to basics, but as your int value was 32767 wouldn't 32512 have been a better distractor than 65280?

Tom
Post #1469467
Posted Tuesday, July 2, 2013 7:15 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Friday, September 12, 2014 4:52 AM
Points: 8,813, Visits: 9,370
Patibandla (7/2/2013)
What if i define @S as BIGINT

According to BOL, [Bitwise Operations (Transact-SQL) bitwise operations don't support bigint; in practise (in SQL 2012 anyway) you can mix and match bit, tinyint, smallint, int, and bigint freely with bitwise AND, bitwise OR, and bitwise XOR. I suspect that this is an error in BOL and bigint actually is supported, but it would be risky to use bigint with bitwise operators for production without confirmation from Microsoft that this is the case. There's a community comment on the page that saying that bigint works, but MS don't appear to police those at all.


Tom
Post #1469478
Posted Tuesday, July 2, 2013 7:29 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, September 12, 2014 8:40 AM
Points: 1,877, Visits: 1,409
Thanks for the question!



Everything is awesome!
Post #1469488
Posted Tuesday, July 2, 2013 9:04 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, July 24, 2014 2:48 PM
Points: 1,379, Visits: 834
martin.whitton (7/2/2013)
Danny Ocean (7/1/2013)
Good question. But i never feel to use "Bitwise AND" (&) in real working scenario. It will good, if anyone come with some real working example.

It provides a neat way of sending multiple options as a single integer parameter.

For example, let's say you have a table called "Locations" with 2 columns, LocationID and Location, containing the following values:

LocationID Location
1 London
2 Dublin
4 Paris
8 Berlin
16 New York
etc...

Then you could select any combination of Location values as follows:
declare @Selection int;
set @Selection=10; -- (this will select Dublin and Paris, because Dublin's LocationID plus Paris's LocationID equals 10)

select
Location
from
Locations
where
LocationId & @Selection>0;



I'm confused. Doesn't 2 (Dublin) and 8 (Berlin) equal 10 not 2 (Dublin) and 4 (Paris)? What am I missing?
Post #1469585
Posted Tuesday, July 2, 2013 9:18 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Friday, September 12, 2014 8:48 AM
Points: 608, Visits: 889
Danny Ocean (7/2/2013)
[quote]raulggonzalez (7/2/2013)
[quote]Danny Ocean (7/1/2013)Thanks raulggonzalez


You're welcome!

but please note that the script would give you correct results only when freq_interval = 8

http://msdn.microsoft.com/en-us/library/ms178644.aspx

Cheers
Post #1469600
Posted Tuesday, July 2, 2013 9:28 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 1:28 AM
Points: 848, Visits: 1,539
JAZZ Master (7/2/2013)
martin.whitton (7/2/2013)
Danny Ocean (7/1/2013)
Good question. But i never feel to use "Bitwise AND" (&) in real working scenario. It will good, if anyone come with some real working example.

It provides a neat way of sending multiple options as a single integer parameter.

For example, let's say you have a table called "Locations" with 2 columns, LocationID and Location, containing the following values:

LocationID Location
1 London
2 Dublin
4 Paris
8 Berlin
16 New York
etc...

Then you could select any combination of Location values as follows:
declare @Selection int;
set @Selection=10; -- (this will select Dublin and Paris, because Dublin's LocationID plus Paris's LocationID equals 10)

select
Location
from
Locations
where
LocationId & @Selection>0;



I'm confused. Doesn't 2 (Dublin) and 8 (Berlin) equal 10 not 2 (Dublin) and 4 (Paris)? What am I missing?

Sorry

You're absolutely right - I should have said Dublin and Berlin.
Post #1469612
Posted Tuesday, July 2, 2013 9:29 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Friday, September 5, 2014 1:13 PM
Points: 4,427, Visits: 3,422
Patibandla (7/2/2013)
What if i define @S as BIGINT

Can you give me a real time scenario as in this operator would be useful. i am just a bit curious as i have never used used it.

If you check out the Books Online entry for the sysschedules table (2012),

http://msdn.microsoft.com/en-us/library/ms178644.aspx

you’ll notice the following statement:

freq_interval is one or more of the following:
1 = Sunday
2 = Monday
4 = Tuesday
8 = Wednesday
16 = Thursday
32 = Friday
64 = Saturday

It means that if my job is to run on Tuesday and Saturday, the value will be 68. On Tuesday you would test it with a mask 4, on Saturday with 64. Test on say Wednesday (mask 8) will result in zero, meaning "don't run."
Post #1469613
Posted Tuesday, July 2, 2013 9:30 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Friday, September 5, 2014 1:13 PM
Points: 4,427, Visits: 3,422
L' Eomot Inversé (7/2/2013)
Good question, genuinely back to basics, but as your int value was 32767 wouldn't 32512 have been a better distractor than 65280?

Yes, it probably would.
Post #1469617
Posted Tuesday, July 2, 2013 9:34 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, July 24, 2014 2:48 PM
Points: 1,379, Visits: 834
martin.whitton (7/2/2013)
JAZZ Master (7/2/2013)
martin.whitton (7/2/2013)
Danny Ocean (7/1/2013)
Good question. But i never feel to use "Bitwise AND" (&) in real working scenario. It will good, if anyone come with some real working example.

It provides a neat way of sending multiple options as a single integer parameter.

For example, let's say you have a table called "Locations" with 2 columns, LocationID and Location, containing the following values:




I'm confused. Doesn't 2 (Dublin) and 8 (Berlin) equal 10 not 2 (Dublin) and 4 (Paris)? What am I missing?

Sorry

You're absolutely right - I should have said Dublin and Berlin.

Oh good. I was beginning to think I did not understand bitwise AND at all!
Post #1469621
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse