Thursday, May 29, 2014

t-sql 2008 r2 explain select

In existing t-sql 2008 r2, there is the following sql that I am trying to understand so that I can modify it. The field that is called tMask is definitely as an integer. The values stored in this field are hexadecimal values. The values indicate what fields contain a check box in them, For example, if checkbox 1 is selected then the value in this field = 1. If the value is 3, then the first and second checkboxes have been selected.


Based upon what I have said, can you tell me how to tell what the value = '42' means? What checkboxes have been selected? If the value = 1023, what checkboxes have been selected?


Can you explain to me the sql listed above?











select
, replace(rtrim(
case when max(case when b = 0 then convert(varchar(2), ((convert(smallint, convert(binary(2), tMask)) & e)) / e) else 0 end ) = 1 then '1 ' else '' end
+ case when max(case when b = 1 then convert(varchar(2), ((convert(smallint, convert(binary(2), tMask)) & e)) / e) else 0 end ) = 1 then '2 ' else '' end
+ case when max(case when b = 2 then convert(varchar(2), ((convert(smallint, convert(binary(2), tMask)) & e)) / e) else 0 end ) = 1 then '3 ' else '' end
+ case when max(case when b = 3 then convert(varchar(2), ((convert(smallint, convert(binary(2), tMask)) & e)) / e) else 0 end ) = 1 then '4 ' else '' end
+ case when max(case when b = 4 then convert(varchar(2), ((convert(smallint, convert(binary(2), tMask)) & e)) / e) else 0 end ) = 1 then '5 ' else '' end
+ case when max(case when b = 5 then convert(varchar(2), ((convert(smallint, convert(binary(2), tMask)) & e)) / e) else 0 end ) = 1 then '6 ' else '' end
+ case when max(case when b = 6 then convert(varchar(2), ((convert(smallint, convert(binary(2), tMask)) & e)) / e) else 0 end ) = 1 then '7 ' else '' end
+ case when max(case when b = 7 then convert(varchar(2), ((convert(smallint, convert(binary(2), tMask)) & e)) / e) else 0 end ) = 1 then '8 ' else '' end
+ case when max(case when b = 8 then convert(varchar(2), ((convert(smallint, convert(binary(2), tMask)) & e)) / e) else 0 end ) = 1 then '9 ' else '' end
+ case when max(case when b = 9 then convert(varchar(2), ((convert(smallint, convert(binary(2), tMask)) & e)) / e) else 0 end ) = 1 then '10 ' else '' end + case when max(case when b = 10 then convert(varchar(2), ((convert(smallint, convert(binary(2), tMask)) & e)) / e) else 0 end ) = 1 then '11 ' else '' enase when max(case when b = 11 then convert(varchar(2), ((convert(smallint, convert(binary(2), tMask)) & e)) / e) else 0 end ) = 1 then '12 ' else '' end
),' ',',') as [tMask] FROM [test].[dbo].[test1]join (select 11 as b)bits on 1=1







No comments:

Post a Comment