Converting text to date format SSIS - sql-server

I am using SSIS to import data from Excel to SQL Server database.
The date column has nvarchar data type, and I want to change it to a date data type YYYY-MM-DD.
If the value is null then I want to get null.
I used [mycolumn] date, on the create a new table option on the OLE DB destination.
Is that right?

Add a Data Conversion Data Flow Component.
Right click, Show Advanced Editor...
Goto Input and Output Properties
Expand Data Conversion Output and click on the date column
Set FastParse to True in the Custom Properties
Make sure the data type is set to database date [DT_DBDATE]
Run the ssis package again the yyyymmdd dates should flow smoothly from nvarchar to date

Convert [mycolumn] to the needed DATE format before inserting it to the table. Try this:-
SELECT CONVERT(CHAR(10), [mycolumn],126)

Related

When using SSIS to import a CSV file, how can you replace missing dates with the current UTC datetime?

I am creating an SSIS package to import a CSV file into a SQL Server table.
The CSV file has a date column and the value will be missing from some of the rows.
The missing value is represented by two consecutive commas(i.e. val1,,va3).
When the value is missing, I want to insert the current date and time in UTC.
Within a Dervied Column Transformation I am using:
REPLACENULL(DateCreated, GetUtcDate())
This doesn't work; instead, the value 0001-01-01 00:00:00.0000000 is being inserted.
Details:
The SQL Server table is expecting the date format: Datetime2(7)
The flat file connection manager uses database timestamp with precision [DT_DBTIMESTAMP2]
How can I replace the missing values with the current UTC datetime?
The column might not be NULL so the REPLACENULL isn't triggering
The default flat file source behavior is to replace empty column values with an empty string or empty date, double check on your flatfile source and ensure that your are importing empty columns as NULLs.
The REPLACENULL should then register the column as being NULL and replace it with GetUtcDate()

Importing Flat File into SQL Server stores incorrect dates In the database

I am a SQL student who has been tasked with loading data into SQL Server for a company that I intern with.
I have tried loading multiple flat files with dates formatted as 1/23/04 into SSMS and when going through the wizard the dates preview correctly. Once they're loaded into the actual database and a select query is performed, all dates return as 2023-01-04 format.
I'm not sure where to even begin to fix this. I've loaded columns as nvarchar(50) as opposed to date, datetime, and datetime2 to see if it would make a difference, and each case returns the same format. Is this a setting in the flat file, SSMS, or the computer itself?
In SSIS bring in the column (with the dates) as a string and add a derived column transform that will transform the column (using the substring function) to the correct date. SQL Server loves seeing dates as YYYY-MM-DD so that is why without explicitly telling it how to read the string it defaults to thinking that the inputted date is of that format.
If you are using SQL Server (SSMS) you should input it as a string (char(8)) and then use cast or convert functions to change the string into a date. You can then issue a 'Alter table drop column' to drop the string version column of the date.

Microsoft SQL server edit imported file

I'm new in SQL server. I have imported an Excel file into my SQL Server Database. There is a column with the Name of myDate and it shows dates like: 20.07.2018.
I did right click on my table, then I chose "Select 1000 rows" and added the convert code there but it didn't work! I have the same result as before! (the Format didn't change). Is there anyone who knows why it didn't change?
Here is what I wrote:
select convert (varchar,[dateColumn],102) as date2
from [test1].[dbo].[change1]
Go
Having a column's name as myDate doesn't mean its datatype is date, datetime or datetime2. Apparently your data type is VARCHAR. You should use date, datetime or datetime2 for such a column.
For conversion you can do it like this:
SET DATEFORMAT DMY;
SELECT convert (varchar, TRY_CAST(myDate AS DATE),102) as date2
from [test1].[dbo].[change1];
(Again this is questionable why would you convert to a varchar).

SQL Server - data lost when converted datetime to varchar

I had to restore a table that contained a datetime column.
I used bulk insert to insert the data from a CSV file. However the import couldn't insert the datetime values because SQL server saw it as a different format.
I ended up modifying the table, removing the datetime data type and replacing it with a varchar.
The issue is the data got converted from this format: 7/15/2015 3:41:57 PM to something like this: 47:47.0
Is there a way I can convert these values back or is the data lost?
As #ChrisSteele mentioned, your data is hosed. It likely got this way by Excel's cool feature to convert datetime strings to integers. Try re-saving the original file in notepad, or changing the format of the column from Date/Datetime to Text if you're using Excel.

SQL DATETIME Insert from Excel?

So im having a rather strange problem, I have a Column (lets say Column A) in excel that has data that looks like this:
4/11/2015 10:14
I have a bunch of other columns, but anyways in my SQL Insert statement within excel, the data (when copying out) looks like this:
42105.4561921296
The ="INSERT INTO TABLE VALUES ('"&A1&"', Etc....)" is in the data format of "general" and the Date column is in the format of "Custom" where there is a M/DD/YYYY MM/HH type within.
The SQL Column is of the data type DATETIME, so it of course doesn't accept the weird number it gets.
Any ideas? changing the format of the "SQL INSERT" column doesn't change the results.
You are right - Excel formats only changes the way the numbers are displayed, not the underlying value of the cell. In this case, the value of the cell is an excel date-time value, which is the # of days since 1/1/1900, with decimals for the time.
I'd recommend using Excel's TEXT function to convert Excel's numeric date-time value to a text string that can be inserted into SQL:
Instead of:
INSERT INTO TABLE VALUES ('"&A1&"', Etc....)"
Try:
INSERT INTO TABLE VALUES ('"&TEXT(A1,"YYYY-MM-DD HH:MM")&"', Etc...)"
The best format to insert a date time into a datetime column is to present the date and time as YYYY-MM-DD Minute:Second or 2015-04-15 12:52
so to insert a datetime from Excel you can use this set of Excel functions:
(where A1 contains the datetime to be saved)
=YEAR(A1)&"-"&RIGHT("00"&MONTH(A1),2)&"-"&RIGHT("00"&DAY(A1),2)&" "&RIGHT("00"&HOUR(A1),2)&":"&RIGHT("00"&MINUTE(A1),2)&":"&RIGHT("00"&SECOND(A1),2)

Resources