how does datatypes get set when using select into in sql server?

  • I used a select...into to create a staging table. Its been fine but lastnight I got a truncation error. I was under the "Assumption" that sql server looked at the source table and retrieved the datatype from the column there.

  • dndaughtery (12/30/2015)


    I used a select...into to create a staging table. Its been fine but lastnight I got a truncation error. I was under the "Assumption" that sql server looked at the source table and retrieved the datatype from the column there.

    It does, generally. Are you doing some processing on the source column, or is it a naked SELECT?

    You can always force things by doing a suitable CAST.

    Please provide some sample code which highlights the behaviour.

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • There aren't any mathematical calculations but there are several Case statements. I'd post the query but its a monster

  • dndaughtery (12/30/2015)


    There aren't any mathematical calculations but there are several Case statements. I'd post the query but its a monster

    If a CASE statement is returning an item from a list of items with different types, the data type of the returned item is dictated by data type precedence.

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • dndaughtery (12/30/2015)


    There aren't any mathematical calculations but there are several Case statements. I'd post the query but its a monster

    CASE [font="Arial Black"]IS [/font]a type of calculation. You may have to CAST the CASE to the maximum expected length of any result.

    --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)

  • dndaughtery (12/30/2015)


    I used a select...into to create a staging table. Its been fine but lastnight I got a truncation error. I was under the "Assumption" that sql server looked at the source table and retrieved the datatype from the column there.

    For your CASE statement, SQL does not know what the resulting datatype should be.

    As Phil suggested, use the CAST statement for any column in your results where you have done anything other than a pure SELECT. Use CAST to specify the desired data type for your CASE results.

    Edit: oops, I see other were faster at posting their response.



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

Viewing 6 posts - 1 through 5 (of 5 total)

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