Tuesday, July 10, 2012
The Argument Against Bit
I know a column of bit data type is very helpful as a flag or a code with only two valid values (three if you count NULL). However, I rarely use this data type to signify codes.
Say the column is used to identify an account status, which could be open or closed. More often than that, though, additional information is usually requested like when was it closed or when was it open. In this case, an AccountOpenDate and AccountCloseDate of date data type is more helpful.
I'm sure bit has its use, but sometimes I believe it's better to use a different data type that can hold other information than just three values.
As always, comments are welcome...
~~ CK
Thursday, March 1, 2012
Product() Aggregate Funcion
According to BOL, aggregate functions perform a calculation on a set of values and return a single value. However, I can't find a PRODUCT() function. Looks like it's not one of the aggregate functions provided by T-SQL.
Consider this data set:
~~ CK
Consider this data set:
custid Factor ----------- ----------- 1 2 1 3 2 4 2 5 2 2 3 6 3 7 3 3 4 8 4 9To produce this result set:
custid Product ----------- ---------- 1 6 2 40 3 126 4 72Here's the code:
select custid, POWER(10.,SUM(LOG10(Factor))) from SampleTable group by custidAs usual, comments are welcome...
~~ CK
Sunday, February 26, 2012
The Caveat of IsNull()
IsNullI() is a very handy function to replace the value of an expression with another, if the expression being tested is NULL. However, this function should be use with care. Remember always that the expression being checked and the replacement value should have the same data type or some unexpected (and sometimes undetectable) result may occur. As a rule, the function will return an expression with the same data type of that of the expression being checked.
Considering the following code:
What's going on? It's because the variable @a is a bit data type. The function will explicitly convert the second expression into the data type of the first expression. On the sample code, 9 will be converted into a bit expression which will return 1. So the expression isnull(@a, 9) = 9 will always be false. The entire code will not return an error and is not immediate visible.
This can also happen with strings. Consider the following code:
So make sure to check the data types of those two parameters.
As always, comments are welcome...
~~ CK
Considering the following code:
declare @a as bit select test_result = case when isnull(@a, 9) = 9 then 'Nine' when @a = 1 then 'One' when @a = 0 then 'Zero' else 'Unknown' endThe above code will never return the value 'Nine', even if at first glance, the code above should return the string 'Nine'.
What's going on? It's because the variable @a is a bit data type. The function will explicitly convert the second expression into the data type of the first expression. On the sample code, 9 will be converted into a bit expression which will return 1. So the expression isnull(@a, 9) = 9 will always be false. The entire code will not return an error and is not immediate visible.
This can also happen with strings. Consider the following code:
declare @a as varchar(5) select isnull(@a, 'abcdefghij')The above code will return the first 5 characters ('abcde') only. Why? Because this time, the variable @a is 5-character string. So even if it looks like the entire replacement string should have been returned by the function, again, the second expression will be converted into the first expression resulting to data truncation.
So make sure to check the data types of those two parameters.
As always, comments are welcome...
~~ CK
Subscribe to:
Posts (Atom)