Author |
Topic |
learning_grsql
Posting Yak Master
230 Posts |
Posted - 2012-11-27 : 09:10:58
|
Hi,I want to insert values with comma and I tried as below but I get error "String or binary data would be truncated.The statement has been terminated."insert into table1 (column1, column2, column3)values ('1,2,3,4', 'abc,def,cay','ab1,bc2,3d4') |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2012-11-27 : 09:18:50
|
insert into table1 (column1, column2, column3)values ('1,2,3,4'), ('abc,def,cay'),('ab1,bc2,3d4')This will insert '1,2,3,4' into column1,'abc,def,cay' into column2, and 'ab1,bc2,3d4' into column3jimEveryday I learn something that somebody else already knew |
 |
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2012-11-27 : 10:24:08
|
This is actually bad design to enter comma separated values in DB. Have you thought about normalization? |
 |
|
learning_grsql
Posting Yak Master
230 Posts |
Posted - 2012-11-28 : 00:14:00
|
Thanks jimf and sodeep.@jimf. I'm using sql server 2005 and I get error for your code - "There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement."@sodeep..in this case, i'm just following database designed by somebody else(my boss). |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-11-28 : 01:54:53
|
quote: Originally posted by learning_grsql Hi,I want to insert values with comma and I tried as below but I get error "String or binary data would be truncated.The statement has been terminated."insert into table1 (column1, column2, column3)values ('1,2,3,4', 'abc,def,cay','ab1,bc2,3d4')
The value are for 1 record or 3 records ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-11-28 : 02:33:41
|
quote: Originally posted by learning_grsql Hi,I want to insert values with comma and I tried as below but I get error "String or binary data would be truncated.The statement has been terminated."insert into table1 (column1, column2, column3)values ('1,2,3,4', 'abc,def,cay','ab1,bc2,3d4')
If this is your requirement,Column1 Column2 Column31,2,3,4 abc,def,cay ab1,bc2,3d4The reason for the error (String or binary data would be truncated) is: Sizes of these columns column1, column2, column3 may be lesser than ur data --Chandu |
 |
|
learning_grsql
Posting Yak Master
230 Posts |
Posted - 2012-11-30 : 10:18:09
|
@bandi, you are correct. I just increased the column sizes and made the change mentioned below, it worked. Thanks for pointing that out.I just added one more bracket to make it work as below :insert into table1 (column1, column2, column3)values (('1,2,3,4', 'abc,def,cay','ab1,bc2,3d4')) Thanks to all. |
 |
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2012-11-30 : 10:23:59
|
quote: Originally posted by learning_grsql Thanks jimf and sodeep.@jimf. I'm using sql server 2005 and I get error for your code - "There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement."@sodeep..in this case, i'm just following database designed by somebody else(my boss).
So that means you are parsing everytime to get the value ??? |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-12-03 : 05:48:50
|
quote: Originally posted by learning_grsql @bandi, you are correct. I just increased the column sizes and made the change mentioned below, it worked. Thanks for pointing that out.I just added one more bracket to make it work as below :insert into table1 (column1, column2, column3)values (('1,2,3,4', 'abc,def,cay','ab1,bc2,3d4')) Thanks to all.
welcome --Chandu |
 |
|
|
|
|