简体   繁体   中英

Using TO_CHAR within Group By Function not matching datatypes. Oracle SQL

i'm really struggling with something that I think should be pretty basic. I keep getting an error that says:

ORA-00932: inconsistent datatypes: expected NUMBER got NCHAR

  1. 00000 - "inconsistent datatypes: expected %s got %s"

Here is what my query looks like (with some extra parts taken out but this is the gist of it)

SELECT  
  TO_CHAR(COALESCE(ABC.Number,Name)) as "Editor"
FROM tableone ABC   
  LEFT OUTER JOIN tabletwo ON ABC.Number= tabletwo.Number  
WHERE ABC.Number <> 0  
GROUP BY
  TO_CHAR(COALESCE(ABC.Number,Name))

The Number field is created as INT and the Name field is VARCHAR(100). The group by is there as there is other stuff in this query that uses max and sum but I didn't want to fill this with anything extra. Any help is super appreciated.

This has nothing to do with the GROUP BY . The type incompatible is in the COALESCE() . I think you intend:

COALESCE(TO_CHAR(ABC.Number), Name)

COALESCE() is a function that returns a single type. It gets confused if the arguments are of different types.

The way I see it, you put TO_CHAR to the wrong place. Should be coalesce(to_char , not to_char(coalesce

SQL> with
  2  tab1 (cnumber) as
  3    (select 100 from dual
  4    ),
  5  tab2 (cnumber, cname) as
  6    (select 100, 'Little' from dual
  7    )
  8  select coalesce(to_char(a.cnumber), b.cname)
  9  from tab1 a left outer join tab2 b on a.cnumber = b.cnumber
 10  where a.cnumber <> 0
 11  group by coalesce(to_char(a.cnumber), b.cname);

COALES
------
100

SQL>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM