473,224 Members | 1,619 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,224 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 4096
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
1,017 Expert 512MB
Try to do this

select Top 100 * from table order by DOB
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
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
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

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
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

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...
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=...
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...
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'...
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...
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...
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")...
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....
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...
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...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.