Hi i have one doubt in sql server .
if any field donot have values then need to provide which filed donot have value
if data not available in one filed then output field value not available.
if data not available more than one filed then output those fields value are not available.
concate multiple fileds when values not exists.
sample data :
CREATE TABLE [dbo].[EmpDetails](
[Empid] [int] NULL,
[Empname] varchar NULL,
[Location] varchar NULL,
[Deptid] [int] NULL,
[Deptname] varchar NULL
)
INSERT [dbo].[EmpDetails] ([Empid], [Empname], [Location], [Deptid], [Deptname]) VALUES (1, NULL, N'che', 10, N'hr')
INSERT [dbo].[EmpDetails] ([Empid], [Empname], [Location], [Deptid], [Deptname]) VALUES (2, N'hari', N'pune', NULL, N'pm')
INSERT [dbo].[EmpDetails] ([Empid], [Empname], [Location], [Deptid], [Deptname]) VALUES (3, N'var', NULL, 30, NULL)
INSERT [dbo].[EmpDetails] ([Empid], [Empname], [Location], [Deptid], [Deptname]) VALUES (4, NULL, NULL, NULL, N'hr')
INSERT [dbo].[EmpDetails] ([Empid], [Empname], [Location], [Deptid], [Deptname]) VALUES (NULL, N'venu', N'pune', NULL, NULL)
INSERT [dbo].[EmpDetails] ([Empid], [Empname], [Location], [Deptid], [Deptname]) VALUES (NULL, N'kumar', N'pune', 20, NULL)
INSERT [dbo].[EmpDetails] ([Empid], [Empname], [Location], [Deptid], [Deptname]) VALUES (8, 'ravi', NULL, 10, N'hr')
INSERT [dbo].[EmpDetails] ([Empid], [Empname], [Location], [Deptid], [Deptname]) VALUES (10, N'k', N'pune', 20, N'hr')
based on above data I want output like below :
empid | Empname | Location | Deptid| Deptname | Validate
1 | NULL |Che |10 | hr | Empname value is not available
2 | hari |pune |NULL | pm | Deptid value is not available
3 | var |NULL |30 | NULL | location and deptname values are not available
4 | NULL |NULL |NULL | hr | empname and location and deptid values are not available
NULL | venu |pune |NULL | NULL | empid and deptid and deptname values are not available
NULL | kumar |pune |20 | NULL | empid and deptname values are not available
8 | ravi |NULL |10 | hr | location value is not available
10 | k |pune |20 | hr |
I tried like below :
select empid,empname,location,deptid,deptname,
case when coalesce(empid,'')='' then 'Empid' else '' end + ' '+
case when coalesce(empname,'')='' then 'Empname' else '' end + ' '+
case when coalesce(Location,'')='' then 'Location' else '' end + ' '+
case when coalesce(Deptid,'')='' then 'Deptid' else '' end +' '+
case when coalesce(Deptname,'')='' then 'Deptname' else '' end +' '+
- 'value not available' as Validate
FROM [Test].[dbo].[EmpDetails]
but above query not given expected format
please tell me how to write query to achive this task in sql server