Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T-SQL Query


T-SQL Query

Author
Message
RBarryYoung
RBarryYoung
SSCrazy Eights
SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)

Group: General Forum Members
Points: 9456 Visits: 9517
FYI, it is ALWAYS possible to construct a mathematical expression that will fit ANY finite set of numbers. for instance:

WHERE (month(dob)-2) * (month(dob)-6) * (month(dob)-7) * (month(dob)-8) * (month(dob)-11) * (month(dob)-12) = 0



Works just as well.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
timothyawiseman
timothyawiseman
SSC Eights!
SSC Eights! (804 reputation)SSC Eights! (804 reputation)SSC Eights! (804 reputation)SSC Eights! (804 reputation)SSC Eights! (804 reputation)SSC Eights! (804 reputation)SSC Eights! (804 reputation)SSC Eights! (804 reputation)

Group: General Forum Members
Points: 804 Visits: 920
There are almost always multiple ways of doing something. They would both be graceless and inelegant but you could use a series of union all statements and you can also do a join with another table or table like object (table variable, view, etc) which contained the desired months.

---
Timothy A Wiseman
SQL Blog: http://timothyawiseman.wordpress.com/
john.arnott
john.arnott
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1474 Visits: 3059
rbarryyoung, I don't quite follow your code, there. Somethng missing?

Anyway, it did inspire what may be the most obscure solution yet. We define an integer to hold what is essentially an array of bit-switches, then test the birth month against it with a modulo-remainder.

Declare @SelectionSwitches int
Set @SelectionSwitches = power(2,2)
+power(2,6)
+power(2,7)
+power(2,8)
+power(2,11)
+power(2,12)

Declare @Dob datetime

Set @Dob='2/24/08'
Select @Dob as Dob
,case when ((@SelectionSwitches/power(2,datepart(mm,@Dob)))%2)
= 1
then 'Yes' else 'No'
end
as Is_Selected


RBarryYoung
RBarryYoung
SSCrazy Eights
SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)

Group: General Forum Members
Points: 9456 Visits: 9517
john.arnott (4/16/2008)
rbarryyoung, I don't quite follow your code, there. Somethng missing?

That was just the Where clause. Here is the whole query:

Select *
From stud_mast
WHERE ( month(dob)-2)
* (month(dob)-6)
* (month(dob)-7)
* (month(dob)-8)
* (month(dob)-11)
* (month(dob)-12) ) = 0



-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
john.arnott
john.arnott
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1474 Visits: 3059
Thanks, rbarry.

What was missing was on my end -- should have looked more closelyBlush. Now it's obvious that any of the sought-after months would make one of the differences equal zero and that in turn forces the entire product to zero.
Mike Dougherty
Mike Dougherty
SSC-Enthusiastic
SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)

Group: General Forum Members
Points: 112 Visits: 952
If we're looking for obscure/inefficient ways to produce the requested output:

select *
from stud_mast o
where
(select month( dob ) from stud_mast i where i.pk = o.pk) != 1
and
(select month( dob ) from stud_mast i where i.pk = o.pk) != 3
and
(select month( dob ) from stud_mast i where i.pk = o.pk) != 4
and
(select month( dob ) from stud_mast i where i.pk = o.pk) != 5
and
(select month( dob ) from stud_mast i where i.pk = o.pk) != 9
and
(select month( dob ) from stud_mast i where i.pk = o.pk) != 10


Perhaps the question should not have asked "is there any other way" (there are almost always other/worse ways) but could have asked "is there a more efficient way than.."

I think if we're going to be challenged with writing different SQL, it should be to produce more useful code - not less.
StarNamer
StarNamer
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1294 Visits: 1992
Mike Dougherty (4/17/2008)
If we're looking for obscure/inefficient ways to produce the requested output:
...
I think if we're going to be challenged with writing different SQL, it should be to produce more useful code - not less.

The alternative is to start an Obfuscated T-SQL contest where entrants have to write an obscure query either to acheive a given result or which people have to work out what it does without actually running it.

A couple of us (briefly) tried to think of a scenario where the number of characters in the month of a student's date of birth would be significant and hence the query would be appropriate, but couldn't come up with a beleivable one.

The only idea that came close was printing the dates on certificates (or similar) where someone needed to decide which ones would need to be offset by half a character in order to center them. But it's a very contrived situation.

Derek
Terry Maher
Terry Maher
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 45
Living in France, the proposed solution would not work in French, just like a previous post mentions about it not working in German. Relying on the characteristics of a specific language for a general algorithm is always a really bad idea, especially in this era of globalization.
Amit-695544
Amit-695544
Mr or Mrs. 500
Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)

Group: General Forum Members
Points: 527 Visits: 16
hmmmmmmmm Smile
You r right dude !!!
Norma Jean Claeys
Norma Jean Claeys
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 11
select * from stud_mast
join (select 2 choice union select 6 union select 7
union select 8 union select 11 union select 12) choices
where month(dob) = choice
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