Balaji Ravichandran
Balaji Ravichandran

Reputation: 167

How to hide column header from the result set output?

I have a piece of SQL query which returns the result set with email_address column name on the top row like this:

|   email_adress    |
|-------------------|
| [email protected]    |
| [email protected] |

What I need instead a result set without the first row like:

| [email protected]    |
| [email protected] |

Here's my query:

SELECT email_address
FROM company_digital
WHERE email_address IS NOT NULL
AND email_address IS NOT NULL
AND hash_id >= 700
AND hash_id < 800
UNION
SELECT email_address_2
FROM company_digital
WHERE email_address_2 IS NOT NULL
AND email_address_2 IS NOT NULL
AND hash_id >= 700
AND hash_id < 800
UNION
SELECT email_address_3
FROM company_digital
WHERE email_address_3 IS NOT NULL
AND email_address_3 IS NOT NULL
AND hash_id >= 700
AND hash_id < 800;

Upvotes: 2

Views: 10696

Answers (3)

Post Impatica
Post Impatica

Reputation: 16373

This is probably not what the OP would want but still posting this for community. You can also do a cast, like this:

select CAST(email_address as nvarchar)

even though email_address is already an nvarchar, this works.

Upvotes: 1

Manel Dassanayake
Manel Dassanayake

Reputation: 9

Try this:

SET FMTONLY ON;
SELECT email_address
FROM company_digital
WHERE email_address IS NOT NULL
AND email_address IS NOT NULL
AND hash_id >= 700
AND hash_id < 800
UNION
SELECT email_address_2
FROM company_digital
WHERE email_address_2 IS NOT NULL
AND email_address_2 IS NOT NULL
AND hash_id >= 700
AND hash_id < 800
UNION
SELECT email_address_3
FROM company_digital
WHERE email_address_3 IS NOT NULL
AND email_address_3 IS NOT NULL
AND hash_id >= 700
AND hash_id < 800;

SET FMTONLY OFF;

Upvotes: 0

Matt
Matt

Reputation: 15061

Assuming your mean the column header then give the first field in the query an alias like:

select email_address AS Whatever
from company_digital
where email_address is not null
and email_address is not null
and hash_id >=700 and hash_id <800
union
select email_address_2
from company_digital
where email_address_2 is not null
and email_address_2 is not null
and hash_id >=700 and hash_id <800
union
select email_address_3
from company_digital
where email_address_3 is not null
and email_address_3 is not null
and hash_id >=700 and hash_id <800;

Which would output:

| Whatever |
_____________
[email protected]

[email protected]
_________________

Or

select email_address AS ' '
from company_digital
where email_address is not null
and email_address is not null
and hash_id >=700 and hash_id <800
union
select email_address_2
from company_digital
where email_address_2 is not null
and email_address_2 is not null
and hash_id >=700 and hash_id <800
union
select email_address_3
from company_digital
where email_address_3 is not null
and email_address_3 is not null
and hash_id >=700 and hash_id <800;

Which would output:

|  |
_____________
[email protected]

[email protected]
_________________

EDIT CLEAN UP CODE:

SELECT email_address AS ' '
FROM company_digital
WHERE email_address IS NOT NULL
AND hash_id BETWEEN 700 AND 800
UNION
SELECT email_address_2
FROM company_digital
WHERE email_address_2 IS NOT NULL
AND hash_id BETWEEN 700 AND 800
UNION
SELECT email_address_3
FROM company_digital
WHERE email_address_3 IS NOT NULL
AND hash_id BETWEEN 700 AND 800;

Or depending on your data...

SELECT CASE WHEN email_address IS NOT NULL 
            THEN email_address
            WHEN email_address_2 IS NOT NULL 
            THEN email_address_2 
            WHEN email_address_3 IS NOT NULL 
            THEN email_address_3
       END AS ' '
FROM company_digital
WHERE hash_id BETWEEN 700 AND 800

Upvotes: 1

Related Questions