Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to convert a nvarchar(250) to datetime in a database.

Actually I have converted access database into sql in access they mention INV

Date datatype as text so it is converted into nvarchar(250) so that I need to

convert that in datetime.

It is saving in sql like this format 02-01-2015

I have written this code in sql

update soINVOICE set INV_DATE =convert(datetime,convert(datetime,INV_DATE ,(105)))

but I am getting this error all the time

The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.

So Please Resolve this error
Posted
Updated 2-Aug-15 19:43pm
v3

I'd do it in a high level language: C# or similar, and do the conversion there rather than in SQL - you are working with text based dates, and the most likely reason it's giving you problems is that the dates stored are not all valid - that's one of the reasons for always storing then as DATETIME values.

If you do this in a general purpose language, you get a lot better support for errors, and can log or manually correct invalid entries. It's a heck of a lot harder to do that in SQL!
 
Share this answer
 
SQL
update soINVOICE set INV_DATE =convert(datetime,convert(varcahr(16),INV_DATE ,(105)))
 
Share this answer
 
DateTime.TryParse[^] and DateTime.ParseExact[^] can help you resolve some of these errors at the front end.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900