Looking for Function to Format Values for SQL INSERT Statement

Alex20850

Board Regular
Joined
Mar 9, 2010
Messages
146
Office Version
  1. 365
Platform
  1. Windows
I have values in Excel spreadsheets that I would like to format for creating INSERT statements to be run in SQL.

Based on whether a column heading value is C for Character or N for numeric, the value would either apostrophe before and after the value or no apostrophes if it is numeric.

[Sorry, don't know how to paste image.]

I am looking for function - CREATEINSERT for example - that would create the values inside the individual
values inside the parenthesis based on the values in row 6 which has C and N.
=CREATEINSERT($B6,B8) would produce '01001' while
=CREATEINSERT($E6,E8) would produce '55347'.
CCC N N
COUNTIESCOUNTYFIPSSTATEABBRCOUNTYNAME POP2015AREASQMI
INSERT INTO COUNTIES VALUES ('01001','AL','Autauga County',55347,594.436);01001ALAutauga County55347594.436
INSERT INTO COUNTIES VALUES ('01003','AL','Baldwin County',203709,1589.784);01003ALBaldwin County2037091589.784
INSERT INTO COUNTIES VALUES ('01005','AL','Barbour County',26489,884.876);01005ALBarbour County26489884.876
INSERT INTO COUNTIES VALUES ('01007','AL','Bibb County',22583,622.582);01007ALBibb County22583622.582
INSERT INTO COUNTIES VALUES ('01009','AL','Blount County',57673,644.776);01009ALBlount County57673644.776
INSERT INTO COUNTIES VALUES ('01011','AL','Bullock County',10696,622.805);01011ALBullock County 10,696622.805

<tbody>
</tbody>
 
Last edited:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
This UDF seems to do what you're looking for.

Code:
Function CREATEINSERT(r As Range, head As Range, table As String) As String
Dim ar() As Variant: ar = r.Value
Dim h() As Variant: h = head.Value
Dim res As String: res = "INSERT INTO " & table & " VALUES ("


If UBound(ar, 2) = UBound(h, 2) Then
    For i = LBound(ar) To UBound(ar, 2)
        If (h(1, i)) = "N" Then
            res = res & ar(1, i) & ","
        Else
            If i = 1 Then
                res = res & "'" & Format(ar(1, i), "00000") & "',"
            Else
                res = res & "'" & ar(1, i) & "',"
            End If
        End If
    Next i
End If


CREATEINSERT = Left(res, Len(res) - 1) & ");"
            
End Function


Cell Formulas
RangeFormula
A3=CREATEINSERT(B3:F3,$B$1:$F$1,"COUNTIES")
A4=CREATEINSERT(B4:F4,$B$1:$F$1,"COUNTIES")
A5=CREATEINSERT(B5:F5,$B$1:$F$1,"COUNTIES")
A6=CREATEINSERT(B6:F6,$B$1:$F$1,"COUNTIES")
A7=CREATEINSERT(B7:F7,$B$1:$F$1,"COUNTIES")
A8=CREATEINSERT(B8:F8,$B$1:$F$1,"COUNTIES")
 
Upvote 0
Refactored the code a bit to clean it up.

Code:
Function CREATEINSERT(r As Range, head As Range, table As String) As String
Dim ar() As Variant:    ar = r.Value
Dim h() As Variant:     h = head.Value
Dim res As String:      res = "INSERT INTO " & table & " VALUES ("
Dim val As Variant:     val = vbNullString
Dim cn As String:       cn = vbNullString


If UBound(ar, 2) = UBound(h, 2) Then
    For i = LBound(ar) To UBound(ar, 2)
        cn = h(1, i)
        val = ar(1, i)
        If i = 1 Then val = Format(val, "00000")
        If cn = "C" Then val = "'" & val & "'"
        res = res & val & ","
    Next i
End If


CREATEINSERT = Left(res, Len(res) - 1) & ");"
            
End Function
 
Upvote 0
Wow!! Works great. Thank you so much. Very useful and flexible.
Really appreciate.
 
Upvote 0
This works great, but I have found something that would be a nice tweak.
<code>INSERT INTO COUNTIES VALUES ('24035','MD','Queen Anne's County',48904,371.908); returns the error ORA-00917: missing comma
It needs the apostrophe to be doubled to avoid the error.
INSERT INTO COUNTIES VALUES ('24035','MD','Queen Anne''s County',48904,371.908);
Could the function fix that?


</code>
 
Upvote 0
Give this a shot.

Code:
Function CREATEINSERT(r As Range, head As Range, table As String) As String
Dim ar() As Variant:    ar = r.Value
Dim h() As Variant:     h = head.Value
Dim res As String:      res = "INSERT INTO " & table & " VALUES ("
Dim val As Variant:     val = vbNullString
Dim cn As String:       cn = vbNullString




If UBound(ar, 2) = UBound(h, 2) Then
    For i = LBound(ar) To UBound(ar, 2)
        cn = h(1, i)
[COLOR=#0000cd]        val = Replace(ar(1, i), "'", "''")[/COLOR]
        If i = 1 Then val = Format(val, "00000")
        If cn = "C" Then val = "'" & val & "'"
        res = res & val & ","
    Next i
End If




CREATEINSERT = Left(res, Len(res) - 1) & ");"
            
End Function
 
Upvote 0
Works great. Thank you very much. This has been very useful for me.
May I submit your work to SQL groups?
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,487
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top