skip to Main Content

I have this table:

create table TableName5 (     col1 varchar(200),     col2 varchar(200),     col3 varchar(200) generated
always as ('col1' + 'col2') stored );

The col3 is supposed to be a computed column – the value in col1 plus the value in col2.

The create query works OK.

However trying to insert values:

insert into TableName5 (col1, col2) values ('a', 'b');

Results in

ERROR 1292 (22007): Truncated incorrect DOUBLE value: 'col1'

error.

What am I doing wrong?

EDIT:

I tried using a different create query:

create table TableName5 (     col1 varchar(200),     col2 varchar(200),     col3 varchar(200) generated
always as concat(col1, ' ', col2) stored );

but got a syntax error on it

2

Answers


  1. in your original you had stings cl1 and clo2 so the result was col1col2

    as i satd you can’t sum varchar or text you need CONCAT

    create table TableName5 (     col1 varchar(200),     col2 varchar(200),     col3 varchar(200) generated
    always as (CONCAT(`col1` , `col2`)) stored );
    
    insert into TableName5 (col1, col2) values ('a', 'b');
    
    
    SELECT * FROM TableName5
    
    col1 col2 col3
    a b ab

    fiddle

    Login or Signup to reply.
  2. create table TableName5 (
        col1 varchar(200),
        col2 varchar(200),
        col3 varchar(200) generated always as (concat(col1, ' ', col2)) stored
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search