Error inserting data with T-sql

  • Hello

    I am inserting data from one table to another using update,set command and getting the following error.

    update XDDDepositor

    SET XDDdepositor.WBeneName = Vendor.RemitName,

    XDDDepositor.WBeneAddr = Vendor.RemitAddr1,

    XDDDepositor.WBeneAddr2 = Vendor.RemitAddr2,

    XDDDepositor.WBeneCity = Vendor.RemitCity,

    XDDDepositor.WBeneState = Vendor.RemitState,

    XDDDepositor.WBeneZipPostal = Vendor.RemitZip

    From XDDDepositor, Vendor

    Where XDDDepositor.vendid like '05%'and

    XDDDepositor.vendid = Vendor.Vendid and

    XDDDepositor.WBeneName =''

    ERROR:

    Msg 8152, Level 16, State 14, Line 2

    String or binary data would be truncated.

    The statement has been terminated.

    I know the problem is the character length of the address columns I am inserting data from but is there a way to correct it without altering the columns being updated?

    WBenBankAddr (char(35),not null) and RemitAddr1 (char(60), not null)

    WBeneAddr2 (char(35),not null) and RemitAddr2 (char(60), not null)

  • Use LEFT function to limit number of characters to be taken from columns which causing this problem.

    P.S. Without having DDL of tables involved it's impossible to tell which exact columns causing this problem. I guess you know them yourself...

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Here is how you would do that for the two columns you say are the problem.

    update XDDDepositor

    SET XDDdepositor.WBeneName = Vendor.RemitName,

    XDDDepositor.WBeneAddr = left(Vendor.RemitAddr1, 35),

    XDDDepositor.WBeneAddr2 = left(Vendor.RemitAddr2, 35),

    XDDDepositor.WBeneCity = Vendor.RemitCity,

    XDDDepositor.WBeneState = Vendor.RemitState,

    XDDDepositor.WBeneZipPostal = Vendor.RemitZip

    From XDDDepositor, Vendor

    Where XDDDepositor.vendid like '05%'and

    XDDDepositor.vendid = Vendor.Vendid and

    XDDDepositor.WBeneName =''

    The potential issue that you are only copying some of the data. If you need to hold all the information you will have to make the destination columns wide enough to hold the data.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • LEFT, SUBSTRING, you need to limit the amount of data updated, as Eugene noted.

  • Thanks Sean that worked. I also found that substring worked too.

    update XDDDepositor

    SET XDDdepositor.WBeneName = Vendor.RemitName,

    XDDDepositor.WBeneAddr = SUBSTRING( Vendor.RemitAddr1,0, 35),

    XDDDepositor.WBeneAddr2 = SUBSTRING( Vendor.RemitAddr2, 0 , 35),

    XDDDepositor.WBeneCity = Vendor.RemitCity,

    XDDDepositor.WBeneState = Vendor.RemitState,

    XDDDepositor.WBeneZipPostal = Vendor.RemitZip

    From XDDDepositor, Vendor

    Where XDDDepositor.vendid like '05%'

    and XDDDepositor.vendid = Vendor.Vendid

    and XDDDepositor.WBeneName =''

  • Thanks

    Thanks Sean that worked. I also found that substring worked too.

    update XDDDepositor

    SET XDDdepositor.WBeneName = Vendor.RemitName,

    XDDDepositor.WBeneAddr = SUBSTRING( Vendor.RemitAddr1,0, 35),

    XDDDepositor.WBeneAddr2 = SUBSTRING( Vendor.RemitAddr2, 0 , 35),

    XDDDepositor.WBeneCity = Vendor.RemitCity,

    XDDDepositor.WBeneState = Vendor.RemitState,

    XDDDepositor.WBeneZipPostal = Vendor.RemitZip

    From XDDDepositor, Vendor

    Where XDDDepositor.vendid like '05%'

    and XDDDepositor.vendid = Vendor.Vendid

    and XDDDepositor.WBeneName =''

  • your last bracket is missing a / before the word code. /code

  • jdbrown239 (7/18/2013)


    Thanks

    Thanks Sean that worked. I also found that substring worked too.

    That is because in this case they are doing the same thing. 😉

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • jdbrown239 (7/18/2013)


    Thanks

    Thanks Sean that worked. I also found that substring worked too.

    update XDDDepositor

    SET XDDdepositor.WBeneName = Vendor.RemitName,

    XDDDepositor.WBeneAddr = SUBSTRING( Vendor.RemitAddr1,0, 35),

    XDDDepositor.WBeneAddr2 = SUBSTRING( Vendor.RemitAddr2, 0 , 35),

    XDDDepositor.WBeneCity = Vendor.RemitCity,

    XDDDepositor.WBeneState = Vendor.RemitState,

    XDDDepositor.WBeneZipPostal = Vendor.RemitZip

    From XDDDepositor, Vendor

    Where XDDDepositor.vendid like '05%'

    and XDDDepositor.vendid = Vendor.Vendid

    and XDDDepositor.WBeneName =''

    Rather than just truncating the address, you may want to use the REPLACE() function to substitute long words with common abbreviation or remove unneeded spaces.

    http://www.semaphorecorp.com/cgi/abbrev.html

    For example:

    XDDDepositor.WBeneAddr =

    SUBSTRING( REPLACE( REPLACE(

    Vendor.RemitAddr1

    , 'BOULEVARD', 'BLVD' )

    , 'EXPRESSWAY', 'EXPY' )

    , 0, 35 )

    "Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho

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

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