Showing posts with label Math. Show all posts
Showing posts with label Math. Show all posts

Monday, May 14, 2012

T-SQL Conditional WHERE Clause

I started the day today with great ambition. I am tasked with removing hard coded SQL and change to use stored procedures. I made it through several stored procedures and was fat, dumb, and happy. Until… I was stumped with a problem I was unprepared to solve. The problem occurred in converting a bunch of case statements in the hard coded SQL.  The code looked something like this (changed for brevity)

Dim strSQL As New StringBuilder
strSQL.Append("SELECT TOP 300 cus_no ,cus_name, search_name, slspsn_no")
strSQL.Append("FROM MyTable")
If myValue1 IsNot Nothing Then
strSQL.AppendFormat("WHERE value1 = '{0}' ", myValue1)
ElseIf myValue2 Nothing Then
strSQL.AppendFormat("WHERE value2 = '{0}' ", myValue2)
ElseIf myValue3 IsNot Nothing Then
strSQL.AppendFormat("WHERE value3 LIKE '{0}%' ", myValue2)
End If
If accountType > 0 Then
strSQL.AppendFormat("AND value1 {0} IN (SELECT thisValue FROM MySecondTable", IIf(accountType = 1, "", "NOT"))
End If
So I decided to tackle the first if block first. I googled the hell out of conditional where clauses in T-SQL. After a little bit of reading and experimenting I finally decided to go with the COALESCE solution.  this was tricky because if a field is NULL and the parameter is NULL then COALESCE will not equal each other so for the three conditions in the first if block I came up with the following:
WHERE COALESCE(value1,' ') = COALESCE(@myValue1, COALESCE(value1,' '))
AND COALESCE(value2,' ') = COALESCE(@myValue2, COALESCE(value2,' '))
AND COALESCE(value3,' ') LIKE COALESCE(@myValue3, COALESCE(value3,' '))
This had the desired affect. Notice the first and third COALESCE in each line has the space.  This is how you account for the NULL = NULL situation.
Now on to what I consider the harder of the two types of conditional where clauses represented here. If accountType is supplied and it is equal to one then check to see if it is IN the list returned from the SELECT. If accountType is supplied and is greater than one then it should not be IN the SELECT statement. if accountType is not supplied then don’t bother adding it to the where clause. I found some great examples of bit comparisons in the google search but they all used equals instead of IN and I couldn’t get IN to work with the case statements but I combined a couple of good items into this solution:
AND ((@AccountType=1 AND value1 IN (SELECT thisValue FROM mySecondTable))
OR (@AccountType>1 AND value1 NOT IN(SELECT thisValue FROM mySecondTable))
OR (@AccountType<1 AND value1 = value1))
This covers all possible scenarios. Let me know if you have another way to accomplish this.

Till next time…
Technorati Tags: ,,,,

Tuesday, September 20, 2011

Math in Middle School and High School. How important is it?

Microsoft did a survey of College Students and parents about Science, Technology, Engineering, and Math (STEM). STEM, over the course of the next decade or 2, will be where most of the worldwide higher paying jobs will come from.  Are your Middle Schools and High Schools preparing your college bound child well enough for them to succeed in STEM?

According to the survey, the answer to the question is a big NO.  Most of the parents and college students surveyed believe they were not prepared enough in their respective secondary education institutions for what they needed in college.

Why do I think this is an important subject?  Because I believe it is true.  I am very fortunate to have a son in the 8th grade who is an honor student (He gets that from his mom.) My son, Brendan, is not a good student. He is a GREAT student. He has consistently scored the highest or one of the top 3 highest scores on his EOG (End of Grade) tests. North Carolina’s answer to “No child left behind”.  And he is consistently one of the best students in his grade.

Now, I am not one to brag. But I will now.  Brendan was invited to participate in Duke’s TIP program for seventh graders last year.  Through this program he was able to take the SATs with college bound 11th graders this past January.  He did extremely well. And now has all kinds of opportunities world wide that are available for him to participate in.

How did he get there? He was very fortunate to have math teachers who realized the simple truth that is stated in the results of Microsoft’s survey.  Math is important! In fact, in the Orange County School System, advanced math is emphasized and encouraged in the Middle Schools.  Brendan started with Algebra in 7th grade and is now taking Geometry.  (He is also taking English 1 so he will get High School credit for it.)

What does Brendan want to be when he grows up? An engineer!  He has already showed a preference for his mom’s alma-mater Purdue.  He also realizes that math is what is going to help him succeed.  (He also dreams of being a professional Goalie in the Premier League as well, oh well, so much for math.)

We as parents need to be a part of the equation (pun intended) as well. We need to encourage our kids at an early age to look at math as something fun and interesting. I remember driving Brendan to and from pre school singing our numbers to 1000 by tens. We had lots of fun.  So yeah, math is important, and fun too!

till next time…

Technorati Tags: ,,,