Showing posts with label similar. Show all posts
Showing posts with label similar. Show all posts

Friday, February 24, 2012

IIF Statement on Select

Is there a way in an SQL Select statement to set up something similar to the
IIf command in Access:
Select LastName, IIf(CodeA = "Y","Yes'","No")
From NameTablermcompute,
Try using a "case" expression.
Select LastName, case when CodeA = 'Y' then 'Yes' else 'No' end as CodeA
From NameTable
AMB
"rmcompute" wrote:

> Is there a way in an SQL Select statement to set up something similar to t
he
> IIf command in Access:
> Select LastName, IIf(CodeA = "Y","Yes'","No")
> From NameTable|||There is no IIF in SQL Server, check out the CASE expression in Books
Online. Also, suggest you stay away from " as string delimiters.
SELECT LastName, CodeA = CASE CodeA WHEN 'Y' THEN 'Yes' ELSE 'No' END
FROM NameTable
Or
SELECT LastName, CodeA = CASE WHEN CodeA = 'Y' THEN 'Yes' ELSE 'No' END
FROM NameTable
"rmcompute" <rmcompute@.discussions.microsoft.com> wrote in message
news:2DA1A89A-17DA-4E9C-AF6F-AC37C2337277@.microsoft.com...
> Is there a way in an SQL Select statement to set up something similar to
> the
> IIf command in Access:
> Select LastName, IIf(CodeA = "Y","Yes'","No")
> From NameTable|||Thank you.
"Alejandro Mesa" wrote:
> rmcompute,
> Try using a "case" expression.
> Select LastName, case when CodeA = 'Y' then 'Yes' else 'No' end as CodeA
> From NameTable
>
> AMB
> "rmcompute" wrote:
>|||Thank you.
"Aaron Bertrand [SQL Server MVP]" wrote:

> There is no IIF in SQL Server, check out the CASE expression in Books
> Online. Also, suggest you stay away from " as string delimiters.
> SELECT LastName, CodeA = CASE CodeA WHEN 'Y' THEN 'Yes' ELSE 'No' END
> FROM NameTable
> Or
> SELECT LastName, CodeA = CASE WHEN CodeA = 'Y' THEN 'Yes' ELSE 'No' END
> FROM NameTable
>
> "rmcompute" <rmcompute@.discussions.microsoft.com> wrote in message
> news:2DA1A89A-17DA-4E9C-AF6F-AC37C2337277@.microsoft.com...
>
>

Sunday, February 19, 2012

IIF in SQL server

Is there a way in SQL Server to run the simple query
having something like IIF function in Access.
Similar to this easy query.
SELECT Mobile,iif([PlanType]=1, "New", "Upgrade") As Type FROM Acts WHERE
(RepId = 2194)
Thanks,
Michael
On Wed, 27 Oct 2004 12:51:12 -0700, MichaelK wrote:

>Is there a way in SQL Server to run the simple query
>having something like IIF function in Access.
>Similar to this easy query.
>SELECT Mobile,iif([PlanType]=1, "New", "Upgrade") As Type FROM Acts WHERE
>(RepId = 2194)
>Thanks,
>Michael
>
Hi Michael,
SELECT Mobile,
CASE
WHEN PlanType = 1
THEN 'New'
ELSE 'Upgrade'
END AS Type
FROM Acts
WHERE RepId = 2194
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)