Solved

Convert mm/dd/yyyy nvarchar to SQL datetime

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

724 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