Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5
Default How to CONCATENATE 50 fields in Excel table into SQL Insert State

Excel 2003 SP3 on Windows XP SP2

I have problem when trying to construct an SQL Insert statement using the
CONCATENATE function in Excel. There are over 50 fields.

I would like to construct an SQL insert statement from tables stored in
Excel to be used to populate tables in a SQL database. Using the CONCATENATE
function below I ends up with a complete SQL insert statement

=CONCATENATE("INSERT INTO Product (Field1,Field2,Field3,Field4,Field5)
VALUES ('",B2,"','",C2,"',",D2,",",E2,",",F2,")")

When I try to do the same for a large table ( 50 fields). I have the
following error in Excel ("The formula you typed contains an error").
Reading through the forum Excel sees to have a limit of 30 strings for the
CONCATENATE function.

However, the concatenate function only allow me to enter 14 fields. There
seems to be some sort of limits in total characters within concatenate
function (when I shorten some of the field names i.e. to F1, F2, F3 I could
enter more fields)

Please could you advice how I could maybe do multiple CONCATENATE. For
example, concatenate the first 30 strings, then concatenate the result of
that with the remaining 20 strings, etc?

Many thanks in advance,

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,365
Default How to CONCATENATE 50 fields in Excel table into SQL Insert State

There are 2 ways to attack this. First is to concatenate a different way.
Instead of CONCATENATE() just use the ampersand ( & ) to merge the text, as:
="INSERT INTO Product(Field1,Field2) VALUES('" & B2 & "," & C2 & "')"
Much like CONCATENATE, but the & replaces the comma and you don't have the
CONCATENATE( at the beginning or the closing ) at the end.

You're no longer limited by the 30 strings although you are still limited by
formula length.

As for breaking the CONCATENATE() into several cells, you could simply start
working from left to right in the SQL statement you need to create. Lets say
you start building it up at column A, row 3:
in A3
="INSERT INTO Product "
in B3
=" (Field1, Field2, Field3, Field4, Field5)"
in C3
=" VALUES ('"
then in C4 you could have
=CONCATENATE(A3,B3,C3, ",B2,"','",C2,"',",D2,",",E2,",",F2,")")
or simply string them together as
=A3 & B3 & C3 & ",'" & B2 & "'," & "','" & C2 & "','" & "'," & D2 & "," &
"," & E2 & "," & F2 & ")"

That last might make seeing errors in pairing single quote marks up a little
easier also. I've not examined your example extremely closely, but it
appeared that you might have had doubld/single quote marks in wrong sequence
at times, which would give a formula error.

Hope this helps some

"will~" wrote:

Excel 2003 SP3 on Windows XP SP2

I have problem when trying to construct an SQL Insert statement using the
CONCATENATE function in Excel. There are over 50 fields.

I would like to construct an SQL insert statement from tables stored in
Excel to be used to populate tables in a SQL database. Using the CONCATENATE
function below I ends up with a complete SQL insert statement

=CONCATENATE("INSERT INTO Product (Field1,Field2,Field3,Field4,Field5)
VALUES ('",B2,"','",C2,"',",D2,",",E2,",",F2,")")

When I try to do the same for a large table ( 50 fields). I have the
following error in Excel ("The formula you typed contains an error").
Reading through the forum Excel sees to have a limit of 30 strings for the
CONCATENATE function.

However, the concatenate function only allow me to enter 14 fields. There
seems to be some sort of limits in total characters within concatenate
function (when I shorten some of the field names i.e. to F1, F2, F3 I could
enter more fields)

Please could you advice how I could maybe do multiple CONCATENATE. For
example, concatenate the first 30 strings, then concatenate the result of
that with the remaining 20 strings, etc?

Many thanks in advance,

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5
Default How to CONCATENATE 50 fields in Excel table into SQL Insert S

Many thanks for your quick reply. Your answer is very clear and informative.
Due to the number of fields, the formula limit would restrict me from using
the first method. (I have tried replacing the comma with & for a smaller
table and it works well).

I would give the second method a try, and string together different parts of
the SQL statement.

kind Regards,

William

"JLatham" wrote:

There are 2 ways to attack this. First is to concatenate a different way.
Instead of CONCATENATE() just use the ampersand ( & ) to merge the text, as:
="INSERT INTO Product(Field1,Field2) VALUES('" & B2 & "," & C2 & "')"
Much like CONCATENATE, but the & replaces the comma and you don't have the
CONCATENATE( at the beginning or the closing ) at the end.

You're no longer limited by the 30 strings although you are still limited by
formula length.

As for breaking the CONCATENATE() into several cells, you could simply start
working from left to right in the SQL statement you need to create. Lets say
you start building it up at column A, row 3:
in A3
="INSERT INTO Product "
in B3
=" (Field1, Field2, Field3, Field4, Field5)"
in C3
=" VALUES ('"
then in C4 you could have
=CONCATENATE(A3,B3,C3, ",B2,"','",C2,"',",D2,",",E2,",",F2,")")
or simply string them together as
=A3 & B3 & C3 & ",'" & B2 & "'," & "','" & C2 & "','" & "'," & D2 & "," &
"," & E2 & "," & F2 & ")"

That last might make seeing errors in pairing single quote marks up a little
easier also. I've not examined your example extremely closely, but it
appeared that you might have had doubld/single quote marks in wrong sequence
at times, which would give a formula error.

Hope this helps some

"will~" wrote:

Excel 2003 SP3 on Windows XP SP2

I have problem when trying to construct an SQL Insert statement using the
CONCATENATE function in Excel. There are over 50 fields.

I would like to construct an SQL insert statement from tables stored in
Excel to be used to populate tables in a SQL database. Using the CONCATENATE
function below I ends up with a complete SQL insert statement

=CONCATENATE("INSERT INTO Product (Field1,Field2,Field3,Field4,Field5)
VALUES ('",B2,"','",C2,"',",D2,",",E2,",",F2,")")

When I try to do the same for a large table ( 50 fields). I have the
following error in Excel ("The formula you typed contains an error").
Reading through the forum Excel sees to have a limit of 30 strings for the
CONCATENATE function.

However, the concatenate function only allow me to enter 14 fields. There
seems to be some sort of limits in total characters within concatenate
function (when I shorten some of the field names i.e. to F1, F2, F3 I could
enter more fields)

Please could you advice how I could maybe do multiple CONCATENATE. For
example, concatenate the first 30 strings, then concatenate the result of
that with the remaining 20 strings, etc?

Many thanks in advance,

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 22,906
Default How to CONCATENATE 50 fields in Excel table into SQL Insert State

This UDF will allow many contiguous cells to be concatenated, comma-delimited.

Function ConCatRange(CellBlock As Range) As String
Dim cell As Range
Dim sbuf As String
For Each cell In CellBlock
If Len(cell.text) 0 Then sbuf = sbuf & cell.text & ","
Next
ConCatRange = Left(sbuf, Len(sbuf) - 1)
End Function

=concatrange(A2:CA2)

Will skip blanks if any in range.


Gord Dibben MS Excel MVP

On Thu, 28 Feb 2008 04:02:01 -0800, will~
wrote:

Excel 2003 SP3 on Windows XP SP2

I have problem when trying to construct an SQL Insert statement using the
CONCATENATE function in Excel. There are over 50 fields.

I would like to construct an SQL insert statement from tables stored in
Excel to be used to populate tables in a SQL database. Using the CONCATENATE
function below I ends up with a complete SQL insert statement

=CONCATENATE("INSERT INTO Product (Field1,Field2,Field3,Field4,Field5)
VALUES ('",B2,"','",C2,"',",D2,",",E2,",",F2,")")

When I try to do the same for a large table ( 50 fields). I have the
following error in Excel ("The formula you typed contains an error").
Reading through the forum Excel sees to have a limit of 30 strings for the
CONCATENATE function.

However, the concatenate function only allow me to enter 14 fields. There
seems to be some sort of limits in total characters within concatenate
function (when I shorten some of the field names i.e. to F1, F2, F3 I could
enter more fields)

Please could you advice how I could maybe do multiple CONCATENATE. For
example, concatenate the first 30 strings, then concatenate the result of
that with the remaining 20 strings, etc?

Many thanks in advance,


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I show state-by-state data (as silos) on a map of NA Rob Charts and Charting in Excel 0 November 5th 07 03:41 PM
Cannot Concatenate fields using Query CJ Excel Discussion (Misc queries) 6 September 10th 07 04:32 AM
Converting State Names to State Abbreviations aznate Excel Discussion (Misc queries) 1 October 20th 06 06:52 AM
How do I insert new fields in Excel DB created using template wiza SHARIEEH Excel Discussion (Misc queries) 0 May 3rd 06 06:55 PM
number of fields in the row fields in pivot table UT Excel Discussion (Misc queries) 0 April 13th 06 01:17 AM


All times are GMT +1. The time now is 12:01 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"