Question: What is Coalesce Function?
Answer: Coalesce function returns the first non-null expression in the list. If All expressions evaluate to null Value, then the Coalesce function will return null value. Ms SQL IsNull function is used to specify how we want to treat NULL Values. Coalesce return same result as IsNull Function.
Question: What is Ltrim Function?
Answer: LTRIM to remove leading spaces from a character variable.
Step 1: Create A Table.
Create Table Emp(empid numeric(18,0) IDENTITY(1,1) PRIMARY KEY , EmpName varchar(50)).
Step 2: Insert Data.
- Insert Into Emp(empName) values('john')
- Insert Into Emp(empName) values('sachin')
- Insert Into Emp(empName) values('kamal')
- Insert Into Emp(empName) values('ram')
- Insert Into Emp(empName) values('smith')
- Insert Into Emp(empName) values('gopal')
Step 3 : See Result.
Result
Step 4: Comma Separated Value.
- Declare @vEmpName varchar(max)
- Select @vEmpName= Coalesce(@vEmpName + ',', '') + Ltrim(EmpName) From Emp
- Print @vEmpName
Result
If useful than please comment.