Solved

Convert mm/dd/yyyy nvarchar to SQL datetime

Posted on 2006-11-30
5
1,912 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
5 Comments
 
LVL 142

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 142

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now