I'm trying to convert datetime2 to datetime in order to create a standard between different sources using only SQL or SSIS Take the following SQL query as example:
SELECT CAST(offer_start_date AS datetime)
FROM [ODS].[macaclient_offers]
I get the following error: 'Explicit conversion from data type datetime2 to timestamp is not allowed.'
Furthermore, I did managed to convert datetime2 into date using a simple cast.
What is the right way to convert datetime2 to datetime using SQL Server 2008 or SSIS?
gilibi
Your sample select statement is trying to cast offer_start_date to timestamp not datetime.
If you do want a timestamp value from your datetime2 column you could the DatePart function to retrieve parts of the date and build it up yourself.
For example:
MSDN reference to DatePart function.
Not sure why you're getting that error, I've not had the same issue. Example below works fine in my 2008 Management Studio.
In my case the value was a
varchar
.If the value is a
varchar
ofdatetime2
, like'2018-10-24 12:06:29.6112233'
, then first cast it to adatetime2
and then to adatetime
:OK, I managed to accomplish that using a SSIS data conversion component. I found out that I can use either DT_DBTIME2 or DT_DBTIME
Thanks for everyone who helped
You are casting to
timestamp
in your code. Change todatetime
.