LL - Union All - Conversion failed when converting the varchar value to data type int

in #sql4 years ago

SQL union between NULL and VARCHAR error

NULL is a member of every data type.

When any SELECT query is being run,
the contents of each column must be of one type, and only one type.
When there are a mixture of values in a column (including NULLs),
the type can obviously be determined by examining the types of the non-NULL values,
and appropriate conversions are performed, as necessary.

But, when all rows contain NULL for a particular column,
and the NULL hasn't been cast to a particular type, then there's no type information to use.
So, SQL Server, somewhat arbitrarily, decides that the type of this column is int.

Sort:  

Hello,
Did you try a VARCHAR(https://codingsight.com/the-best-sql-varchar-dos-and-donts-for-faster-databases/)? It is one of the most common datatypes and exists in all popular DBMS systems like Oracle, Microsoft SQL Server, PostgreSQL. or can declare VARCHAR columns or variables using VARCHAR[(n)], where n stands for the string size in bytes.