Solved

convert varchar -> datetime with <nulls> and dd/mm/yyyy 00:00:00 in field [out-of-range error]

Posted on 2004-10-01
17
344 Views
Last Modified: 2008-03-04
Hi,

I have 40,000 records which I want to to convert the date of birth field.

the field has about 50% of <NULL> and the rest have dates.

The dates are in the format for example 04/05/1977 0:00:00

I can remove the 0:00:00 because it is not relevant. (Is there an SQL command I can use to remove all of these?)

Is the NULL or the time causing the out-of-range error and is there any way around this?

I need to convert it to add more date records and dates not varchars in the future.

Regards,
tp
0
Comment
Question by:tippingpoint
  • 8
  • 8
17 Comments
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12205971
If you want to strip the 0:00:00 out of an existing varchar column this will do it:

UPDATE TableA SET ColA = Replace(ColA, ' 0:00:00','')

If you have date values in a varchar column and convert the column to a datetime data type, it will be stored as a number. If no time is specified the number will always be equal to 12 AM of the date.

If you want us to help you figure out what's causing the error, you need to post the query/command you're executing to generate the error.
0
 
LVL 34

Expert Comment

by:arbert
ID: 12205983
You need to look at the nulls before you attemp the conversion.  If your varchar "date" is always in MM/DD/YYYY then this will select the dates using left (you can build an update statement out of it as well):


SELECT CASE WHEN yourdobfield IS NULL THEN NULL
       ELSE CONVERT(DATETIME,LEFT(yourdobfield,10)) END
FROM yourinputtable
0
 

Author Comment

by:tippingpoint
ID: 12206159
arbert:
This is the error I got.

Server: Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:tippingpoint
ID: 12206177
jdlambert1:
I have removed the times but it seems the <NULLS> in that column are not allowing me to convert it to datetime

Any other suggestions?
0
 
LVL 34

Expert Comment

by:arbert
ID: 12206178
So I take it you must also have some invalid dates in the column???

select
case when isdate(left(yourdobfield,10)) then CONVERT(DATETIME,LEFT(yourdobfield,10))
else null END
from yourinputtable
0
 
LVL 34

Expert Comment

by:arbert
ID: 12206182
Actually, sorry I didn't ask this earlier, what's the existing format of the field that contains DOB?  If there happen to be spaces at the beginning of the string, the would need to be stripped off with LTRIM.
0
 

Author Comment

by:tippingpoint
ID: 12206183
arbert:
Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'then'.

still no good, do you think there are bad entries in there?
0
 

Author Comment

by:tippingpoint
ID: 12206192
it was a varchar,

I have removed spaces
0
 
LVL 34

Expert Comment

by:arbert
ID: 12206194
Sorry--I tested the first one and changed it without testing the second:

select case isdate(dobfield)
        when 1 then  CONVERT(DATETIME,LEFT(dobfield,10))
            else null END
from yourtable
0
 

Author Comment

by:tippingpoint
ID: 12206197
If there are invalid date entries in there is there a way to check if they are invalid and make them null??

0
 
LVL 34

Expert Comment

by:arbert
ID: 12206201
That's what the above does--if ISDATE is 0 then the date is invalid and it will return NULL.  If it's 1, it performs the conversion.
0
 

Author Comment

by:tippingpoint
ID: 12206209
It seems as though the above works, but it is converting my dates wrong using LEFT

my format is DD/MM/YYYY

it is being converted to YYYY-MM-DD which is ok but it is swicthing the DD and MM around in the output

0
 
LVL 34

Expert Comment

by:arbert
ID: 12206220
Sorry--dumb american here :)

add a 103 to the convert:

select case isdate(dob)
when 1 then  CONVERT(DATETIME,LEFT(dob,10),103)
else null END
from #datetest


The YYYY-MM-DD returned is just the iso representation of the date....
0
 

Author Comment

by:tippingpoint
ID: 12206233
ok all good!! :)

last thing..how do i update into that data table with that query... sorry i fairly new to sql!
0
 
LVL 34

Accepted Solution

by:
arbert earned 500 total points
ID: 12206250
Well, I would either add a new column with a smalldate time definition, or you could update the existing data in-place and then change the column definition afterwards.

Update existing:

update your table
set dob= case isdate(dob)
when 1 then  CONVERT(DATETIME,LEFT(dob,10),103)
else null END


If you add a new column, just substitute the "set dob=" to be "set younewcolumn="

Brett


0
 
LVL 34

Expert Comment

by:arbert
ID: 12206296
Did you get it tp?
0
 

Author Comment

by:tippingpoint
ID: 12206347
VERY GOOD..

THANK YOU SO MUCH.

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Update trigger 5 18
sql server tables from access 18 19
Sql server get data from a usp to use in a usp 5 16
sql server query 6 9
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

773 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