Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
sql_server 217
t-sql 211
tsql 116
sqlserver 96
BRH 78
#SQLServer 66
#TSQL 56
SQL Server 34
function 11
SSMS 9

Archive · View All
August 2007 17
August 2010 8
June 2012 7
June 2011 7
November 2007 7
August 2012 6
May 2012 6
November 2011 6
August 2011 6
October 2011 6

Madhivanan's TSQL Blog

Question of the month July 2012 - How does COALESCE(null,null+1,null) work?

Jul 11 2012 12:00AM by Madhivanan   

As you know COALESCE function returns the first non NULL value from the list.

When you execute the following

select coalesce(null,null,null)
you will get the following error
Msg 4127, Level 16, State 1, Line 1
At least one of the arguments to COALESCE must be an expression that is not the NULL constant.
But when you execute the following
select coalesce(null,null+1,null)

It returns NULL

Question : How does the second query work although NULL+1 is NULL?

Tags: 


Madhivanan
3 · 39% · 12441
13
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

11  Comments  

  • When I execute this query in 2008 R2 it get following msg: At least one of the arguments to COALESCE must be a typed NULL.

    And in 2012 I get: At least one of the arguments to COALESCE must be an expression that is not the NULL constant.

    The message description means that at least one parameter should be typed NULL, means should have a datatype. And when you add 1 to the NULL, the SQL Server engine automatically type casts the second param to INT.

    Thus your first query fails and second one executes.

    commented on Jul 11 2012 12:25AM
    Manoj
    245 · 1% · 183
  • I guess it's connected with this note in Book Online: "At least one of the null values must be a typed NULL.". In the first query(select coalesce(null,null,null) ) all arguments are not typed NULLs.

    By the way, when I run first query it returns this error:

    Msg 4127, Level 16, State 1, Line 1 At least one of the arguments to COALESCE must be a typed NULL.

    commented on Jul 11 2012 12:27AM
    Olga Medvedeva
    66 · 3% · 843
  • The COALESCE() is an expression; expressions have a data type. Your first example cannto be typred. Anohter problem is that the data type is the highest type in the list. Noobs will add "ELSE 'nothing' in an attempt to do display fomatting in the DB, but the CAST all the THEN clauses to strings

    commented on Jul 11 2012 10:25AM
    jcelko
    444 · 0% · 87
  • 'NULL' value in T-SQL can be in two forms. That's typed and untyped. Typed null is when a variable gets a null value, yet having a data type. Untyped is when you directly use the null value (without using a variable). But when you say 'NULL+1' that expression will be evaluated and type casted into 'INT'. It seems since the COALSCE expects at leasts one 'typed' value in it's input parameter list, you will not get any error in your second expression. Check the following code sample

    declare 
      @v1 as int = null
      ,@v2 as int = null
      ,@v3 as int = null
      
      
      select coalesce(@v1,@v2,@v3)
      

    It will return NULL

    commented on Jul 15 2012 8:50PM
    Manjuke
    2895 · 0% · 2
  • Could you explain working: select COALESCE(NULL * 3, '') select COALESCE(NULL * 3, NULL) why it return 0 NULL Regards

    commented on Jul 27 2012 5:30AM
    gogul
    127 · 1% · 397
  • select COALESCE(NULL * 3, '') is giving 0 because, when you multiple NULL with 3 the result will be NULL, but it implicitly converts the NULL value to INT datatype.

    And because first param is NULL, so second param value cast'ed by INT is returned, which comes to be 0.

    Check this: select SQLVARIANTPROPERTY(COALESCE(NULL * 3, '') , 'BaseType')

    commented on Jul 27 2012 6:05AM
    Manoj
    245 · 1% · 183
  • Thanks:) Now it's clear. Regards

    commented on Jul 27 2012 6:28AM
    gogul
    127 · 1% · 397
  • I recalled my memory in the days of Visual Basic6 and trying to share my experience.

    I always use a statement ADODB.Recordser.Fields("empName") .Value & "" to handle NULL values efficiently.

    I have seen people handle it like - If isnull(ADODB.Recordser.Fields("empName") .Value) then txtEmpName.Text = "" else txtEmpName.Text = ADODB.Recordser.Fields("empName") .Value End If

    Coming to the point, it converts Null value to string. Same thing is happening in the SQL server too, but, depends upon what type of value you add with null for e.g.

    If you run below script on SQL server and see the definition using sp_help CoalesceTest you will fine it is returning int and varchar data type.

    create view CoalesceTest as select coalesce(null, null+1, null) as CoalesceValue1 ,coalesce(null, null+'', null) as CoalesceValue2 ,coalesce(null, null+'2012-06-30', null) as CoalesceValue3

    Summary, null+1 will no longer remain actual "null it will null string!

    commented on Aug 6 2012 4:19AM
    nilesh.argade
    1493 · 0% · 13
  • RE: SELECT(COALESCE(null * 3, '') returning NULL

    Manoj is correct. It depends upon the datatype.

    Another way to see this is SELECT(COALESCE(null + 'x','y')) This will return the value 'y'.

    But SELECT(COALESCE(null + 1,'y')) will return this error message: Syntax error converting the varchar value 'y' to a column of data type int.

    However SELECT(COALESCE(null + 'y', 1)) will correctly return the value 1.

    commented on Aug 7 2012 8:51AM
    Cris
    191 · 1% · 242
  • But getting to the original question:

    The reason why

    select coalesce(null,null,null) returns an error

    but

    select coalesce(null,null + 1,null) returns null

    is because "null + 1" is not a "null constant".

    The expression "null + 1" has to get evaluated. It is run through some functions which evaluate to a null value.

    commented on Aug 7 2012 9:07AM
    Cris
    191 · 1% · 242
  • Another question would be...

    Is there ever a real world case for including the NULL keyword in a COALESCE call?

    COALESCE(NULL, AddressLine1, @myVar1, NULL, AddressLine2, @myVar2, NULL)

    Under what circumstance would any of the NULL's above be of any significance?

    Would the result ever be any different than::

    COALESCE(AddressLine1, @myVar1, AddressLine2, @myVar2)

    commented on Aug 7 2012 10:33AM
    Cris
    191 · 1% · 242

Your Comment


Sign Up or Login to post a comment.

"Question of the month July 2012 - How does COALESCE(null,null+1,null) work?" rated 5 out of 5 by 13 readers
Question of the month July 2012 - How does COALESCE(null,null+1,null) work? , 5.0 out of 5 based on 13 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]