473,461 Members | 2,267 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Return Type of COALESCE?

Hi everybody,

VARCHAR has a higher precedence than CHAR. If you have a query

SELECT COALESCE(c1,c2) FROM T1

where c1 is CHAR(5) and c2 is VARCHAR(10), I would expect the return
type to be varchar(10) similiar to UNION. However it turns out to
be CHAR(10). Does anyone know why that is so?

Thanks,
Steffen
Jul 23 '05 #1
3 9199
I'm not sure why you are seeing this behavior. According to BoL
COALESCE acts just like a CASE statement. According to the
documentation there the precedence order is supposed to be followed for
data type conversions. Yet, when I run the code below I get
contradictory results:

DECLARE @test INT, @char CHAR(10), @varchar VARCHAR(10)
SET @test = 1
SET @char = 'char'
SET @varchar = 'varchar'
SELECT 'x' + CASE @test WHEN 1 THEN @varchar ELSE @char END + 'x'

I ran this on a system with ANSI_PADDING set to ON and it had spaces
between the string and the final 'x'. If I make both arguments VARCHAR
the trailing spaces are not there.

When I tested this with an INT and DECIMAL I saw the results that I
would expect there - the result is always returned as a DECIMAL.

-Tom.

Jul 23 '05 #2
COALESCE() is defined in terms of a CASE expression, so it should
follow the same rules about returning the highest data type in the
list. COALESCE correctly promotes its arguments to the highest data
type in the expression:

13 / COALESCE(CAST(NULL AS INTEGER), 2.00) = 6.5

The proprietary ISNULL() uses the first data type and gets things wrong

13 / ISNULL(CAST(NULL AS INTEGER), 2.00) = 6

You would need to write:

13 / ISNULL(CAST(NULL AS DECIMAL(4,2)), 2.00)

It sounds like the compiler is giving you an ISNULL().

Jul 23 '05 #3
--CELKO-- wrote:
COALESCE() is defined in terms of a CASE expression, so it should
follow the same rules about returning the highest data type in the
list. COALESCE correctly promotes its arguments to the highest data
type in the expression:

13 / COALESCE(CAST(NULL AS INTEGER), 2.00) = 6.5

The proprietary ISNULL() uses the first data type and gets things wrong

13 / ISNULL(CAST(NULL AS INTEGER), 2.00) = 6

You would need to write:

13 / ISNULL(CAST(NULL AS DECIMAL(4,2)), 2.00)

It sounds like the compiler is giving you an ISNULL().

I dont think so.
SELECT COALESCE(CAST (NULL AS CHAR(5)), CAST('a' as VARCHAR(10)) returns
'a ', a fixed length char.
SELECT COALESCE(CAST('a' as VARCHAR(10), CAST (NULL AS CHAR(5))) returns
the same. The order of the arguments doesnt change anything.
Only if all arguments are varchar, do I get varchar back.

Regards,
Steffen
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob Bedford | last post by:
I've a query like this: select coealesce(table1.code1, table2.code1) as mycode from... where mycode = 'XXX' But this query fails. Unknow column mycode in where clause. why ??? "coalesce()...
2
by: Jeff Roughgarden | last post by:
I am using a clever product called SQL Expert Pro to optimize problematic SQL statements. It works by generating syntactically identical variations of the original SQL, finding all unique execution...
5
by: John Greve | last post by:
Any ideas on why my the result 'green' row column DELTA does not yield -1 for? That is not how I expected COALESCE( ... ) to work. Every other row for DELTA (including 'black') comes out the...
7
by: KevinMGore | last post by:
I'm trying to create a getFunction that returns a single value from an sql table (the next sequence number actually in a group of records). I tried two different methods, a command and data...
4
by: Don | last post by:
If I have an SQL query which returns an aggregate of several decimal fields like so: (sum(COALESCE(myDecimal1, 0)+ sum(COALESCE(myDecimal2, 0)+ sum(COALESCE(myDecimal3, 0)) as MyTotal I get...
10
by: db2group88 | last post by:
hi, we are using db2 v8.2 EE on windows, with jcc driver. try to create a join view, after that i check the syscat.columns table, for those decimal field use COALESCE method, all the length become...
17
by: pbd22 | last post by:
Hi. How does one return a range of rows. I know that "Top 5" will return rows 0 - 5 but, how do I get 6 - 10? thanks
4
by: lenygold via DBMonster.com | last post by:
Hi everybody! I am truing to concstruct something like this: SELECT id, name, role, CASE WHEN role = 1 THEN SELECT item1, item2, item3 FROM table1 WHEN role = 2 THEN SELECT item1, item2,...
1
by: =?Utf-8?B?R1ROMTcwNzc3?= | last post by:
Hi All, is this the correct way to Coalesce a session var - <%Session("EMPLOYETOKENS") = tokens.Fields.Item(COALESCE(SUM("JBCLTokens"),0)).Value%? Thank you
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.