SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 insert values with comma into table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

learning_grsql
Posting Yak Master

230 Posts

Posted - 11/27/2012 :  09:10:58  Show Profile  Reply with Quote
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
2869 Posts

Posted - 11/27/2012 :  09:18:50  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 11/27/2012 :  10:24:08  Show Profile  Reply with Quote
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 - 11/28/2012 :  00:14:00  Show Profile  Reply with Quote
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)

Singapore
17642 Posts

Posted - 11/28/2012 :  01:54:53  Show Profile  Reply with Quote
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

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 11/28/2012 :  02:33:41  Show Profile  Reply with Quote
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 - 11/30/2012 :  10:18:09  Show Profile  Reply with Quote
@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
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 11/30/2012 :  10:23:59  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 12/03/2012 :  05:48:50  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000