4

In the code below I am inserting values into a table and getting the error "String or binary data would be truncated."

My table definition:

CREATE TABLE urs_prem_feed_out_control
( 
bd_pr_cntl_rec_type  char(7)  NULL ,
pd_pr_cntl_acctg_dte char(6)  NULL ,
bd_pr_cntl_run_dte   char(10)  NULL ,
bd_pr_cntl_start_dte char(10)  NULL ,
bd_pr_cntl_end_dte   char(10)  NULL ,
bd_pr_cntl_rec_count char(16)  NULL ,
bd_pr_tot_premium    char(16)  NULL ,
bd_pr_tot_commission char(16)  NULL ,
fd_ctl_nbr           integer  NOT NULL 
)

DECLARE @cur_fd_ctl_nbr INT = 2, 
@acctg_cyc_ym_2 CHAR(6) = '201402',
@rundate CHAR (10) = CONVERT(CHAR(10),GETDATE(),101),
@cycle_start_dt DATETIME = '2014-02-17',
@cycle_end_dt DATETIME = '2014-02-24',
@record_count INT = 24704,
@tot_pr_premium DECIMAL(18,2) = 476922242,
@tot_pr_comm DECIMAL(18,2) = 2624209257

Insert code (I've declared the variables as constant values for testing, I took these values from what they were at runtime):

INSERT INTO urs_prem_feed_out_control
SELECT fd_ctl_nbr = @cur_fd_ctl_nbr,
       bd_pr_cntl_rec_type      = 'CONTROL',
       bd_pr_cntl_acctg_dte     = @acctg_cyc_ym_2,
       bd_pr_cntl_run_dte       = @rundate,
       bd_pr_cntl_start_dte     = CONVERT(CHAR(10),@cycle_start_dt,101),    
       bd_pr_cntl_end_dte       = CONVERT(CHAR(10),@cycle_end_dt,101),   
       bd_pr_cntl_rec_count     = RIGHT('0000000000000000' +     RTRIM(CONVERT(CHAR(16),@record_count)),16),                                         
       bd_pr_tot_premium        = CASE       
                                     WHEN @tot_pr_premium < 0
                                        THEN '-' + SUBSTRING(RIGHT('000000000000000' + LTRIM(RTRIM(CONVERT(VARCHAR,ABS(@tot_pr_premium)*100))),18),1,15)
                                     ELSE
                                        '+' + SUBSTRING(RIGHT('000000000000000' + LTRIM(RTRIM(CONVERT(VARCHAR,ABS(@tot_pr_premium)*100))),18),1,15)
                                     END,                                        
        bd_pr_tot_commission    = CASE       
                                     WHEN @tot_pr_comm < 0
                                        THEN '-' + SUBSTRING(RIGHT('000000000000000' + LTRIM(RTRIM(CONVERT(VARCHAR,ABS(@tot_pr_comm)*100))),18),1,15)
                                     ELSE
                                        '+' + SUBSTRING(RIGHT('000000000000000' + LTRIM(RTRIM(CONVERT(VARCHAR,ABS(@tot_pr_comm)*100))),18),1,15)
                                     END

When I look at each value individually it seems like they are all within the variable length constraints of the table. Any idea why I'm getting this error?

Thanks!

7
  • 3
    Well, at least one of them is too long - otherwise you wouldn't be getting this error. Try to replace one variable at a time with e.g. a NULL value or a single character (e.g. *) - when the error goes away, you have your offending column value!
    – marc_s
    Jun 23, 2014 at 18:10
  • 1
    Write a select with all len().
    – paparazzo
    Jun 23, 2014 at 18:15
  • Rewrote the INSERT statement to a SELECT with all len()'s. Got 7,6,10,10,10,16,16,16 which corresponds exactly to the column lengths. When I go back to the INSERT I get the "String or binary data would be truncated" error!!!
    – intA
    Jun 23, 2014 at 18:22
  • 2
    Why are you mixing style 101 (mm/dd/yyyy) and style 120 (yyyy-mm-dd) instead of using an absolutely unambiguous format like yyyymmdd? Why are you converting to varchar without length? And why aren't you listing the column names in your insert statement? Notice the column that is last in your table but first in your select list. Jun 23, 2014 at 18:33
  • 2
    @SatwikNadkarny Yes, I know, but when I see all those other wrong things I have to mention them, too. Jun 23, 2014 at 18:47

3 Answers 3

4

The problem with your insert query is THE ORDER OF INSERTION :

SELECT fd_ctl_nbr = @cur_fd_ctl_nbr,

This column must be defined at the last in the INSERT as its the last column defined in the create table script.

Change your query to this:

   INSERT INTO #urs_prem_feed_out_control (fd_ctl_nbr, bd_pr_cntl_rec_type, pd_pr_cntl_acctg_dte, bd_pr_cntl_run_dte, bd_pr_cntl_start_dte, bd_pr_cntl_end_dte, bd_pr_cntl_rec_count, bd_pr_tot_premium, bd_pr_tot_commission)
SELECT fd_ctl_nbr = @cur_fd_ctl_nbr,
       bd_pr_cntl_rec_type      = 'CONTROL',
       bd_pr_cntl_acctg_dte     = @acctg_cyc_ym_2,
       bd_pr_cntl_run_dte       = @rundate,
       bd_pr_cntl_start_dte     = CONVERT(CHAR(10),@cycle_start_dt,101),    
       bd_pr_cntl_end_dte       = CONVERT(CHAR(10),@cycle_end_dt,101),   
       bd_pr_cntl_rec_count     = RIGHT('0000000000000000' +     RTRIM(CONVERT(CHAR(16),@record_count)),16),                                         
       bd_pr_tot_premium        = CASE       
                                     WHEN @tot_pr_premium < 0
                                        THEN '-' + SUBSTRING(RIGHT('000000000000000' + LTRIM(RTRIM(CONVERT(VARCHAR,ABS(@tot_pr_premium)*100))),18),1,15)
                                     ELSE
                                        '+' + SUBSTRING(RIGHT('000000000000000' + LTRIM(RTRIM(CONVERT(VARCHAR,ABS(@tot_pr_premium)*100))),18),1,15)
                                     END,                                        
        bd_pr_tot_commission    = CASE       
                                     WHEN @tot_pr_comm < 0
                                        THEN '-' + SUBSTRING(RIGHT('000000000000000' + LTRIM(RTRIM(CONVERT(VARCHAR,ABS(@tot_pr_comm)*100))),18),1,15)
                                     ELSE
                                        '+' + SUBSTRING(RIGHT('000000000000000' + LTRIM(RTRIM(CONVERT(VARCHAR,ABS(@tot_pr_comm)*100))),18),1,15)
                                     END

Doing this would also work. Notice that the first column here in the SELECT within the INSERT is just the way you have provided in your question.

See this here-> http://sqlfiddle.com/#!3/0e09b/1

Hope this helps!!!

4
  • @HLGEM See the second part of the answer where the column names are specified. There's a fiddle for that too (See the fiddle near the ending of the answer). Jun 23, 2014 at 18:57
  • @HLGEM There was no need to edit out the answer. Both the ways DO work. It's only a question of 'best practice'. However, personally, I do prefer & recommend specifying column names. Jun 23, 2014 at 19:01
  • It is not a question of best practice, it is a question of promoting poor practices especially since the poor practice (which was causing his problem) came first. I considered downvoting you instead but elected to edit since you do have the right solution. But the OP needed to see that the second solution is what he should use not the first one or this will break the next time he puts columns out of order.
    – HLGEM
    Jun 23, 2014 at 19:04
  • @HLGEM The reason you see the 'right solution' is because that's what I personally use and am comfortable with. I actually had to edit that out on my workstation to arrive at the one with no column names. But yes, I agree, I should've mentioned that in my answer as this is not the 'right way' of going forward. And on another note, there are plenty of things that are wrong with the way the query is framed (Aaron Bertrand sheds some light on this in his comments). Jun 23, 2014 at 19:13
2

This is why you should never write an insert statement without specifying the columns. Since you did not, it wil try to put the data in the columns in the order they are in the table which is not at all the order you have them in.

Another thing that can happen when you get this sort of message (but which I don't think applies in your case, I include it for people searching later) is that the eeror is actually coming from a trigger and not the main insert.

Finally a note on database design, you should not be using char for dates, you should be using date fields. You cannot do date math on a char field and it will accept incorrect date values like feb30, 2014. It is always a bad idea to store dates as anything except date or datetime values. In general char should only be used rarely when a column will always have the same number of characters (like a 2 column state abbreviation), it should not be used as the default datatype. You need to do a better job of defining datatypes that match the type and size of data being stored. You can run into problems with queries as 'VA' is not the same thing as 'VA '. In general my experience is that less than 1 % of all database fields should be Char.

1

I think it may be beneficial to double check your work. Consider the following example code:

DECLARE @Table TABLE (Name NVARCHAR(1))

INSERT INTO @Table (Name) SELECT 'ab'
INSERT INTO @Table (Name) SELECT SUBSTRING('ab',1,2)
INSERT INTO @Table (Name) SELECT RIGHT('abc',2)

All yield the following: String or binary data would be truncated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.