SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help with CASE in SELECT statment
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Eagle_f90
Constraint Violating Yak Guru

USA
384 Posts

Posted - 06/13/2012 :  22:31:58  Show Profile  Visit Eagle_f90's Homepage  Reply with Quote
Can anyone look over the below code and help me figure out why it I am told there is incorect syntac in my frist THEN statment of my CASE stement?

SELECT si.SectionID AS ID,
       si.ShortName AS Title,
       si.Synopsis AS Descript,
       CAST(
            CASE
                WHEN nc.CategoryName = 'Cinema' 
                    THEN CAST('Section/EditCinema' AS char(18)) AS PageName
                WHEN nc.CategoryName = 'Book'
                    THEN CAST('Section/EditBook' AS char(16)) AS PageName
                WHEN nc.CategoryName = 'Merchandise'
                    THEN CAST('Section/EditMerch' AS char(17)) AS PageName
                ELSE CAST('Section/EditGame' AS char(16)) AS PageName
            )
FROM dbo.SectionInfo AS si WITH (NOLOCK)
JOIN dbo.NavigationCategories AS nc WITH (NOLOCK)
ON si.SectionID = nc.SectionID


Error:

quote:
Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'AS'.


--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

khtan
In (Som, Ni, Yak)

Singapore
16769 Posts

Posted - 06/13/2012 :  22:34:36  Show Profile  Reply with Quote

SELECT si.SectionID AS ID,
       si.ShortName AS Title,
       si.Synopsis AS Descript,
       CAST(
            CASE
                WHEN nc.CategoryName = 'Cinema' 
                    THEN CAST('Section/EditCinema' AS char(18)) AS PageName
                WHEN nc.CategoryName = 'Book'
                    THEN CAST('Section/EditBook' AS char(16)) AS PageName
                WHEN nc.CategoryName = 'Merchandise'
                    THEN CAST('Section/EditMerch' AS char(17)) AS PageName
                ELSE CAST('Section/EditGame' AS char(16)) AS PageName
                END
            ) AS PageName
FROM dbo.SectionInfo AS si WITH (NOLOCK)
JOIN dbo.NavigationCategories AS nc WITH (NOLOCK)
ON si.SectionID = nc.SectionID



KH
Time is always against us


Edited by - khtan on 06/13/2012 22:35:43
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16769 Posts

Posted - 06/13/2012 :  22:38:56  Show Profile  Reply with Quote
actually you don't need to cast the string to char(xx) at all

SELECT si.SectionID AS ID,
       si.ShortName AS Title,
       si.Synopsis AS Descript,
            CASE
                WHEN nc.CategoryName = 'Cinema'      THEN 'Section/EditCinema'
                WHEN nc.CategoryName = 'Book'        THEN 'Section/EditBook'
                WHEN nc.CategoryName = 'Merchandise' THEN 'Section/EditMerch'
                ELSE                                      'Section/EditGame'
                END AS PageName
FROM dbo.SectionInfo AS si WITH (NOLOCK)
JOIN dbo.NavigationCategories AS nc WITH (NOLOCK)
ON si.SectionID = nc.SectionID



KH
Time is always against us

Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

USA
384 Posts

Posted - 06/13/2012 :  22:50:07  Show Profile  Visit Eagle_f90's Homepage  Reply with Quote
Thanks

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000