Sample Header Ad - 728x90

How to concatenate two column of different types in sql? (without getting some strange warning)

5 votes
1 answer
1478 views
When I try to concatenate two columns, of different type, I get the following warning: > Type conversion in the expression > (CONVERT_IMPLICIT(varchar(41), [TABLE].[COLUMN], 0)) can > affect "CardinalityEstimate" in choosing query plan. By doing a search... I seem to have understood that this warning can derive from the comparison of two columns of different types. For this reason I wanted to carry out some simple tests to deepen the thing:
SQL SERVER
CREATE TABLE TABLE_A (
	ID DECIMAL(5, 0) NOT NULL,
	COLUMN_A VARCHAR(255) NULL
)

INSERT TABLE_A (ID, COLUMN_A) VALUES (1, 'TEST_1')
INSERT TABLE_A (ID, COLUMN_A) VALUES (2, 'TEST_2')
INSERT TABLE_A (ID, COLUMN_A) VALUES (3, 'TEST_3')
Trying to concatenate with the operator, two columns of different types, an error is returned: "Error converting data type from varchar to numeric."
SQL SERVER
SELECT ID + COLUMN_A
FROM TABLE_A
If I try to do the same thing but converting the "ID" column, I no longer have the error but I get the previously mentioned warning.
SQL SERVER
SELECT CAST(ID AS VARCHAR(255)) + COLUMN_A
FROM TABLE_A
I have the same result if I try to concatenate using the concat() function, no error but I have the warning.
SQL SERVER
SELECT CONCAT(ID, COLUMN_A)
FROM TABLE_A
My question is this, how is it possible that I get a warning about comparing two columns even when they should be equal when I cast? How can I concatenate two columns of different types without that warning being returned? Thanks in advance, Giacomo.
Asked by G. Ciardini (153 rep)
Feb 14, 2023, 11:31 AM
Last activity: Feb 14, 2023, 11:58 AM