SSIS Conversion Failure From Numeric Field to Date - sql-server

I have a field in an AS400 table which is a Numeric (decimal 8) field. Within this, dates are stored YYYYMMDD format, such as 20180518.
I then have a stage table in MS SQL Server that I am dumping this data to before processing. The destination column is of type Date.
I am having trouble getting the SSIS package to pass the values accordingly. What we've tried doing is pulling apart the numeric field as a string, and grabbing the sub-strings. Then concatenating the sections to assemble a MM/DD/YYYY formatted string.
substring(myfield,5,2) || '-' || substring(myfield,7,2) || '-' || substring(myfield,1,4)
We also tried using the Date() function on the AS400..
Date(substring(myfield,1,4) || '-' || substring(myfield,5,2) || '-' || substring(myfield,7,2))
With neither of these options working, I then tried using the SSIS conversion tool to perform the task. I changed my query back to just pull the field in, and then pass it to a data conversion tool. Within the tool, it first sees the column as a decimal.
[Input Column][Output Alias][Data Type]
[myfield ][AliasMyField][decimal[DT_DECIMAL]]
I then changed this to be
[Input Column][Output Alias][Data Type]
[myfield ][AliasMyField][date[DT_DATE]]
It seems no matter which avenue we attempt the package will not execute, and I keep getting:
Conversion failed because the data value overflowed the specified type.

You don't need to grab any substrings. If you insert a String in YYYYMMDD format into a SQL Server datetime field, it will insert with no problem, regardless of your regional settings.
EDIT: A definite way to solve this is to have your dataflow import the data to a staging table that receives the 8-digit date into a varchar field without any modifications.
Then move the data to the final table using a TRY_CONVERT() to convert the varchar to a datetime, or NULL if it's unable to convert a particular value.

probably have an invalid date "numbers", 20180229, 20171305, 00000000 or 99999999...
or it could be a valid date for the i, but not SQL server, 00010101 or 99991231 for instance (unless you're using the newer date or datetime2 types)
You need a function that does the conversion and catches the conversion error substitutes a valid date or returns NULL if that's acceptable.
When NULL isn't acceptable, I usually do something like
< 00010101 ==> 00010101
> 99991231 ==> 99991231
= xxxx0229 ==> xxxx0228
Those are the easy ones...any others you have to decide for yourself

Related

T SQL Conversion failed when converting date and/or time from character string from VARCHAR(MAX)

I'm using SQL Server 2014. I have a date stored as varchar(MAX) in the format of:
2019-02-18
However, I want it in the British format dd/mm/yyyy (103).
This is my SQL:
SELECT CONVERT(DATE, DateField, 103) AS "JobStartDate"
FROM tblTest
However, I keep getting this error:
Conversion failed when converting date and/or time from character string.
What am I missing?
Update: The date is initially stored as varchar max as it is coming from a 3rd party system. I have no control over this and I completly understand this is the wrong format, but this is what I have been given.
I have a date stored as varchar(MAX)
There's your problem right there.
Not only you are using the wrong data type to store dates, you are also using max which is a known performance killer.
The solution to the problem is to alter the table and store dates in a Date data type - but first, you must look up all the objects that depends on that column and make sure they will not break or change them as well.
Assuming this can't be done, or as a temporary workaround, you must first convert the data you have to Date, and then convert it back to a string representation of that date using the 103 style to get dd/mm/yyyy.
Since yyyy-mm-dd string format is not culture dependent with the date data type, you can simply do this:
SELECT CONVERT(char(10), TRY_CAST(DateField As Date), 103) As [JobStartDate]
FROM tblTest
Note I've used try_cast and not cast since the database can't stop you from storing values that can't be converted to dates in that column.
You want to format the DateField column and not convert it to date.
So first convert it to DATE and then apply the format:
SELECT FORMAT(CONVERT(DATE, DateField, 21), 'dd/MM/yyyy') AS JobStartDate
See the demo.

How to write where condition for column having decimal datatype in SQL

I have a date column which is of type "PIC S9(7) COMP-3" in IBM DB2/AS400.
When I put these values in SQL Server, my data type of column made changed as "decimal(7,0)".This is just to making DB2 datatype similar to SQL Server.
Now, I would like to find if there is any "space" or "numeric" in this particular date column.
The date column is like this:
DATE
-------
4040404
(a space)
404040
2020202
(a space)
202020
In where condition like this gives error:
"Error converting data type varchar to numeric."
Select ID, DATE
from Table1
Where DATE = ''
How to resolve this?
Because a space is occurring in the column, data type should default to varchar.
Explicitly convert the column (to avoid data type errors) to varchar and then check for floating spaces or numbers using wildcards. E.g.
where Convert(varchar, Column_Date) like '%[ ]%' or
where Convert(varchar, Column_Date) like '%[0-9]%' depending on what you want to filter out.

Convert from 'YYYYMMDD' format to to SQL Datetime2

I have an issue concerning conversion in SSIS.
I'm trying to convert StartDATE from DT_WSTR to Datetime2 (for SQL Server)
My date originaly looks like this 20140804 but I need to convert it to Datetime2 in such format 2014-08-04 00:00:00.0000000.
What I've done earlier with the StartDATE Column is:
RTRIM(DATSTHYRA)
Since I need to remove blank spaces...
I figured I can use the already Derived Column and add a new expression to convert it to Datetime2 but I'm running into issues and can't really find a topic online that covers my issue.
You can do it in a single step.
Add Derived Column transformation - transform your YYYYMMDD string to YYYY-MM-DD with SUBSTRING functions and then - cast to DT_DBTIMESTAMP2 with scale needed. This would yield an expression like
(DT_DBTIMESTAMP2, 7)(SUBSTRING([StartDATE],1,4) + "-" + SUBSTRING([StartDATE],5,2)
+ "-" + SUBSTRING([StartDATE],7,2))
Then configure Error Output on this Derived Column transformation to capture and handle conversion errors.
In SSIS, you can use data conversion transformation, the data type mapping is database timestamp with precisionin SSIS is for datetime2 in SQL Server.

What should be a default datetime value?

I am inserting Excel Sheet records in my DataTable in c# and passing this DataTable to an SQL stored procedure. In my c# code I have put certain checks for empty data cells of Excel sheet to avoid Exceptions. But It seems like I am missing something while giving a default value for my SQL Date field.
string InvoiceDate = (row.Cells[3].Text == " ") ? "0/00/0000 00:00:00 AM" : (row.Cells[3].Text);
And I get the following error:
String was not recognized as a valid DateTime.Couldn't store
<0/00/0000 00:00:00 AM> in InvoiceDate Column. Expected type is
DateTime.
Edited -
Declaration of SQL field [InvoiceDate]
[InvoiceDate] [date] NOT NULL
Please don't suggest inserting null as I cannot Insert null for this column.
First, There is no 00/00/0000 date, not in the real world and not in sql server.
Second, why do you even need a default values? just use null instead.
Third, use ISO 8601 format for specifying dates in strings (yyyy-mm-ddThh:mm:ss)
Forth, As Giorgi rightfully pointed out in his comment, why even use strings for a datetime value? use a variable of type DateTime in c#. note that it's min value is different then the sql server DateTime data type min value.
If your datetime column is not nullable, you can use 1753-01-01 (min value for datetime) or 9999-12-31 (max value for datetime)
One last thing, you might want to consider using either datetime2 or separate the date and time to different columns (data types date and time, of course). Why? read here.
Try to insert the current date instead:
string InvoiceDate = string.IsNullOrEmpty(row.Cells[3].Text) ? DateTime.Now.ToString() : (row.Cells[3].Text);

Date Conversion Issue MS Access to SQL Server

I'm creating a table B from an exisitng table A. In the table A I have a column ValDate which is varchar and contains Date. When I try to create the table B I have a function used in the query as given below and I get a conversion error. Table A contains null values as well.
MS Access:
((DateDiff("d",Date(),format(Replace(Replace([Table A].ValDate,".","/"),"00/00/0000","00:00:00"),"dd/mm/yyyy")))>0)).
Tables were in MS Access and are being migrated to SQL Server 2012.
SQL Server:
((DATEDIFF(day,FORMAT( GETDATE(), 'dd-MM-yyyy', 'en-US' ),FORMAT( ValDate, 'dd-MM-yyyy', 'en-US' ))>0))
or
((DateDiff(day,GETDATE(),Format(Replace(Replace([TableA].[ValidFrom],'.','/'),'00/00/0000','00:00:00'),'dd/mm/yyyy')))
I tried converting the date using several approachs like Convert , Format and Cast but I end up getting error as below.
Msg 8116, Level 16, State 1, Line 1
Argument data type date is invalid for argument 1 of isdate function.
I would really appreciate someone telling me what I'm missing here.
since you have date data in a string field is very likely you have some value that is not valid against your expected date format.
copy the data in a sql server table and then perform check and validation of the content of the string field.
have a look to the function try_convert that can be helpful when checking the content of the string field containing the date values.
when bad data is ruled out you can apply again your formula with (hopefully) a different result.
a better solution would be to create a separate field with appropriate datatype to store date values converted from the string field and apply your logic to that field.

Resources