28

I want to insert the value into a bit type column in MySQL. But I am getting a data truncation error.

CREATE TABLE `BITTESTTABLE` (
  `db_field` varchar(50) NOT NULL,
   `is_editable` bit(1) NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1

If I am inserting a row with

INSERT INTO BITTESTTABLE values('XYZ','0')

I am getting

Data too long for column 'is_editable' at row 1

So how do I insert the data for bit type column?

7
  • 3
    change to INSERT INTO BITTESTTABLE values('XYZ',0); inserting the number 0 not the character.. Commented Jan 1, 2016 at 10:24
  • Integer type data it takes perfectly even though I use quotes for the data like '123' but why bits type is not taking is there any restriction in MySQL? Commented Jan 1, 2016 at 10:55
  • if you will put like '123' it means it will take like string and Integer is support to null value and null value is also string Commented Jan 1, 2016 at 11:01
  • 1
    @SrinivasDJ, if you will insert integer value '123' sometime it works because of sql tools or query but if you will insert ineteger value like 'abc' the it will throw error.......sql workbench, yog tools supports integer '123' cause of conversation of tools Commented Jan 1, 2016 at 11:42
  • 1
    I recommend using a TINYINT instead of a BIT. Your DBAL should quote and escape every value, so you'll have single quotes, which will work fine for a TINYINT, but not for a BIT. Commented Jan 1, 2016 at 11:47

6 Answers 6

38

You should use:

INSERT INTO `BITTESTTABLE` VALUES('XYZ', b'0');
Sign up to request clarification or add additional context in comments.

1 Comment

Worked for entering directly into the MySQL Workbench grid.
9

Since bit is a number not a string you need enter it like

INSERT INTO BITTESTTABLE values('XYZ',0)

2 Comments

Suppose if I have interger data type if I insert column value as '123' mysql accepts. it won't give an error.
Oh thanks for telling me this. Once I was adding number like this '1' in Int column it gives me error!
6

You need to insert data in bit format like this INSERT INTO BITTESTTABLE values('XYZ',0);.you are doing values('XYZ','0') so it is taking as string value.

Comments

5

You can use true/false directly; BUT I am not sure if this is related to MySQL itself or to InnoDB : EXAMPLE:

INSERT INTO BITTESTTABLE values('XYZ',false);    
INSERT INTO BITTESTTABLE values('XYZ',true);

To be sure try to run:

SELECT TRUE , FALSE  ;

result

Comments

4

Generally speaking, for boolean or bit data types, you would use 0 or 1 like that:

INSERT INTO BITTESTTABLE values('XYZ',0);

Comments

0

I had the same problem inserting data in prostgres db and I resolved.

Do like this INSERT INTO table (bitData) VALUES (CAST(1 AS bit));

rather then INSERT INTO table (bitData) VALUES (1);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.