Importing data from excel and inserting into sql tables - sql

I am Importing data from excel and inserting into a sql table. One of the fields in my Excel file is populated with a time value. I want to import that time value as a string into my sql table. When i do that i get a weird value in my sql table. Excel value is: 07:00 and after inserting that as a string into the sql table the time values looks like this: 0,29166666667.
The reason for importing it as a string value is that you have to be able to define Days in the same field. Like this : D2 10:30. When i import this kind of values it is inserted correctly.
can anyone help ?

Excel stores dates and times as number-values from 0 to 0.99999999 +x days.
0.29166666667 would be like 00.01.1900 07:00:00, which seems to be correct in your case.
So, you would have to use some reformatting or conversion of this value, before using it as a direct string-input.
In VBA you could use Format(myValue,"DD-MM-YYYY hh:mm:ss").
The equivalent worksheet function would be TEXT(A1,"DD-MM-YYYY hh:mm:ss").
The format-code depends on your regional settings. You might want to try something like this Format(myTime, "Long Time"), if you preffer to use excel-defined time-formats.
Because you did not post any code, I am not sure about how you import your excel-data. But I would say, the fastest way to get better results, would be setting up a new column, using the TEXT formula with a reference to the previously time-column and use this new formatted column as input for your sql-db.

Related

SSIS importing percentage column from Excel displaying as NULL in database

I have an ETL process set up to take data from an Excel spreadsheet and store it in a database using SSIS. However, one of the columns in the the Excel file is formatted as a percent, and it will sometimes erroneously be stored as a NULL value in the database, as if there was some sort of translation error.
Pictured is the exact format being used for the column in Excel.
Interestingly, these percent values do load properly on some days, but for some reason one particular Excel sheet I was given as an example of this issue will not load any of them at all when put through the SSIS processor.
In Excel, these values will show up like "50.00%", and when the SSIS processor is able to translate them properly it will display as the decimal equivalent in the database, "0.5", which is what I want instead of the NULL values. The data type I am using in SSIS for this is Unicode string [DT_WSTR], and it is saved as an NVARCHAR in the database.
Any insight as to why these values will sometimes not display/translate as intended? I have tried messing around with the data types in SSIS/SQL Server, but it has either resulted in no change or error. When I put test values in the Excel sheet, such as "test" to see if it is importing anything at all from this column, it does seem to work (just not for the percent numbers that I need).
The issue was caused by the "mixed data types" that were present in the first few rows of my data (the "mixed" part being blank fields), which would explain why some sheets would work and others wouldn't.
https://stackoverflow.com/a/542573/11815822
Setting the connection string to accommodate for this fixed the issue.

Import Date column in a particular format

I have an excel file which has a date column with different datatypes.
For eg: few values have Date data-type while others have string format.
I tried to import the data and change the column metadata type to string but it changes the date values completely.
I have attached a few screenshots of the data, and I very new to Pentaho, so can anybody help me understand how to tackle this problem.
I tried changing the metadata type or using str2date function in js step but still, no use as data imported is different from the data from the file
When importing from an Excel sheet with some invalid dates, you can import as string format, then use a Select Values step.
Specify the date field on the Meta-data tab with the correct format (dd/MM/yyyy) and Date format Lenient? set to Y. This should change the 29/02/2017 to 01/03/2017, which is a decent option.
Also, don't use Excel to inspect the results, because it might be screwing up the conversion on re-import. Look at the preview data in Spoon or export to csv and look with a text editor to see if the format is correct first.
After changing the data type and importing the date values as string. Using SQL to store the date in string format and then formatting it while retrieving solved the issue.

Import data into SQL Server 2012 using wizard lost data (have date and empty cells in the same column)

I know this is not a new issue and I have browsed a lot but could not figure it out.
The table contains a column with a few dates (like 25-Apr-16) while the majority of the rows are empty. After importing to SQL, there is only NULL left; all the dates were gone.
I have tried three methods:
save the file to .xls, change the column to Date format;
save the file to .xls, fill up empty with NULL, change the column to Date format;
save to .csv;
So far, none of them worked.
At last, I tried this and it worked, but I am wondering if there are better ways:
save the file to .xls, fill up empty with 12/31/9999, after importing, update 12/31/9999 to NULL.
The issue when you import data to SQL Server is that SQL Server try to wonder what kind of data is in each cell of the sheet, and it does their research on the first 100 records. So if there is anything on the top 100 records of a cell, SQL Server considers that the cell is empty. To avoid that, you can fill the first record of your Excel sheet with dummy values and after that import to SQL Server.

Date in a short text data type field... Select query trouble

In my Access database, I have a table called customers. In this table I have a column called DateEntered. The data type for the field is short text.
The values in this column are not coherent - they come in several variations:
MM-DD-YYYY,
MMDDYYYY and
MM/DD/YYYY.
There doesn't seem to be any standard set.
My goal is to select all customers from 2012. I tried
select *
from customers
where DateEntered <('%2013') AND >('%2012');
but it comes up blank when I run it.
Can anyone point out what I'm failing to do correctly & more importantly explain why exactly this query doesn't work in Access? From my understanding of SQL (not very advanced) this should work.
Another variant)
select * from customers where RIGHT(DateEntered, 4) = '2012'
If you have control over the database and application code, the best way to handle this is to use an actual Date field instead of text in the table.
One way to handle this would be to add a new field to the table, write a query or two to correctly convert the text values to actual date values, and populate the new field.
At this point, you would then need to hunt down the application code the refers to this field in any way and adjust to treat the field as a date, not text. This includes your insert and update statements, report code, etc.
Finally, as a last step, I would rename the original text field (or remove it altogether) and rename the new date field to the original field name.
Once you fix the problem, querying against the field will be a piece of cake.
Alternatively, if you can't alter the table and source code, you can use the date conversion function CDATE() to convert the text value to an actual date. Note that you may need to guard against non-date entries (NULL or empty string values, as well as other text values that aren't really dates in the first place). The IsDate() function can be your friend here.
If you have the time and patience, fixing the data and code is the better approach to take, but sometimes this isn't always feasible.
Why don't you use LIKE operators (they're appropriate when you have a pattern using % and _):
select * from customers where DateEntered like '%2013' or DateEntered like '%2012'

How to convert a text field to a date/time field in Access 2010?

I am importing an excel file into Access 2010 and the date field(CALLDATE) comes in as text(YYYYMMDD). I would like to use an update query to update a new field "dateofcall" but using a date/time format. I tried to use:
UPDATE tbl_Import SET tbl_Import.dateofcall = CDate([tbl_Import].[CALLDATE]);
I thought it was going to be that simple but it shows up blank with a date format. I also tried to use DateSerriel() but still was getting errors. Any suggestions?
You can use left, right and mid string functions to construct a date from the various parts of the string.
For example:
DateSerial(Left(MyTextDate,4),Mid(MyTextDate,5,2),Right(MytextDate,2))
You can use the above in an Update query to update a date type coulmn 9field) to a the date from the text column.
My date came in as text looking like this:"2013-03-23 00:00:00.0"
I take the left 10 characters only, "2013-03-23", this makes it so Access can recognize it is a date field and then I just switch around the format.
Format(Left([WEEKEND],10),"m/d/yyyy")