CASE STATEMENT

  • I'm having an issue with this case statement:

    CASE WHEN CLPCR.LicenseExpirationDate IS NOT NULL THEN CAST(CLPCR.LicenseExpirationDate AS DATE)

    WHEN CLPCR.LicenseExpirationDate IS NULL THEN 'Perpetual' END AS 'Expiration'

    When executing, i get this error:

    Msg 241, Level 16, State 1, Line 1

    Conversion failed when converting date and/or time from character string.

    I understand that it is looking for a date not a word, but am not sure how to make it work. Please let me know your thoughts. thanks.

  • You are trying to cast some values as date and others as character data in the same result column. You can't do that. I don't know what working means. What do you want it do do? Please provide some DDL and sample data and results. Look at Jeff Moden's article in my signature for the proper way to post.

    Jared
    CE - Microsoft

  • Jeremy... (4/16/2012)


    I'm having an issue with this case statement:

    CASE WHEN CLPCR.LicenseExpirationDate IS NOT NULL THEN CAST(CLPCR.LicenseExpirationDate AS DATE)

    WHEN CLPCR.LicenseExpirationDate IS NULL THEN 'Perpetual' END AS 'Expiration'

    When executing, i get this error:

    Msg 241, Level 16, State 1, Line 1

    Conversion failed when converting date and/or time from character string.

    I understand that it is looking for a date not a word, but am not sure how to make it work. Please let me know your thoughts. thanks.

    You need to cast CLPCR.LicenseExpirationDate to a character string, not a date since you want this column to contain a string value if the value of this column is null.

  • If you want to keep these results as dates, try replacing 'Perpetual' with '1900-01-01' OR '9999-12-31'

    Jared
    CE - Microsoft

  • Thanks for the help so far. I am essentially trying to show the expiration date for an item, however, when the value is null, this means the license has no expiration, and therefore is a "perpetual license". So the data would look something like this (with more variation):

    2012-07-31

    2012-07-31

    2012-07-31

    Perpetual

    2012-07-31

    2012-07-31

    Perpetual

    I am not locked in on how I display the date, so if the date can be converted to something else, that may work to. thanks.

  • you can also try an else instead of another "When" clause, with a proper cast

  • Jeremy... (4/16/2012)


    Thanks for the help so far. I am essentially trying to show the expiration date for an item, however, when the value is null, this means the license has no expiration, and therefore is a "perpetual license". So the data would look something like this (with more variation):

    2012-07-31

    2012-07-31

    2012-07-31

    Perpetual

    2012-07-31

    2012-07-31

    Perpetual

    I am not locked in on how I display the date, so if the date can be converted to something else, that may work to. thanks.

    Is this for a report? If so, then do this:

    CASE WHEN CLPCR.LicenseExpirationDate IS NOT NULL THEN CAST(CLPCR.LicenseExpirationDate AS VARCHAR(10))

    ELSE 'Perpetual' END AS 'Expiration'

    If this is for an application, don't change it to VARCHAR(10)... Make an identifier such as 1900-01-01 or 1753-01-01 or 9999-12-31 or 2079-06-06 (depending on your datatype).

    EDIT: If doing this in reporting services, don't even use a case. Build your expression in the report builder.

    Jared
    CE - Microsoft

  • sqlbi.vvamsi (4/16/2012)


    you can also try an else instead of another "When" clause, with a proper cast

    And still get an error...

    Jared
    CE - Microsoft

  • SQLKnowItAll (4/16/2012)


    Jeremy... (4/16/2012)


    Thanks for the help so far. I am essentially trying to show the expiration date for an item, however, when the value is null, this means the license has no expiration, and therefore is a "perpetual license". So the data would look something like this (with more variation):

    2012-07-31

    2012-07-31

    2012-07-31

    Perpetual

    2012-07-31

    2012-07-31

    Perpetual

    I am not locked in on how I display the date, so if the date can be converted to something else, that may work to. thanks.

    Is this for a report? If so, then do this:

    CASE WHEN CLPCR.LicenseExpirationDate IS NOT NULL THEN CAST(CLPCR.LicenseExpirationDate AS VARCHAR(10))

    ELSE 'Perpetual' END AS 'Expiration'

    If this is for an application, don't change it to VARCHAR(10)... Make an identifier such as 1900-01-01 or 1753-01-01 or 9999-12-31 or 2079-06-06 (depending on your datatype).

    EDIT: If doing this in reporting services, don't even use a case. Build your expression in the report builder.

    That did it. Thank you!

Viewing 9 posts - 1 through 8 (of 8 total)

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