Converting Integer to literal text value if NULL

  • Good Morning all and Happy Friday.

    I have this SQL that I need to convert an Integer to a literal string if the value is NULL, otherwise it needs to just be left alone. Here is the SQL that I'm playing with that of course isn't working. The "deleteflag" logic is fine, the "Intgertest" not so much.

    Thanks for your help

    William

    ----------------------- SLQ -------------------

    select

    userid,

    category,

    payperiod,

    case

    when

    deleteflag IS NULL then 'literal null'

    else

    deleteflag

    end as delflag,

    case when

    Integertest IS NULL then 'null integer'

    else

    Integertest

    end as IntTest

    from timeworked_temp

  • Its probably because the Integertest column is defined as an Integer, and when the column is NULL, the query is trying to return a string of 'null integer'.

    You may need to convert the Integertest to varchar or convert to 0 when it has a NULL value

     


    I feel the need - the need for speed

    CK Bhatia

  • Why not do it on the client application?  Would cost much cpu cycles on the server (no cast at al to do).

  • As CK suggested... but different...

     SELECT UserID,

            Category,

            PayPeriod,

            ISNULL(CAST(DeleteFlag AS VARCHAR(12)),'Literal Null') AS DelFlag

            ISNULL(CAST(IntegerTest AS VARCHAR(12)),'Null Integer') AS IntTest

       FROM TimeWorked_Temp

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Jeff, would such solution be acceptable if it would be FOR YOU?

    _____________
    Code for TallyGenerator

  • Only if I absolutely had to put up with the same cruddy requirement that William has obviously been blessed with.   It's simple data manipulation that need not be relegated to the rigors of the app.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Do you think requirements grow on trees?

    Who, on you opinion, made this requirement?

    _____________
    Code for TallyGenerator

  • I think that someone was barking up the wrong tree when they wrote the requirement ... but, since some folks suggested doing it in the app, I thought I'd provide the solution in SQL.  Not sure who came up with the requirement and I sometimes get tired of asking on the forum...

    Now, where I work is a totally different story... I'd hunt down the person that made such a requirement and shoot them butt first out of a cannon into a stone wall for being so stupid.

    William, if you're watching this, please don't take personal offense to this.... I'm thinking that you are NOT the one who wrote the requirement.  If you are, beware of stone walls

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

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

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