473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Datetime conversion problem

I'm having a problem with converting a DOB column from nvarchar to a datetime format. I thought the easiest way to do this would be to create another column in the table "DOB2" and run [update table set DOB2=Convert(datetime, DOB)]. This fails with the following message:

Msg 241, Level 16, State 1, Line 1

Conversion failed when converting datetime from character string.

This is SQL Server 2005, running on a Windows 2003 server. Any help, suggestions, or better ways to do this would be greatly appreciated.
Jan 4 '07 #1
8 4099
iburyak
1,017 Expert 512MB
It looks like you have some garbage data.
Select all from your table and order by DOB you might be able to visualize and correct it.
Jan 4 '07 #2
Thanks iburyak. I should have mentioned that this table has over 250 million records so I probably won't be able to select all and figure out if there are problems in the DOB column, right?
Jan 4 '07 #3
iburyak
1,017 Expert 512MB
Try to do this

select Top 100 * from table order by DOB
or
select Top 100 * from table order by DOB desc

This way you would be able to see something abnormal - it is usually at the end or at the beginning of your table and if you have index over this column it should be no problem.
Jan 4 '07 #4
iburyak
1,017 Expert 512MB
You can try this:

[PHP]--List all valid date characters in brackets separated by comma.

select Top 100 * from table_name where DOB not like '%[0-9,-,\,:]%'[/PHP]
Jan 4 '07 #5
OK, I ran your second option:
[select Top 100 *
from criminal
where DOB not like '%[0-9,-,\,:]%']

And it returned the first 100 rows but the DOB column is blank, which is understandable, some DOB's aren't given. Should these be Null? and if so how do I update or correct them?

Thank you for the help.
Jan 5 '07 #6
iburyak
1,017 Expert 512MB
Good that it helped :) because after I had a second look at the query I wasn't sure if it will work. The thing is this query works in cases where you don't have any numbers or -,\,: but in cases where you have mixed data like “aaaaaa56:00” it wouldn't work. But still try to fix what you see first.

1. Count how many records like this you have in your table.
[PHP]select count(*)
from criminal
where DOB not like '%[0-9,-,\,:]%'[/PHP]

2. Check if they are the problem records by trying to convert them to datetime because I suspect server doesn’t have a problem converting null or empty string to a date. It would be valid date 1900-01-01 00:00:00.000. You have to search for something else in your data that is totally unrecognizable. To test if conversion go smoothly for records like this do following
[PHP]select top 100 convert(datetime, DOB)
from criminal
where DOB not like '%[0-9,-,\,:]%'[/PHP]

3. Try to search for letters in this column.
[PHP]select Top 100 *
from criminal
where DOB like '%[A-Z,a-z]%'[/PHP]

4. Try to do following search. Not sure how will it work on your server it different from version to version.

[PHP]select top 100 *
from criminal
where DOB not like '[^0-9,-,\,:]%' and isnull(DOB,’’) <> ‘’[/PHP]

^ sign means negative, meaning everything that is not. Also I removed nulls and empty strings because we already know they are possibly there and want to see the rest.
Good luck.
Jan 5 '07 #7
iburyak,

I just wanted to say thanks. I finally got a chance to use what you provided and although this was a long process, this was exactly what I needed. Thanks for all of the help and direction, I appreciate it.
Jan 12 '07 #8
iburyak
1,017 Expert 512MB
You are welcome... :)
Jan 12 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Symon R | last post by:
This is a bit of a weird one that I haven't yet been able to solve - I'm hoping someone out there can disprove my findings and tell me where I've gone wrong! I have designed a web service that...
2
by: Andre | last post by:
Hi, I have a problem with my Date conversion and can't find a way to resolve it. Here's my code : friday= DateTime.Now.AddDays((day - (day * 2 + 2))) thursday= friday.AddDays(6) friday=...
14
by: Bob Day | last post by:
using VS 2003, VB.net... Why is this valid: dim x as datetime = nothing but this is invalid: public sub DoSomething (Optional byval x as datetime = nothing) ....code to do something end...
26
by: Reny J Joseph Thuthikattu | last post by:
Hi, I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a miniute to it.How can i do that? by manipulation i want to make '01-JUNE-2004 11:59 PM' to '02-JUNE-2004 12:00 AM'...
11
by: Cor Ligthert | last post by:
Hello everybody, Jay and Herfried are telling me every time when I use CDate that using the datetime.parseexact is always the best way to do String to datetime conversions. They don't tell why...
9
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope...
11
by: Tim | last post by:
Hi, I am trying to do something simple. Convert a string date to datetime but it is not working and is giving me a baffling error! System.Convert.ToDateTime("Jan 30, 2006")...
2
by: Mike | last post by:
Hi, I have DateTime filed in db table. I'd like to search my db based on DateTime filed, actually to list all rows with particular date, for e.g. 16.11.2007... I send string to method for....
11
by: =?Utf-8?B?UGFvbG8=?= | last post by:
I have a SQL database table with rows in which the primary key is a DateTime type (although I don't use the Time part). Have added numerous rows and now want to delete one so I enter the date...
1
Manikgisl
by: Manikgisl | last post by:
But the problem is we have dates in Varchar instead Datetime While Converting Varchar To Datetime All four formats are unable to Convert ie select Convert(Datetime,'18-11-2008 2:35:19...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.