VB.Net - Oracle BulkCopy from CSV Date Format

I am reading off a CSV file with some hours, but this is a string.  The "Labor" column have values such as followed:


When I do my test in SQL Developer, I can convert my string into date, and then get it to decimal.  However, when I put this in my VB.net application so the user do the BulkCopy, I am getting the following error:

"Undefined function 'TO_DATE' in expression."

Here's my VB.Net code.
Dim cmd As New OleDb.OleDbCommand("SELECT [Name], [ORDER], [JOB],  " _
	& "ROUND((TO_DATE(LPAD(NVL([Duration], '00:00'), 5, '0'), 'HH24:MI')-TRUNC(TO_DATE(LPAD(NVL([Duration], '00:00'), 5, '0'), 'HH24:MI'), 'DD'))*24, 2) FROM [" + csvFileName + "]", excelstrCon)
Dim reader As OleDb.OleDbDataReader = cmd.ExecuteReader

Open in new window

Can I not use these functions?  Or do I need to do SQL function instead?
Who is Participating?
Geert GConnect With a Mentor Oracle dbaCommented:
if you can't process it in 1 go, use a staging table
it's sometimes easier to upload a line of a csv, skipping the headers, into 1 column of a table
1 line in csv = 1 record in staging table

then use a procedure to process the staging table and move the data to the final table

if have problems in converting, with a record, log it in another separate table
afterwards you can fine tune the process

if no errors occur, fine
if someone invents a new format in the csv, you can evaluate it and take actions as needed
slightwv (䄆 Netminder) Commented:
TO_DATE is an Oracle function.  If you are using OleDB to read a file and not running the query through an Oracle database engine, then you cannot use Oracle functions.

You would need an OleDB specific function when not connecting to an Oracle database.

That I cannot help with.
holemaniaAuthor Commented:
Do you know if there's a sql query that can take the following date string and convert into decimal?


When convert into Decimal should be:

I think if I can get a straight sql query to convert that and would work with Oracle, it should solve my issue.
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

slightwv (䄆 Netminder) Commented:
Are you reading from an Oracle database or from a CSV file on disk?
You would read your dataset as is and then parse the field in question using the TimeSpan.TryParseExact method that takes a list (or array) of formats, if the string to parse does not match one of the formats, the parse fails, by using TryParseExact you can opt to use a default value and issue an error; e.g. -
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Imports System.Threading

Module Module1
	Sub Main()
		Dim [data] = New List(Of Data)() From {
			New Data() With {.Name = "Job1", .Order = "12345", .Job = "1", .Labor = ":15"}, _
			New Data() With {.Name = "Job2", .Order = "98765", .Job = "2", .Labor = "2:30"}, _
			New Data() With {.Name = "Job3", .Order = "19283", .Job = "3", .Labor = "4:00"}, _
			New Data() With {.Name = "Job4", .Order = "56473", .Job = "4", .Labor = ":00"}
		Dim table = [data].ConvertToDataTable()
		Dim formats = New String() {"%h", "%h\:%m", "\:%m"}
		Dim result = TimeSpan.MinValue
		For Each row In table.Rows
			If TimeSpan.TryParseExact(row("Labor"), formats, Thread.CurrentThread.CurrentCulture, result) Then
				Console.WriteLine("{0} - Labor: {1}", row("Name"), Convert.ToDecimal(result.TotalHours))
				Console.WriteLine("Parse failed for - {0} - Labor: {1}", row("Name"), row("Labor"))
			End If
	End Sub
End Module

Class Data
	Public Property Name() As String
	Public Property Order() As String
	Public Property Job() As String
	Public Property Labor() As String
End Class

Module Extensions
	<Extension()> _
	Public Function ConvertToDataTable(Of T)(ByVal source As IEnumerable(Of T)) As DataTable
		Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(T))
		Dim table As DataTable = New DataTable()

		For i As Integer = 0 To properties.Count - 1
			Dim [property] As PropertyDescriptor = properties(i)
			If [property].PropertyType.IsGenericType AndAlso [property].PropertyType.GetGenericTypeDefinition().Equals(GetType(Nullable)) Then
				table.Columns.Add([property].Name, [property].PropertyType.GetGenericArguments()(0))
				table.Columns.Add([property].Name, [property].PropertyType)
			End If

		Dim values(properties.Count - 1) As Object
		For Each item As T In source
			For i As Integer = 0 To properties.Count - 1
				values(i) = properties(i).GetValue(item)

		Return table
	End Function
End Module

Open in new window

Which produces the following output -Capture.JPG
holemaniaAuthor Commented:
I am reading a CSV file and doing a bulkcopy into Oracle.  However, the "Duration" field is not a string and I need to convert it into decimal before I can dump it.  Was hoping I can convert it from my example I provided with the original thread.

This is my code snippit to read from CSV file and doing the Bulkcopy.
            Dim excelCon As String
            Dim conn As String = ConnectionString()

            excelCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + csvFilePath + ";Extended Properties=""TEXT;HDR=YES;FMT=Delimited;Characterset=ANSI;"""

            Dim excelstrCon As New OleDb.OleDbConnection(excelCon)

            Dim cmd As New OleDb.OleDbCommand("SELECT [Name], [ORDER], [JOB], [DURATION] FROM [" + csvFileName + "]", excelstrCon)
            Dim reader As OleDb.OleDbDataReader = cmd.ExecuteReader

            Dim dbCon As OracleConnection = New OracleConnection(conn)


            Dim bulkCopy As OracleBulkCopy = New OracleBulkCopy(dbCon)

            bulkCopy.DestinationTableName = "LABOR"
            bulkCopy.BulkCopyTimeout = 500

Open in new window

holemaniaAuthor Commented:
This is what I end up doing is using a staging table.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.