| Author |
Topic  |
|
|
learning_grsql
Posting Yak Master
155 Posts |
Posted - 11/27/2012 : 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
Flowing Fount of Yak Knowledge
USA
2868 Posts |
Posted - 11/27/2012 : 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 column3
jim
Everyday I learn something that somebody else already knew |
 |
|
|
sodeep
Flowing Fount of Yak Knowledge
USA
7173 Posts |
Posted - 11/27/2012 : 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
155 Posts |
Posted - 11/28/2012 : 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)
Singapore
16746 Posts |
Posted - 11/28/2012 : 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 Time is always against us
|
 |
|
|
bandi
Flowing Fount of Yak Knowledge
India
1451 Posts |
Posted - 11/28/2012 : 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 Column3
1,2,3,4 abc,def,cay ab1,bc2,3d4
The 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
155 Posts |
Posted - 11/30/2012 : 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
Flowing Fount of Yak Knowledge
USA
7173 Posts |
Posted - 11/30/2012 : 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
Flowing Fount of Yak Knowledge
India
1451 Posts |
Posted - 12/03/2012 : 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 |
 |
|
| |
Topic  |
|
|
|