4

I have the following SQL code:

SELECT eml AS "Email Address"
FROM emailtbl
WHERE DEPT LIKE 'E%'

My issue is that if the department starts in 'F' I must select fax instead of eml. What could I do to select eml if the dept starts with 'E' and select fax if the dept starts in 'F'

Thanks

2 Answers 2

9

One option is to use a UNION ALL statement

SELECT eml AS "Entity"
FROM   emailtbl
WHERE  DEPT LIKE 'E%'
UNION ALL
SELECT fax AS "Entity"
FROM   emailtbl
WHERE  DEPT LIKE 'F%'

another one is to use a CASEstatement

SELECT CASE WHEN Dept LIKE 'E%' THEN eml ELSE fax END AS "Entity"
FROM   emailtbl
WHERE  DEPT LIKE 'E%' OR DEPT LIKE 'F%'
Sign up to request clarification or add additional context in comments.

Comments

1

How about this?

IF (SUBSTRING(@useLike,1,1) = 'E')
BEGIN
    SELECT eml AS "Email Address"
    FROM emailtbl
    WHERE DEPT LIKE @useLike

END
ELSE
BEGIN
    SELECT fax AS "Email Address"
    FROM emailtbl
    WHERE DEPT LIKE @useLike
END

2 Comments

Actually now the page has refreshed; Lieven anwser is pretty cool.
You mean to say it wasn't before? ;)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.