4

I Have a table dbo.ArtikelAlternatief created like this:

CREATE TABLE [dbo].[ArtikelAlternatief]( 
    [Barcode] [varchar](50) NOT NULL,
    [BarcodeAlternatief] [varchar](50) NOT NULL,   
 CONSTRAINT [PK_ArtikelAlternatief] PRIMARY KEY CLUSTERED 
(
    [Barcode] ASC,
    [BarcodeAlternatief] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Now I want the following results combined:

select BarcodeAlternatief AS 'Barcode' from dbo.ArtikelAlternatief where Barcode like '7630015711115' 
select Barcode AS 'Barcode' from dbo.ArtikelAlternatief where BarcodeAlternatief like '7630015711115'

How is it possible to combine those 2 query's in one result column?

4 Answers 4

2

You can do it in 3 methods.

SQLFIDDLE

Method 1: Using CASE Statement:

select 
     (case when Barcode = '7630015711115' 
            then BarcodeAlternatief
            else Barcode END) as 'Barcode'  
    from ArtikelAlternatief
    where Barcode = '7630015711115'
          or BarcodeAlternatief = '7630015711115';

Method 2: You can try using DECODE statement (Of oracle),

SELECT  DECODE (BarcodeAlternatief , '7630015711115', Barcode , BarcodeAlternatief ) AS Barcode 
FROM dbo.ArtikelAlternatief 
where Barcode = '7630015711115' OR BarcodeAlternatief = '7630015711115'

Method 3:

Try below query using UNION ALL:

select BarcodeAlternatief AS 'Barcode' from dbo.ArtikelAlternatief 
where Barcode = '7630015711115' 

UNION ALL

select Barcode AS 'Barcode' from dbo.ArtikelAlternatief 
where BarcodeAlternatief = '7630015711115'
  1. If you wish to allow duplicates, then use UNION ALL. If you do not wish to allow duplicates, then use UNION.
  2. In your case, you can use = operator instead of LIKE in where condition because you are not doing any pattern matching.
0
2

Use the UNION operator:

query1
UNION ALL
query2

The ALL keyword is optional, and used if you want duplicate rows.

1

Put 'OR' and 'CASE' function condition :

select 
     (case when Barcode like '7630015711115' then BarcodeAlternatief
           when BarcodeAlternatief like '7630015711115' the Barcode 
            else '') as 'Barcode'  
    from dbo.ArtikelAlternatief
    where Barcode like '7630015711115'
          or BarcodeAlternatief like '7630015711115'
1
  • Please, try and verify with expected output Aug 22, 2014 at 10:20
1

U may try this

select BarcodeAlternatief AS 'Barcode' from dbo.ArtikelAlternatief where Barcode like '7630015711115' 

Union All

select Barcode AS 'Barcode' from dbo.ArtikelAlternatief where BarcodeAlternatief like '7630015711115'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.