Shortening a column

  • Comments posted to this topic are about the item Shortening a column

  • Nice, easy one to end the week on, thanks, Steve

  • This was removed by the editor as SPAM

  • Nice question Steve. Thanks for sharing

  • Thank you for a nice easy Friday question.

    ...

  • Easy - tell the 44% who have got it wrong so far.

    I take it as a well earned point!

  • There are circumstances in which option 4 is the correct answer. The question does not state that the table is empty, nor does it say anything about the data in the column to be altered. For example, if you execute this:

    INSERT INTO MySales

    VALUES (

    1

    ,1

    ,CURRENT_TIMESTAMP

    ,CURRENT_TIMESTAMP

    ,1

    ,10

    ,'0123456789'

    ,1

    ,'Test'

    )

    GO

    DROP INDEX IX_MySales_CustomerReport ON MySales

    ALTER TABLE MySales ALTER COLUMN OrderStatus varchar(5)

    CREATE INDEX IX_MySales_CustomerReport ON MySales (CustomerID, SaleDate) INCLUDE (ShipDate, SalesRepID, orderstatus)

    you get this error:

    Msg 8152, Level 16, State 14, Line 15

    String or binary data would be truncated.

    John

  • A nice and simple question to end the week. Thanks and have a great weekend.

  • John Mitchell-245523 (1/8/2016)


    There are circumstances in which option 4 is the correct answer. The question does not state that the table is empty, nor does it say anything about the data in the column to be altered. For example, if you execute this:

    INSERT INTO MySales

    VALUES (

    1

    ,1

    ,CURRENT_TIMESTAMP

    ,CURRENT_TIMESTAMP

    ,1

    ,10

    ,'0123456789'

    ,1

    ,'Test'

    )

    GO

    DROP INDEX IX_MySales_CustomerReport ON MySales

    ALTER TABLE MySales ALTER COLUMN OrderStatus varchar(5)

    CREATE INDEX IX_MySales_CustomerReport ON MySales (CustomerID, SaleDate) INCLUDE (ShipDate, SalesRepID, orderstatus)

    you get this error:

    Msg 8152, Level 16, State 14, Line 15

    String or binary data would be truncated.

    John

    In that case, answer four is still not correct. The correct answer would be

    Drop the index

    UPDATE MySales SET OrderStatus = LEFT(OrderStatus,5) -- or whatever truncation function you wish to use -- note: do the update after dropping the index so SQL Server doesn't have to update the index as well as the table.

    Alter the column

    Recreate the index

    which is not listed. You can change the maximum length of a varchar column without dropping and recreating the table. You will have to drop and re-create any indexes, and you will have to update any data which won't fit into the smaller size, but it can be done.

  • Nice question, thanks.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • paul s-306273 (1/8/2016)


    Easy - tell the 44% who have got it wrong so far.

    I take it as a well earned point!

    I consistently get answers wrong, like this one, I figured shortening the column would truncate data so I selected "can't do it", but I'm not going to be that bothered by being wrong, just as long as I'm not wrong in production 😀

  • sknox (1/8/2016)


    In that case, answer four is still not correct.

    It is if you don't want to lose any data. Yes, if you were doing this at all then perhaps you wouldn't mind losing the data. But where the information in a question isn't complete, you have to make assumptions. I made the wrong ones. I'm not beating myself up about it, though - it'll certainly help me remember this thing about indexes!

    John

  • Good question. And if you want to lengthen the column you can do it directly without dropping the index.

    Igor Micev,My blog: www.igormicev.com

  • Thanks for the question.

Viewing 14 posts - 1 through 13 (of 13 total)

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