Solved

Convert mm/dd/yyyy nvarchar to SQL datetime

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

810 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