473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 9197
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
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.