Solved

sql - nvarchar to datetime

Posted on 2011-03-14
11
2,485 Views
Last Modified: 2012-05-11
I am trying to cast from nvarchar[255] to datetime as follows:

UPDATE db_table
SET myDateTime = cast([nvarchar255DateTime] as datetime)

Getting the following SQL scripting error:

Msg 242, Level 16, State 3, Line 3
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.


sample data for  nvarchar[255]DateTime:    01/10/2010
0
Comment
Question by:amillyard
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 10

Expert Comment

by:Mez4343
ID: 35133455
I would use DatePart function and break out each part to set a DateTime column.

UPDATE db_table
SET myDateTime = CAST(DATEPART(yyyy, nvarchar255DateTime) AS varchar) + '-' +
                     CAST(DATEPART(mm, nvarchar255DateTime) AS varchar) + '-' +
       CAST(DATEPART(dd, nvarchar255DateTime) AS varchar)

0
 
LVL 26

Expert Comment

by:tigin44
ID: 35133457
use this with the appropriate format parameter..

UPDATE db_table
SET myDateTime =  CONVERT(datetime, nvarchar255DateTime, 101)
0
 

Author Comment

by:amillyard
ID: 35133500
Mez4343:  thank you for the feedback - at the moment I am getting the following sql scripting error:

Msg 241, Level 16, State 1, Line 3
Conversion failed when converting date and/or time from character string.
0
 

Author Comment

by:amillyard
ID: 35133507
tigin44:  getting the following sql scripting error:

Msg 242, Level 16, State 3, Line 1
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
0
 
LVL 26

Expert Comment

by:tigin44
ID: 35133575
so you have values having irregular formats... check the values and correct or discard the irregular formated values...
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 40

Expert Comment

by:Sharath
ID: 35133788
Run this and check the non-dates in your table.
select myDateTime from db_table where ISDATE(myDateTime) = 0

Open in new window

0
 

Author Comment

by:amillyard
ID: 35133853
Sharath_123:  out of 350000 records -- the above script returned 130000

What does this mean please?
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35133876
That means those are not proper dates. Post some of those non-dates.
0
 

Author Comment

by:amillyard
ID: 35133887
Sharath_123:

29/10/2010
15/11/2010
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 35134067
I don't understand what you want to update here. If the underlying data type of myDateTime is varchar, you cannot change the type with an UPDATE statemen. Instead, you can alter the column data type.
But before altering it to datetime, you can change the data to proper date format. Can you try this UPDATE statement?
UPDATE db_table 
   SET myDateTime = CASE 
                      WHEN ISDATE(date_col) = 0 THEN CONVERT(DATETIME,SUBSTRING(myDateTime,7,4) + SUBSTRING(myDateTime,4,2) + LEFT(myDateTime,2))
                      ELSE CONVERT(DATETIME,myDateTime) 
                    END

Open in new window

0
 

Author Closing Comment

by:amillyard
ID: 35134097
Sharath_123:  works perfectly now :-)  thank you.
0

Featured Post

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Updating ms sql with special characters 8 39
Square brackets 4 38
Benefits of SMB Fileshare 3 63
How to use odbc in vb to connect to ms sql 14 37
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

22 Experts available now in Live!

Get 1:1 Help Now