ADF, data conversion in 'copy data' - azure-sql-database

I have data from JSON API that keep in the staging table before it move to destination table. Data type of destination columns are datetime and the source columns in the staging table are varchar(512). I got error when debugging because of datatype conversion between varchar (512) to datetime. This below message is the error message:
ErrorCode=TypeConversionFailure,Exception occurred when converting
value '2021-09-16T07:58:39.187+07:00' for column name 'start__date'
from type 'String' (precision:255, scale:255) to type 'DateTime'
(precision:23, scale:3). Additional info: String was not recognized as
a valid DateTime.
I tried in SSMS:
declare #This_time varchar(256) = '2021-09-16T07:58:39.187+07:00'
, #MyTime datetime
set #MyTime = Cast(#This_time as datetime)
select #MyTime
and the error message
Msg 241, Level 16, State 1, Line 7 Conversion failed when converting
date and/or time from character string.
Completion time: 2021-11-02T20:51:31.5345885+07:00
It looks completion time and value from staging table have same pattern. I tried once again with
datetime2 in SSMS and it works. This is the result 2021-09-16 07:58:39.187
I assume the data type is default but it can be changed. First attempt and error.
Then, I changed the format in the JSON Code became datetime2. Second attempt still no luck.
This is my destination table
how to solve this data conversion in ADF, copy data ?

The datatype that you are trying to cast is actually datetimeoffset, see the MSFT docs. See this fiddle for a working example.

Related

Msg 241, Level 16, State 1, Line 12 Conversion failed when converting date and/or time from character string

Fairly new to SQL Server and I'm struggling to understand why this date conversion doesn't work when I use it in a temp table. For some reason it works when i use it without the temp table.
The database contains dates in the format of YYYYMMDD.
[DOB] = CAST(LEFT(DC.DATE_BIRTH,8) AS DATE),
I get this error :
Conversion failed when converting date and/or time from character string.
Thanks!
This error is due to your data on DC.DATE_BIRTH table. check your date. I suppose that in some rows on this table exist the rows that when you trim it whith 8 characters, the remain part is not as a correct date format.

Convert('yymmdd' as datetime) is not working in sql server

I have this function in one of the SP it's working in one server but same conversion thouing error in other server .
Example:
Convert(datetime,'210319')
Error:
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
You need a four-digit year. How about adding one in?
Convert(datetime, '20' + '210319')

Convert varchar value to datetime in SQL Server 2012

In my import table I have the following column defined:
LFZ_begin VARCHAR(50) NULL
Now when I try to define the column in a view as DATETIME and call the view in SSMS, I get the following error message:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
In my view the column is defined as follows:
CONVERT(VARCHAR(50), CONVERT(DATETIME, LFZ_begin, 120)) AS LFZ_begin,
I need to expect the result value like that: 2020-09-04 00:00:0000
Does anyone have an idea for this issue?
You should fix your data model and never store date/time values as strings. In the meantime you can fix the view using try_ functions:
TRY_CONVERT(VARCHAR(50), TRY_CONVERT(DATETIME, LFZ_begin, 120)) AS LFZ_begin,
Note: This will eliminate the error on invalid formatted columns. Whether it ever returns a valid value depends on the data -- and you have shown no sample data so there is no way to suggest a solution to whatever the underlying problem is.

Cannot convert char to datetime in windows counters table

I have a table in SQL Server 2012 that is populated with Windows perfmon data using the built in Windows processes. The table is automatically created by this process. The problem is the timestamp field is a char but I need a datetime.
I use a view on generated tables to get the data into a usable form and I want to convert the timestamp into a datetime in the view. For some reason anything I try gives me this error:
Conversion failed when converting date and/or time from character string.
I can copy and paste a timestamp value from the table into a convert query and it works, like this:
SELECT convert(datetime, '2018-04-04 00:00:08.022', 121);
or
SELECT cast('2018-04-04 00:00:08.022' as datetime)
But when I try to convert a value directly from the table I get the error:
SELECT convert(datetime, counterDateTime, 121) from counterData
I have ruled out some strange format in row by selecting a specific row with a known correct format but I still get the same error.
What am I missing?
EDIT
Just to reiterate, all the values in the table are in the same format. The table was created automatically by the Windows process that writes perfmon data into the database. I have no control over the format of the data in the table. This is not specific to a row other than the one I am testing, this relates to all rows.
Example:
select counterDateTime from counterData where recordindex = '82331' and counterID = '1'
= 2018-04-04 00:00:08.022
select cast('2018-04-04 00:00:08.022' as datetime)
= 2018-04-04 00:00:08.023
select convert(datetime, '2018-04-04 00:00:08.022', 121)
= 2018-04-04 00:00:08.023
select cast(counterDateTime as datetime) from CounterData where recordIndex = '82331' and counterID = '1'
= Msg 241, Level 16, State 1, Line 109
Conversion failed when converting date and/or time from character string.
Here is an example tutorial for getting the Windows counter data into a database. It's a pretty standard process, there are many more examples online. The interesting tables are CounterData and CounterDetails which I aggregate with a view. It is this in the creation of this view that I would like to do the conversion.
https://logicalread.com/writing-performance-data-sql-server-mo01/#.WuxgzYgvyzU
The CounterDateTime column that I'm interested in is a nullable char of length 24.
I tested by following your instructions and was able to reproduce
SELECT ASCII(RIGHT(CounterDateTime, 1))
FROM dbo.CounterData
Returns 0 which is why I believe your data can't be converted. So basically
the last character is ASCII null.
Workaround is
SELECT CAST(LEFT(CounterDateTime, 23) AS DATETIME)
FROM dbo.CounterData

Showing this error "Conversion failed when converting date and/or time from character string." in SQL

My sp is
EXEC _REPORTS #StartDate=N'17-Sep-2016',
#EndDate=N'17-Sep-2016',
#CustomerID=0,
#ProductID=0,
#BranchID=0,
#NumberOfRecords=100,
#PageNo=1,
#IsBuy=1
here the region of date used in where
AND FCSB.FCBuySellDate
BETWEEN CONVERT(datetime,#StartDate,105) AND CONVERT(datetime,#EndDate,105)
The reason is primarily you are getting some wrong formatted data to convert, For example
declare #StartDate nvarchar(15) = N'17-Septt-2016'
select CONVERT(datetime,#StartDate,105)
You will get this error
Conversion failed when converting date and/or time from character string.
If you get correct date you get it converted, one way to ignore this error is to do TRY_CONVERT which willl return NULL. Best way to solve this problem is to identify why it is getting wrong date as parameter and fix that.
select TRY_CONVERT(datetime,#StartDate,105)
check for null value in FCSB.FCBuySellDate. It might be cause of an error
try this
AND FCSB.FCBuySellDate BETWEEN CONVERT(datetime,#StartDate,105) AND CONVERT(datetime,#EndDate,105)
AND isnull(FCSB.FCBuySellDate,'') <> ''
Also try this, convert column in datetime
AND CONVERT(datetime,FCSB.FCBuySellDate) BETWEEN CONVERT(datetime,#StartDate,105) AND CONVERT(datetime,#EndDate,105)