Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 insert values with comma into table

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 column3

jim

Everyday I learn something that somebody else already knew
Go to Top of Page

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?
Go to Top of Page

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).
Go to Top of Page

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]

Go to Top of Page

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 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
Go to Top of Page

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.
Go to Top of Page

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 ???
Go to Top of Page

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
Go to Top of Page
   

- Advertisement -