?
Solved

Convert mm/dd/yyyy nvarchar to SQL datetime

Posted on 2006-11-30
5
Medium Priority
?
1,933 Views
Last Modified: 2009-12-16
I imported dates in from Access into SQL Server and had to convert them to nvarchar (importing errors)...my dates all say (example) 11/24/1977, etc., but are in nvarchar format.  I'm trying CONVERT but get the arithmetic overflow error.  I'm assuming I need to format the nvarchar numbers and slashes and remove the slashes (?) and then convert?

Basically, how do I get the NVARCHAR 11/24/1977 to DATETIME format in SQL?

Thanks!
Kevin
0
Comment
Question by:Kevin Smith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18048394
convert(datetime, yourfield, 101)
0
 

Author Comment

by:Kevin Smith
ID: 18048395
I tried that but get the overflow error
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 18048406
the you have varchar values in there that are outside the acceptable date ranges.
you will have to check out which rows give problems.
0
 
LVL 1

Expert Comment

by:weiroblpay
ID: 18049264
SQL Server will implicitly convert nvarchar values into DateTime values if the values are allowed.
This then will work where MyDate is a DateTime field and myDateInnVarchar is a nvarchar field with values like 11/24/1977

UPDATE TestTable
SET MyDate =  MyDateInnVarchar
WHERE myTest >= 1

Unfortunately, as angelIII said, you must have nvarchar values that are not valid or are outside of the acceptable date ranges.
For example:  11/24/197  will give you the overflow error.  
Check your values for dates that are not valid. SQL server only allows dates as follows:

Date and time data from January 1, 1753, through December 31, 9999, with an accuracy of three-hundredths of a second, or 3.33 milliseconds.

good luck
0
 
LVL 35

Expert Comment

by:David Todd
ID: 18049594
Hi,

To find the problem rows do this

select *
from TestTable
where isdate( TestTable.yourfield ) = 0 -- 0 is false, 1 is true.

You could even put the isdate in the where clause of a subquery, and only update the ones that will convert to date.

Regards
  David
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question