Solved

Convert mm/dd/yyyy nvarchar to SQL datetime

Posted on 2006-11-30
5
1,927 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 500 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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 shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

749 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