Re: CASE statement

From: Dan Guzman (guzmanda_at_nospam-online.sbcglobal.net)
Date: 11/18/04


Date: Wed, 17 Nov 2004 22:17:42 -0600

The CASE syntax looks fine except that you should enclose the literal in
single quotes instead of double quotes so that it will work regardless of
the QUOTED_IDENTIFIER setting. For example:

SELECT
    CASE col1
        WHEN 1 THEN 'okay'
        WHEN 2 THEN p.LastName
    END As result
FROM MyTable p

-- 
Hope this helps.
Dan Guzman
SQL Server MVP
"smk23" <smk23@discussions.microsoft.com> wrote in message 
news:A7EFCFCB-FB8F-4E39-9F2A-931703906CE7@microsoft.com...
> newbie question:
> In a CASE statement, I would like the THEN clause to be the value of 
> another
> column in the same table. Is this possible? I don't see any examples of 
> that
> in BOL.
>
> What I am trying to do is something like:
> CASE col1
>   WHEN 1 THEN "okay"
>   WHEN 2 THEN p.LastName
> END As result
> but this syntax doesn't fly
>
> very grateful for the help
> -- 
> sam 


Relevant Pages

  • RE: Too Few Parameters Error
    ... I guess I was looking at it so long I didn't notice the syntax. ... You have the reference to the controls on ... YOu also have too many single quotes in the wrong places. ... >> Dim rst As DAO.Recordset ...
    (microsoft.public.access.formscoding)
  • Re: Date in file name
    ... Use single quotes around the ddmmyyyy instead of double quotes. ... I tells me I have a syntax error in the visual basic module ... Steve Schapel, Microsoft Access MVP ...
    (microsoft.public.access.macros)
  • Re: Help with script
    ... 9081710039~1~One Bad Firm ... what syntax? ... (assuming Windows, otherwise you might want to use single quotes) ...
    (comp.lang.perl.misc)
  • Re: Help deleting fields in SQL table
    ... I've been playing about some more - it looks like the SQL string is ... too long - if I do it in chunks it works. ... Also, some of my syntax ... wasn't quite right - I've changed it to allow for no single quotes ...
    (microsoft.public.excel.programming)
  • Re: syntax error in Total of field - I need help ASAP, PLease
    ... "check the subquery's syntax and enclose the subquery in parentheses". ... IF you are trying to use a subquery in the select clause of a query then ...
    (microsoft.public.access.queries)