thebatfink
Well-known member
- Joined
- Mar 29, 2012
- Messages
- 47
- Programming Experience
- Beginner
Hi, I wasn't entirely sure where to post this as I don't know if it's a problem with SQLCE, my SQL syntax or Excel. But here is my issue.
I have dates input into a field in my SQLCE field. The field is format DATETIME. I load up a datatable with output from my SQL query and then cycle through all the fields in the datatable and copy their values to an open spreadsheet.
I think the problem is caused by be outputting a string maybe TempDataTable.Rows(iX)(iY).ToString() ?
When I look on the worksheet, it is showing strings for dates which do not adhere to the MM/DD/YY format, ie if my date is 13th or higher, it shows a string in the cell (because there isn't a 13th Month presumably) for example 25/01/2012. But where the date is 12th or below, it is converting the string to a date using MM/DD/YYYY for example, my date 01/02/2013 is being converted to 2nd January 2013.
I have tried modifying the SQL to this..
I believe this takes the datetime value and changes it to a string with the formatting I want ie DD/MM/YY, but still when this is pasted into Excel, its formatting dates which could be considered in the format MM/DD/YY.
If any of that makes sense, I would really appreciate any help sorting out the problem
Thanks!
I have dates input into a field in my SQLCE field. The field is format DATETIME. I load up a datatable with output from my SQL query and then cycle through all the fields in the datatable and copy their values to an open spreadsheet.
I think the problem is caused by be outputting a string maybe TempDataTable.Rows(iX)(iY).ToString() ?
VB.NET:
SQLCeDA = New SqlCeDataAdapter("SELECT RawDataTable.PlanDay, PolyTable.PolyType, SUM(RawDataTable.Length) [Length (LM)] " & _
"FROM PolyTable " & _
"INNER JOIN RawDataTable on PolyTable.RawDataID = RawDataTable.RawDataID " & _
"GROUP BY PolyType, PlanDay ORDER BY PlanDay", SQLCon)
VB.NET:
If SQLCon.State = ConnectionState.Closed Then SQLCon.Open()
TempDataTable = New DataTable
SQLCeDA.Fill(TempDataTable)
For iC = 0 To TempDataTable.Columns.Count - 1
ExcelWorkSheet.Cells(1, iC + 1).Value = TempDataTable.Columns(iC).ColumnName
ExcelWorkSheet.Cells(1, iC + 1).font.bold = True
Next
ExcelWorkSheet.Rows(2).select()
For iX = 0 To TempDataTable.Rows.Count - 1
For iY = 0 To TempDataTable.Columns.Count - 1
ExcelWorkSheet.Cells(iX + 2, iY + 1).value = TempDataTable.Rows(iX)(iY).ToString()
Next
Next
I have tried modifying the SQL to this..
VB.NET:
SQLCeDA = New SqlCeDataAdapter("SELECT CONVERT(nvarchar(10), RawDataTable.PlanDay, 103) as [Date], PolyTable.PolyType, " & _
"SUM(RawDataTable.Length) as [Length (LM)] " & _
"FROM PolyTable " & _
"INNER JOIN RawDataTable on PolyTable.RawDataID = RawDataTable.RawDataID " & _
"GROUP BY PolyType, PlanDay ORDER BY PlanDay", SQLCon)
If any of that makes sense, I would really appreciate any help sorting out the problem
Thanks!