sum case issues with commas and dashes

  • ,SUM (CASE WHEN Q2.CSISeqNo = 185 AND A.Description = 'Loan Cars - C&D's Not Suitable' AND Q.Answer = 6

    THEN 1 ELSE 0 END) AS [Parts ordering process]

    Guys, how do I get around the above, i'm assuming I am getting an error message due to the  - and ' within the 'Loan Cars - C&D'

    s Not Suitable'

  • craig.jenkins - Thursday, November 16, 2017 9:46 AM

    ,SUM (CASE WHEN Q2.CSISeqNo = 185 AND A.Description = 'Loan Cars - C&D's Not Suitable' AND Q.Answer = 6

    THEN 1 ELSE 0 END) AS [Parts ordering process]

    Guys, how do I get around the above, i'm assuming I am getting an error message due to the  - and ' within the 'Loan Cars - C&D'

    s Not Suitable'

    Just escape the quote after the D by doubling it:
    CASE
        WHEN Q2.CSISeqNo = 185 AND A.Description = 'Loan Cars - C&D''s Not Suitable' AND Q.Answer = 6 THEN 1
        ELSE 0
    END

    John

  • It's standard practice in many programming languages that if you need to include the string delimiter inside a string to use a double delimiter.  So, you just need to double the single quote in your 'C&D''s'

    ,SUM (CASE WHEN Q2.CSISeqNo = 185 AND A.Description = 'Loan Cars - C&D''s Not Suitable' AND Q.Answer = 6

    THEN 1 ELSE 0 END) AS [Parts ordering process]

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • If you're building these queries dynamically, " QUOTENAME(Description, '''') " puts quotes around a string and doubles any embedded quotes.  Or " QUOTENAME(Description, CHAR(39)) " if you don't like the four-apostrophe literal.

    The downside is that it only accepts 128-character values.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply