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
Microsoft SQL Server 2005SQL

Avatar of undefined
Last Comment
spinzr0

8/22/2022 - Mon
SOLUTION
Erick37

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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
cyberkiwi

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
spinzr0

Thanks for the quick help.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck