Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two columns of a table into a single column

Tags:

sql

sql-server

In my Employee table there are two columns named Empid, EmpNm

Empid EmpNm
001   Null
002   Ram
Null  Akhaya
Null  Tom
005   Satya

Two combine both the column I am using the query like

SELECT (Empid + EmpNm) FROM Employee

And the result is like

Null
002Ram
Null
Null
005Satya

But my requirement is like

001
002Ram
Akhaya
Tom
005Satya

Is there any suggestion to this query from your side ?

like image 560
samar Avatar asked Dec 30 '25 16:12

samar


2 Answers

try this,it will help you..

SELECT (ISNULL(Empid,'') +ISNULL(EmpNm,'')) AS name FROM dbo.Employee
like image 82
vidisha Avatar answered Jan 02 '26 07:01

vidisha


When concatenating null with something the result will be null. You can use coalesce to take care of that. Coalesce will return the first non null argument.

This will return a result set

select coalesce(Empid, '') + coalesce(EmpNm, '') as Empid
from Employee

This will update the Empid column in table Employee

update Employee 
set EmpID = coalesce(Empid, '') + coalesce(EmpNm, '') 
like image 24
Mikael Eriksson Avatar answered Jan 02 '26 07:01

Mikael Eriksson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!