Link to home
Start Free TrialLog in
Avatar of spinzr0
spinzr0Flag for United States of America

asked on

Using a query in an if statement

I need to use an if statement to determine if one value is a 1 or 0.  If its a one, the query should be X, otherwise it should be y.  The easiest way to explain it is how I assumed the query would be:

IF(SELECT DEFAULT_EMAIL_LIMITS FROM [AD].[dbo].[V_1] = 1)
    SELECT [LOGON_ID],[EMAIL_SERVER],[DEFAULT_EMAIL_LIMITS],[WARNING_LIMIT],[SEND_LIMIT],[SEND_RECEIVE_LIMIT] FROM [AD].[dbo].[V_1]
ELSE
    SELECT [LOGON_ID],[EMAIL_SERVER],[DEFAULT_EMAIL_LIMITS],'default' as WARNING_LIMIT,'default' as SEND_LIMIT,'default' as SEND_RECEIVE_LIMIT FROM [AD].[dbo].[V_1]

If this isn't clear please let me know and I can explain more
SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cyberkiwi
If Warning_Limit, Send_Limit and Send_Receive_Limit is NUMERIC, then you need to consider:
A query can only return columns of a specific type.  So you cannot have it return string 'default' in one case and 60 in another case (morphing types).
If you are happy for the data to always come back as VARCHAR, you will have to cast the numeric data to varchar.
e.g.

SELECT [LOGON_ID],[EMAIL_SERVER],[DEFAULT_EMAIL_LIMITS],
convert(varchar(10),[WARNING_LIMIT]) as WARNING_LIMIT,
convert(varchar(10),[SEND_LIMIT]) as SEND_LIMIT,
convert(varchar(10),[SEND_RECEIVE_LIMIT]) as SEND_RECEIVE_LIMIT
FROM [AD].[dbo].[V_1]
WHERE DEFAULT_EMAIL_LIMITS = 1
UNION ALL
SELECT [LOGON_ID],[EMAIL_SERVER],[DEFAULT_EMAIL_LIMITS],
'default' as WARNING_LIMIT,
'default' as SEND_LIMIT,
'default' as SEND_RECEIVE_LIMIT
FROM [AD].[dbo].[V_1]
WHERE ISNULL(DEFAULT_EMAIL_LIMITS,0) <> 1
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of spinzr0

ASKER

Thanks for the quick help.