Importing date data from excel into table with right format conversion - sql

I am importing data from excel files into SQL server database. In excel ,Date field are in the format mm-dd-yyyy. Whereas SQL database imports it as yyyy-mm-dd, swapping dates to months.For ex. in excel its 01-12-2018 but in SQL it imports as 2018-12-01. Hence the dates are incorrect.
Please let me know a way to import it correctly in SQL.
I am happy to import in both ways : yyyy-mm-dd or yyyy-dd-mm. I just want it to read the date correctly form excel file.
I am using import export wizard.

Updated answer:
OP mentioned in another answer comment
I am currently using import export wizard to import data.
To solve problem in Import export wizard.
Choose Flat file source - I used a sample file (sample.csv) with this data
id, dates, text
1,08-09-2018,"sample"
2,05-09-2019,"more sample"
Under Choose your data source I went to Advanced tab and ensured that dates are imported as string.
At the Select Source Table and Views step, go into Edit Mappings>Edit SQL and then change the table creation query to have an extra calculated column (say cdates) with definition like [cdates] as convert(date,[ dates],110). As you can see I added this in my SQL as last column in definition.
In the case that you don't create table but insert into existing table. Alter the table to have a calculated column over the varchar date column.
See the output I got
Original Answer:
You should import the dates as nvarchar(10) into the table and then cast them after they have been imported.
Typically nvarchar type structure is followed for all purpose and is also called staging tables. In staging tables most of the relevant mistrusted data fields are of nvarchar(N) type allowing them to be successfully imported into SQL server.
After import, you should take data from staging tables into desired tables using properly casted/converted columns in a MERGE or UP-SERT query.
In you case you should use explicit convert like
CONVERT(date,your_staging_date_column ,110)

If data is set as Date in Excel, you can convert it to nvarchar, and then back to date as follows:
CONVERT(date, convert(nvarchar, [date from excel]), 110);
110 in the end means that it will convert from format mm-dd-yyyy as your data is currently formatted in Excel.
If you are reading data from Excel as pure text, then it will be enough to convert that text to date:
CONVERT(date, [date from excel], 110);
This function will convert data to correct format in your table.

Related

How to solve error gdk-05030 when importing csv data in SQL Developer

I have a problem importing a CSV file using SQL Developer. I created a table to import a 'date' data by using the code below
CREATE TABLE DEPARTMENT (
DATECOLUMN DATE
);
I imported the CSV by right clicking and so on.
Interestingly, the CSV 'date' data has a format of 'YYYY-MM-DD', but when loaded in SQL developer (preview, before importing), it took the format of 'MM/DD/YYYY'.
I believe this is the problem, because when trying to import, it returned error "gdk-05030", meaning the data contains more than needed (to fit the format).
To me, it looks as follows:
date format in a CSV file is yyyy-mm-dd (as you said)
when you queried the table after insert, you found out that values look differently - mm/dd/yyyy
it is because current settings use that date format. If you want to change it, run
alter session set nls_date_format = 'yyyy-mm-dd';
and then select rows from the table.
alternatively, go to SQL Developer's Preferences, and set desired date format in its [Database - NLS] section.

Excel date insert to SQL Server table as 44381

I'm facing problem when Excel date column data were import SQL Server table through Logic apps.
Excel value shows as 7/4/2021 column. which was need import is a varchar column in the table. After data import it shows as 44381. Is there any solution for it.
Did you preview the data before run the pipeline? I tested load the same data from excel file to SQL table varchar column, everything works well. We can't repeat you error. Please check if there are any different steps between you and I.
This my source excel file:
Source dataset preview:
These three columns are both considered as String data type. My sink table schema and pipeline output:

Import/convert my date column from Excel into a datetime datatype in SQL Server?

I am importing an Excel file in CSV format. The file has 1,048,576 rows.
So far I have not been able to import the file with datetime datatype or convert/cast the columns with dates to datetime data types. I can only import the date column as a nvarchar, varchar or sql variant. The only time I can convert the column is if the file is under 71000 rows. Otherwise I get an error that the conversion from string is out of range or failed. (Please see below for pictures and more detail)
I have tried the following
Casting or converting the columns
Changing the data type via the table design
Importing with date time datatype
Copy and paste the data to a datetime column
Checking and converting the date column in excel, before importing
Batch Inserting the document
Importing as a text file
Removing null columns in the excel file
Importing as non string variable
Creating a temporary table
Making temporary variables with datetime data types
Notes
I am using the developer edition of sql server
I am trying to get the date columns(transaction_date, date_created) into a
date time datatype, I can only have them as a narvchar datatype currently
How I Import The CSV File
Right clicking database, clicking task and import flat file
Error Messages
Error message when trying to convert via table design
Error message when importing csv file to sql server
Sample of The CSV File
Sample of the CSV File Showing About 10 Rows
Database Schema
Brief Synopsis of Desired Database, Trying to Get Date Columns to DateTime Data type
Function SQLDate(d)
SQLDate = WorksheetFunction.Text(d, "yyyy-mm-dd hh:MM:ss")
End Function
Note: "d" refers to date field.
Alternatively, you could convert Excel data to string:
TEXT(d,"dd mmmm yyyy hh:mm:ss")
Then convert as req. from within SQL. For further clarity in this regard - see screenshot below.
Citation: Thanks to H Javed

date converting incorrectly with ssms import data wizard (Azure db)

I have update and delete permissions (not ALTER) for a Azure db table and am trying to import a CSV file using the SSMS import data wizard. The import works ok except that it changes the date in a field to today's date. For example the field on the CSV looks like this:
"Jun 01, 2018 01:37AM"
After it runs through the wizard it looks like this in my table:
2018-06-13 01:37:00.000
The datatype that I chose for the date in my CSV file during the import was:
database time [DT_DBTIME]
The datatype specified for that field in my table is:
datetime
Any idea what I'm doing wrong? Am I choosing the wrong datatype for the field in the csv?
It is because of the file data type (DT_DBTIME). This is not the correct datatype, you need to choose DT_DBTIMESTAMP to map correctly to datetime. I presume it is using the current date of the SQL server and is in effect only importing the time (01:37AM) element from your csv.

How to insert values with out scientific notation in ssis package creation?

I developing package for import data from excel to SQL DB using SSIS.
In one of the excel sheet following value present no format is used it's general .
0.0000316
0.0000088
0.0000022
0.000001
I insert above value into DB following data present in DB. in DB it's float data type.
3.16E-05
8.8E-06
2.2E-06
1E-06
How to insert without E . I need to insert same value in excel sheet. It's possible?
The data in the database is exactly the same data in the Excel. The only difference is the way in which it's shown. I don't know what are you using that data for, but I can assure you that you'll get the same data when fyou do calculations as you would in Excel (Excel also uses flotaing point numbers).
If you need, for some reason, to see the query results in a particular format, use SQL Server FORMAT function.