Here I will explain how to solve the problem of “COALESCE function: Conversion failed when converting the varchar value '24,' to data type int in SQL Server”.
Generally this problem will occur whenever we try to bind integer value to string like as shown below
Generally this problem will occur whenever we try to bind integer value to string like as shown below
DECLARE @userdetails VARCHAR(250)
SELECT @userdetails = COALESCE(@userdetails + ',', '') + UserId FROM UserDetails
SELECT @userdetails
In above query UserId is integer datatype because of that if I run above query I will get error like“Conversion failed when converting the varchar value '24,' to data type int.”
To solve this problem we need to convert that interger value to string explicitly like as shown below
DECLARE @userdetails VARCHAR(250)
SELECT @userdetails = COALESCE(@userdetails + ',', '') + CAST(UserId AS VARCHAR(15)) FROMUserDetails
SELECT @userdetails
Once we run above code our problem will get solve and return the output like as shown below
Output
UserDetails
24,25,26,29
No comments:
Post a Comment