Cast using a variable ver.2

Dear Experts,
I'm trying to convert a varchar into a date or datetime (I tried both of them) using a variable but all the time I've got back an error
"Conversion failed when converting date and/or time from character string"

Open in new window

DECLARE @dateFormInvoice as nvarchar(10) = '16.04.2015'

PRINT CONVERT(datetime2, @dateFormInvoice);
PRINT CAST(@dateFormInvoice as datetime2);

Open in new window

Any idea how to solve that

Thank you
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:

DECLARE @dateFormInvoice as nvarchar(10) = '20150416'

Store it as YYYYMMDD in the database but display it any way you want in your screen.

This is language-neutral only for the data types DATE, DATETIME2, and DATETIMEOFFSET.
gokyo66Author Commented:
Point is that the @dateFromInvoice is a date that is read from an application and the format is that
gokyo66Author Commented:
Actually I don't have to store it. Let me explain better with the following code. Pay attention that the syntax is weird because is made in SAP

/*@dateFromInvoice store the date displayed in the Invoice document if you want to print the value of @dateFromInvoice you got back for instance today date 17.04.2015 (Switzerland format)*/
DECLARE @dateFromInvoice as nvarchar(10)= $[OINV.DocDate]

Base on terms of payment of one particular customer I've  to recalculate the due date and put the value and the field of the form Doc due date. Since for doing that recalculation I use the function datedd I need to convert @dateFromInvoice in a date format so I can use in the dateadd function
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Mike EghtebasDatabase and Application DeveloperCommented:
The following gives:
DECLARE @dateFormInvoice as nvarchar(10) = '16.04.2015'
Set @dateFormInvoice=substring(@dateFormInvoice,7,4) + 
                     substring(@dateFormInvoice,4,2) +

PRINT CONVERT(date, @dateFormInvoice);

Open in new window

But, it could be displayed in any other format you want.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulEE Topic AdvisorCommented:
use style number 104

 convert(varchar, your_data_here ,104)


DECLARE @dateFormInvoice as nvarchar(10) = '16.04.2015'
SELECT CONVERT(datetime2, @dateFormInvoice, 104);
Mike EghtebasDatabase and Application DeveloperCommented:
Using Paul's solution, if you don't want time portion, just use date instead of datatime2:

SELECT CONVERT(date, @dateFormInvoice, 104);
gokyo66Author Commented:
Thank you appreciate your support
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.