Combining two columns of a table and returning it as a single column separated by 'slash' if both the values are NOT NULL

StackOverflow https://stackoverflow.com/questions/23090660

  •  04-07-2023
  •  | 
  •  

Question

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
Was it helpful?

Solution

SELECT
Id,
CASE 
WHEN Give IS NOT NULL and Quanity IS NOT NULL THEN Give + '/' + Quanity
WHEN Give IS NOT NULL and Quanity IS NULL THEN Give
WHEN Give IS NULL and Quanity IS NOT NULL THEN Quanity
WHEN Give IS NULL and Quanity IS NULL THEN NULL
END as 'Give/Quanity'
from
View

If Give or/and Quanity is other type than varchar you might need to convert them to varchar so query looks like:

SELECT
Id,
CASE 
WHEN Give IS NOT NULL and Quanity IS NOT NULL THEN convert(varchar(50),Give) + '/' + convert(varchar(50),Quanity)
WHEN Give IS NOT NULL and Quanity IS NULL THEN Give
WHEN Give IS NULL and Quanity IS NOT NULL THEN Quanity
WHEN Give IS NULL and Quanity IS NULL THEN NULL
END as 'Give/Quanity'
from
Table

OTHER TIPS

Here's the simple solution using COALESCE:

create table #t(Give int, Quantity int)
insert #t values
(null, null),
(90, 110),
(null, 99),
(60, 100),
(23, null)

select coalesce(cast(give as varchar)+'/'+cast(quantity as varchar),cast(give as varchar),cast(quantity as varchar))
from #t
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top