Excel to sql datetime

How do I import and preserve date format fields such as «8/21/2012» from Excel to MySQL?

I am using MySQL Workbench and the Excel MySQL Excel data transfer plug in. When I select the Excel data I want to import into MySQL, I get a window where I declare variable types for all fields. All fields and declarations work as expected except for date and time fields. Both date and time switch from 8/21/2012 to a number like 398475 etc. How can I import these fields into MySQL by preserving the dashed mm/dd/yyyy format? I assume the same procedure will work for time as well.

Alternatively, is there a way to convert the serialized datetime value (a float, representing the number of days since 1/1/1900) back to mm/dd/yyyy in MySQL ?

Thank you!

asked Nov 30, 2013 at 21:16

user3053096's user avatar

1

You can convert your date cells into a MySQL supported format using the TEXT function.

=TEXT(A1,"YYYY-MM-DD")

This will convert a date in cell A1 to the yyyy-mm-dd format that the MySQL date field expects.

answered Dec 9, 2013 at 22:45

Andy's user avatar

AndyAndy

48.5k58 gold badges167 silver badges230 bronze badges

To get the serialized date format (say 36422) into a useful date format in MYSQL use the interval function added to the base date that Excel uses (which is actually 1900-00-00 but since that doesn’t exist you will have to use 1900-01-01 which is why we subtract 2 from the date column)

`'1900-01-01' + INTERVAL(Your_Date_Column - 2)DAY`

answered Apr 6, 2016 at 18:05

Sean Caswell's user avatar

You can convert your datetime cells into a MySQL supported format using the TEXT function

=TEXT(A1,"YYYY-MM-DD HH:MM:SS")

answered Oct 16, 2019 at 20:48

Beachhouse's user avatar

BeachhouseBeachhouse

4,9423 gold badges24 silver badges39 bronze badges

Simply use Date (java.util package) for this.

Date d=Date date=cell.getDateCellValue();

Now Use it as you want.

answered Feb 3, 2017 at 10:21

Anshu kumar's user avatar

Anshu kumarAnshu kumar

4075 silver badges5 bronze badges

1

When creating SQL statements you’ll often need a date in the ISO 8601 standard format (e.g. 2010-03-26 12:34).

Of course you can change the format in Excel to show it as such, but that doesn’t give you the string you need, e.g. in an insert or update statement.

Here’s an Excel function to make an SQL date value, presuming the date value is in cell A1:

=TEXT(A1,”yyyy-mm-dd hh:MM:ss”)

This circumvents the use of complicated IF and date/time functions. Append a “Z” if you need to indicate the timezone as UTC (i.e. GMT) time.

Here’s a short VBA function to create this type of date

Function SQLDate(d)
SQLDate = WorksheetFunction.Text(d, "yyyy-mm-dd hh:MM:ss")
End Function

Put this code in a new module in your workbook to instantly start using the function in Excel like this: “=SQLDate(A1)”

SQL date in Excel

SQL date in Excel

More information:

http://en.wikipedia.org/wiki/ISO_8601

Michiel van der Blonk, software developer, trainer, writer, speedcuber, just zis guy you know….
View all posts by michiel

User-1320770751 posted

Hi,

 I have an odd request….

I am importing data from an excel spreadsheet to a sql db. Inside the excel spreadsheet, I have a column with dates in it, I need these dates to be imported as text to the sql DB field(which is a varchar datatype).

The problem is, at the moment, if I insert a date 2006/08/30 it displays it like this in the sql DB: 38959

Is there a way this can be converted back to the date? even possibly before it is stored in the sql DB ?

I am currently bulk copying the excel data to sql DB. The code im using is below: Thank you for any help/assistance

Dim excelConnectionString
As
String =
«Data Source=» & FileUpload1.PostedFile.FileName &
«;Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;»


Using connection
As
New OleDbConnection(excelConnectionString)

‘Reading the specific fields in the excel spreadsheet NOTE: If not all the fields are selected, an error will appear


Dim command
As
New OleDbCommand(«Select 0, MSISDN, Sim_Number, Service_Provider, Package_Description, Activation_Date,
Activation_Date_Action, Pin, Puk, MSISDN_Status, Deactivation_Date, «
& _«Contract_Period, Deactivated_Date, Network_Sub, OBS, BillingStatus,
ID_ProductInstance FROM [Sheet1$]»
, connection)

connection.Open()

Using dr
As OleDbDataReader = command.ExecuteReader()

‘ SQL Server Connection String

Dim sqlConnectionString
As
String =
«Data Source=Server; Initial Catalog=Table; Connect Timeout=200; pooling=’true’; Max Pool Size=200; Integrated Security=True»

‘ Bulk Copy to SQL Server


Using bulkCopy
As
New SqlBulkCopy(sqlConnectionString)

bulkCopy.DestinationTableName = «Table_Name»


bulkCopy.BulkCopyTimeout = 1000

bulkCopy.WriteToServer(dr)

End
Using

End
Using

End
Using


Dim cn
As
New SqlClient.SqlConnection()

Dim cm
As
New SqlClient.SqlCommand()


With cn

‘Stating which database the fields need to be stored into

.ConnectionString = «Data Source=Server; Initial Catalog=Table;Integrated Security=True»


.Open()

cm.CommandTimeout = 100

End
With

Quote:

The destination SQL Server column is varchar and I would prefer not to have to change it in this release.

That’s a big mistake. It’s simple to get the data from excel into the field, but once it’s there, it’s there until you need to use it — and at that point you will start to get problems. NVARCHAR fields should only be used for text data — mobile numbers, names, addresses, product descriptions: anything you aren’t going to try to compare numerically or sort by anything other than a basic string comparison.
And that includes dates. In a string based sort, the comparison is wholly based on the first pair of different characters in the two strings, everything else is ignored.
so «2/3/2022» is before «4/3/1955» but after «1/3/2100».

And converting this when you need to use it is a nightmare as someone. somewhere, somewhen will put «today» or «I dunno» in there instead and your code will fail.

Bite the bullet, change your datatype to DATE, DATETIME, or DATETIME2 and the «month first» / «day first» problem becomes just a matter of preference in the presentation software, instead of a nightmare that will haunt you for years to come.

My problem is kind of weird

In our project we mainly write data from excel to sql-server tables

The date in the excel sheet is in excel’s propriety date format i.e 40630.38042 which is equivalent to 03/28/2011 xx:xx:xx(some date time value)

Now the problem is that we were using the code to convert this decimal date value to varchar value 03/28/2011 using this formula

CONVERT([varchar](10), dateadd(day,-2,convert(decimal,[orderdate])),(101))

orderdate here is in this format — 40630.38042

Now after 6 months of uploading hundreds of excel sheets we realized that the dates were not getting converted right

the time part after the decimal place was messing up this logic — and we don’t even need time in our database

so I tried CONVERT([varchar](10), dateadd(day,-2,convert(decimal,substring([orderdate],1,5))),(101))

and it seems to work fine

Now to apply the correct formula I first need to change them back to the original decimal date format

so basically I need a reverse formula for

CONVERT([varchar](10), dateadd(day,-2,convert(decimal,[orderdate])),(101))

Is there a way to do this??

If not I’ll have to upload all the data (100+ excel sheets again) — which will not make my clients very happy 🙁

Thanks,

Kavita

Понравилась статья? Поделить с друзьями:
  • Excel to png image
  • Excel to php script
  • Excel to php array online
  • Excel to pdf with formulas
  • Excel to pdf script