ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

SQL - ignore [SQL0404] Value for column or variable x too long ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • SQL - ignore [SQL0404] Value for column or variable x too long ?

    If you have a table that is char(10), and you try and insert a value that is 11 or more characters long, you get error SQLState 22001, code -404, [SQL0404] Value for column or variable x too long

    Is there a way to make the SQL ignore this error and silently truncate instead?

    I know that cast() will silently truncate, and I could also test and convert with case, trimr, length and substr:
    Code:
    --myCol is char(10), longValue is char(11)
    
    insert into myTable (myCol)
    values(cast(longValue as char(10)));
    
    insert into myTable (myCol)
    values(case when length(trimr(longValue)) > 10 then substr(longValue,1,10) else longValue end);
    But scalar transforms on every applicable column on every row will incur performance overhead that I would prefer to avoid, and it means the column lengths are hard-coded into the program.
    So I was hoping there was a way of instructing SQL to do this automatically

    The reasoning for this is I want to store some data returned from the joblog_info() table function. But I have seen that IBM increased the lengths of one of the returned columns in 7.3 compared to the 7.2 version, and I want to protect against the program breaking if they do it again in the future. I'd much rather it truncated instead.

  • #2
    I just comment to announce that I’ve got the same question.

    Markus

    Comment


    • #3
      I never got an answer, I ended up casting.

      Comment


      • #4
        Thanks for answering my late reply. :-)

        I tried to make it less hardcoded, but my result wasn’t really satisfying:

        Code:
        [FONT=Courier New][COLOR=#ff0000]insert [/COLOR]
        [COLOR=#0000ff]into[/COLOR][COLOR=#000000] mytable[/COLOR][COLOR=#000080]([/COLOR][COLOR=#000000]mycol[/COLOR][COLOR=#000080])[/COLOR]
        [COLOR=#0000ff]values[/COLOR][COLOR=#000080]([/COLOR] [COLOR=#000000]substr[/COLOR][COLOR=#000080]([/COLOR][COLOR=#b03060] longValue[/COLOR][COLOR=#000000],
        [/COLOR]1[COLOR=#000000],
        [/COLOR][COLOR=#000080]( [/COLOR][COLOR=#0000ff]select[/COLOR][COLOR=#000000] length [/COLOR]
        [COLOR=#0000ff]                  from[/COLOR][COLOR=#000000] qsys2[/COLOR][COLOR=#b03060].[/COLOR][COLOR=#000000]syscolumns[/COLOR][COLOR=#0000ff]
                          where [/COLOR][COLOR=#000080]([/COLOR][COLOR=#000000]column_name[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] table_name[/COLOR][COLOR=#000000], [/COLOR][COLOR=#000000]table_schema[/COLOR][COLOR=#000080])[/COLOR][COLOR=#000000][B] = [/B][/COLOR][COLOR=#000080]([/COLOR][COLOR=#b03060]mycol[/COLOR][COLOR=#000000], [/COLOR][COLOR=#b03060]mytable[/COLOR][COLOR=#000000],[/COLOR][COLOR=#b03060] mylib[/COLOR][COLOR=#000080])
        [/COLOR][COLOR=#000080])
        [/COLOR][COLOR=#000080])[/COLOR]
        [COLOR=#000080])[/COLOR][COLOR=#000080];[/COLOR][/FONT]
        My SQL is embedded in RPG, so in the end I declared host variables with the length of the target fields and inserted them - all truncation now is done in/by RPG.

        Best regards,
        Markus

        Comment

        Working...
        X