I am working on a SQL view which extracts data from a table with multiple columns in it and returns single column with data combined form two columns for a table separated by a slash , but I want the view to return the data such that whenever one of the two column values is null then it should return the value which is not NULL
without any slash in it and if both the values are NULL
then it should be empty or NULL
For Example,
I am creating a view which returns two columns named Give
and Quantity
from a table in which there are four records for ID(0,1,2,3)
, Give (NULL, 90, NULL, 60)
and Quantity
column as NULL, 110, 99, 100.
I have been able to create a view which return these values in the following order
Give/Quantity
/
90/110
/99
60/100
But I want the view which returns something like following
Give/Quantity
Empty
90/110
99
60/100
As we can see from above that if either Give
or Quantity
is NULL
then it should discard slash / and return the value that is NOT NULL
May I know a better way to achieve this.
I have the following query for the view
CREATE VIEW [dbo].[Result]
AS
with CTE as(
SELECT distinct
CC.Term
iCC.Give,
iCC.Volume
FROM Cust CC
CROSS APPLY (SELECT TOP 3
Give,
Quantity,
ISNULL(cast(iCC.Give as varchar(99)),'') + '/' + ISNULL(cast(iCC.Qunatity as varchar(99)),'') as Give/Quantity,
FROM Cust iCC
WHERE CC.Term = iCC.Term
ORDER BY iCC.Give DESC) iCC)
select Term, Give, givelabel as label from CTE
union
select Product, Term, Quantity, quantitylabel from CTE
GO