Showing posts with label datetime. Show all posts
Showing posts with label datetime. Show all posts

Sunday, March 11, 2012

Error converting to DateTime/SmallDateTime

I am trying to add some datetime values into a table. However, the database keeps throwing the following error "The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value."

The code is as follows:

string start = dateCal.SelectedDate.ToString().Substring(0, 10) + " " + startTB.Text;

string end = dateCal.SelectedDate.ToString().Substring(0, 10) + " " + endTB.Text;

//DateTime starts = Convert.ToDateTime(start);

//DateTime ends = Convert.ToDateTime(end);

sqlInsert.CommandText = "INSERT INTO aspnet_reportdate VALUES ('" + refno + "', '" + start + "', '" + end + "')";

where startTB and endTB contains a time in valid format (HH:MM)

The same error would occur even if I converted the string to a DateTime object before I try to insert it into the database.

a sample value of start would be "6/19/2007 10:00"

What puzzles me is that only the error is only thrown by the database (SQL Server Express) but not C#

I have tried using both datetime and smalldatetime for the columns in question in the database also.

There is no problem with your SQL Server or C#. The problem is on your input data (mismatched date format).

I strongly recommand to change your code as follow as. It is really very dangerous code you are writing (SQL Injuction).

Don't use Dynamic Query generation on the UI. Beaware of SQL Injunction. (use parameters or Stored Procs)

The following code will work for you, (the sql server parse the param before executing it, so you can easily identify the problem)

Code Snippet

sqlInsert.CommandText = "INSERT INTO aspnet_reportdate VALUES (@.refno, @.start, @.end)";

sqlInsert.Parameters.Add(new SqlParameter("@.refno", DbType.String));

sqlInsert.Parameters.Add(new SqlParameter("@.start", DbType.DateTime));

sqlInsert.Parameters.Add(new SqlParameter("@.end", DbType.DateTime));

sqlInsert.Parameters[0].Value = refno;

sqlInsert.Parameters[1].Value = starts;

sqlInsert.Parameters[2].Value = ends;

sqlInsert.ExecuteNonQuery();

|||

Just to add to what Mani has said, if you do this kind of thing, the best way to get help is to add a print statement, messagebox, etc and print out the SQL statement that you are trying to execute. You can't use profiler with express (at least not with a license to the real tools), but if you can, using profiler you can see the statement that you are trying to execute. I would strongly consider purchasing a developer license to get the tools to work with. Like Mani says, use a parameterized statement in all cases possible, but if you are trying to build a reproducible script, that might not be possible.

The first most important step in a process like this is to figure out what you are trying to execute and take that statement to Management Studio (Express will do for this) and work out what the issue is in the query.

Date values should ideally use the standard formats. Look up "datetime data type, formats" in the index, and check out the ODBC timestamp format. It will always work.

|||

thx all for the help

just a further question. what is sql injunction and what problems does it bring about?

couldn't seem to find anything substantial on it

|||

Louis Davidson wrote:

Just to add to what Mani has said, if you do this kind of thing, the best way to get help is to add a print statement, messagebox, etc and print out the SQL statement that you are trying to execute. You can't use profiler with express (at least not with a license to the real tools), but if you can, using profiler you can see the statement that you are trying to execute. I would strongly consider purchasing a developer license to get the tools to work with. Like Mani says, use a parameterized statement in all cases possible, but if you are trying to build a reproducible script, that might not be possible.

will bear this in mind

most of what i do now is more for interest or sch work, so my choice of tools are more restricted.

error converting date time

Hi i m tring to convert a date time

declare @.a datetime
declare @.b varchar(10)
set @.b='26/04/2004'
set @.a= Convert(datetime, @.b)

but it gives me this error:

Server: Msg 242, Level 16, State 3, Line 5
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

I believe is my date format..i want to know how to make sure that i am the dd/mm/yyyy format is correct way?Why are you sending the date in as a varchar ?|||Use SET DATEFORMAT

Sets the order of the dateparts (month/day/year) for entering datetime or smalldatetime data.|||Hi,
thanks for all ur reply.. basically i wanted to send date in as dateTime but i cant because i cant assign a date to null so i have to send it to my database and from there i convert it to valid date time.

Thanks alot.

Error converting data type varchar to datetime.

Hi,

It is not exactly what I stated in the subject - It's an outcome - exception thrown while executing non-query command.

I get this exception when I try to execute my stored procedure that takes datetime as one of its parameters.

I am using dataset designer to create table adapters and build queries. Then I simply use objectdatasource component that uses one of the table adapters and bind it to for example a detailsview control.

When I run this in debug mode and trace the objects everything looks perfect including these datetime parameters. It is sql server that throws the exception. I ran the sql profiler to see what exactly is going on, and I captured the command that is sent by ADO - it's broken into several lines right in the middle of my datetime parameters... this is the source of the problem. Everything is working fine when I take this command and execute it as a single line in the sql management studio.

Is there anything about ADO that I do not know?

Is the error only with that particular SQL statement or anyother query with date? What datatype you are passing to the stored procedure? DateTime or varchar?

Thanks

|||

Are you passing date as string ? Are you sure that you date format is the same as default date format on SQL server?

If your date is not in valid format for SQL you can have this kind of problems.

Thanks

Error converting data type varchar to datetime.

Here is the stored procedure
the data type for the column b_trn_tentdte is decimal(8,0).

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

ALTER Procedure Prc_New_cus_wo_prev_posted
@.b_trn_tendte_Beg_Date datetime
,@.b_trn_tendte_End_Date datetime
@.b_trn_tendte_Beg_Date1 datetime
,@.b_trn_tendte_End_Date2 datetime

as

declare @.Beg_tentdte datetime
declare @.End_tentdte datetime
declare @.Beg_tentdte1 datetime
declare @.Beg_tentdte2 datetime

set @.Beg_tentdte = convert(varchar(10), @.b_trn_tendte_Beg_Date, 112)
set @.End_tentdte = convert(varchar(10), @.b_trn_tendte_End_Date, 112)
set @.Beg_tentdte1 = convert(varchar(10), @.b_trn_tendte_Beg_Date1, 112)
set @.Beg_tentdte2 = convert(varchar(10), @.b_trn_tendte_End_Date2, 112)

/* this query looks for customers with posted
transactions but none prior to the date range
in question */

Select distinct b_cus_cname
from
bar_cus_db_rec
,bar_trn_db_rec b
where b.b_trn_instid = ''
and b.b_trn_instid = b_cus_instid
and b.b_trn_actid = b_cus_cusid
and convert(varchar(10), b.b_trn_tentdte) between @.Beg_tentdte and @.End_tentdte
and not exists (select c.b_trn_actid
from bar_trn_db_rec c
where c.b_trn_instid = b.b_trn_instid
--and c.b_trn_tentdte between 19970901 and 20030229
and convert(varchar(10), c.b_trn_tentdte) between @.Beg_tentdte1 and @.Beg_tentdte2
and c.b_trn_actid = b.b_trn_actid)

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

every time I run the query, I get this error message:
Error converting data type varchar to datetime.

thanks for your assistance.I am wondering if you are doing any data validation on your paramaters in you application before you call your sp.

If isDate(parameter) Then
'call your sp
Else
'prompt user for input
End if|||Disregard previous.

you are coverting your parameters to char and trying to assign them to a dattime variable.

set @.Beg_tentdte = convert(varchar(10), @.b_trn_tendte_Beg_Date, 112)
set @.End_tentdte = convert(varchar(10), @.b_trn_tendte_End_Date, 112)
set @.Beg_tentdte1 = convert(varchar(10), @.b_trn_tendte_Beg_Date1, 112)
set @.Beg_tentdte2 = convert(varchar(10), @.b_trn_tendte_End_Date2, 112)

remove your converts|||SQL Server will implicitly convert between character strings and datetime values if the strings are in the proper format. But in your case you are starting with a numeric value.
A numeric value such as 19970901 cannot be implicitly converted to a datetime datatype, even after casting as a character string. You will need to explicitly convert to datetime, like this:
SELECT CONVERT(datetime, CONVERT(varchar(10), c.b_trn_tentdte))

...and for good measure you should probably be more specific about the format, like this:
SELECT CONVERT(datetime, CONVERT(varchar(10), c.b_trn_tentdte), 112)|||...and if you stick around long enough, you "may" become a "Format Man"!|||sir! cannonball over the fore end sir.

Error converting data type varchar to datetime

An application i am using creates an error.

Using SQL-profiler i catch this statement:

exec sp_executesql N'insert into RECORDING_SCHEDULE (programme_oid, capture_source_oid, filename, status, recording_group, recording_type, manual_channel_oid,
manual_start_time, manual_end_time, quality_level, pre_pad_minutes, post_pad_minutes) values (@.P1,@.P2,@.P3,@.P4,@.P5,@.P6,@.P7,@.P8,@.P9,@.P10,@.P11,@.P1 2)',N'@.P1 int,@.P2 int,@.P3
nvarchar(19),@.P4 int,@.P5 int,@.P6 int,@.P7 int,@.P8 datetime,@.P9 datetime,@.P10 int,@.P11 int,@.P12 int',177748,2,N'Lilla rda traktorn',4,40,5,16,''2006-06-21
17:00:00:000'',''2006-06-21 20:00:00:000'',1,1,2

After removing the double ' around the date and time parts i get the following error when running in QA:
Msg 8114, Level 16, State 5, Line 0
Error converting data type varchar to datetime.

If a remove the time portion it works.

The DDL for the table is:

CREATE TABLE [dbo].[RECORDING_SCHEDULE](
[oid] [int] IDENTITY(1,1) NOT NULL,
[programme_oid] [int] NULL,
[capture_source_oid] [int] NULL,
[status] [smallint] NOT NULL,
[filename] [varchar](255) NULL,
[recording_type] [int] NULL,
[recording_group] [int] NULL,
[manual_start_time] [datetime] NULL,
[manual_end_time] [datetime] NULL,
[manual_channel_oid] [int] NULL,
[quality_level] [int] NULL CONSTRAINT [DF__RECORDING__quali__38996AB5] DEFAULT ((0)),
[pre_pad_minutes] [int] NULL CONSTRAINT [DF__RECORDING__pre_p__398D8EEE] DEFAULT ((0)),
[post_pad_minutes] [int] NULL CONSTRAINT [DF__RECORDING__post___3A81B327] DEFAULT ((0)),
CONSTRAINT [aaaaaRECORDING_SCHEDULE_PK] PRIMARY KEY NONCLUSTERED

What am I missing?Well, I corrected a type where you listed [@.P1 2] rather than the obviously intended [@.P12], but after that this code worked fine for me:CREATE TABLE [dbo].[RECORDING_SCHEDULETMP](
[oid] [int] IDENTITY(1,1) NOT NULL,
[programme_oid] [int] NULL,
[capture_source_oid] [int] NULL,
[status] [smallint] NOT NULL,
[filename] [varchar](255) NULL,
[recording_type] [int] NULL,
[recording_group] [int] NULL,
[manual_start_time] [datetime] NULL,
[manual_end_time] [datetime] NULL,
[manual_channel_oid] [int] NULL,
[quality_level] [int] NULL,
[pre_pad_minutes] [int] NULL,
[post_pad_minutes] [int] NULL,
)

exec sp_executesql N'
insert into RECORDING_SCHEDULETMP
(programme_oid,
capture_source_oid,
filename,
status,
recording_group,
recording_type,
manual_channel_oid,
manual_start_time,
manual_end_time,
quality_level,
pre_pad_minutes,
post_pad_minutes)
values (@.P1,
@.P2,
@.P3,
@.P4,
@.P5,
@.P6,
@.P7,
@.P8,
@.P9,
@.P10,
@.P11,
@.P12)',
N'@.P1 int,
@.P2 int,
@.P3 nvarchar(19),
@.P4 int,
@.P5 int,
@.P6 int,
@.P7 int,
@.P8 datetime,
@.P9 datetime,
@.P10 int,
@.P11 int,
@.P12 int',
177748,
2,
N'Lilla rda traktorn',
4,
40,
5,
16,
'2006-06-21 17:00:00:000',
'2006-06-21 20:00:00:000',
1,
1,
2

drop table RECORDING_SCHEDULETMPTry cutting and pasting this to QA and see if it runs for you.|||Oh, I think I see the issue. You "removed" the double quotes, when you should have just changed them to single quotes, as in my sample code...

Error converting data type DBTYPE_DBTIMESTAMP to datetime

I'm getting the error Error converting data type DBTYPE_DBTIMESTAMP to datetime when I try to import a date field from Oracle to SQL Server.

SELECT CAST(cancel_dt as datetime) FROM OPENQUERY(orcldb, 'SELECT cancel_dt FROM tablename WHERE id= 12345')

The date in Oracle is 19-JUN-99, but it's coming over as 06/19/0999 and it throws the error because the year 999 is invalid in SQL Server. Any ideas on why Oracle or SQL server would convert the year to 0999 instead of 1999?
This is also happening on a value 24-SEP-07 coming over as 09/24/0207

Most other date values work, so I'd really like to get in depth into the process that occurs when converting to a SQL Server datetime.Why not use the conversion function in Oracle to produce a 4 digit year?|||I found the problem to be that SQL Server only accepts dates from January 1, 1753 through December 31, 9999 whereas Oracle allows dates January 1, 4712 BC to December 31, 9999 AD.

So these dates that are coming over with years of 999 or 207 produce an error because they are invaild in SQL Server.|||That's correct...

M$ figured that because the calendar was adjusted in that century..(they lost several days) that the date function would produce invalid results...think DATEDIFF, DATEADD, ect

lunacy

anyway...your dates are not 0999

Oracle has the ability to produce the dates you need. The date formatting function in Oracle are far superior to SQL Server...

Just return that date as a varchar in your select, then convert it...|||So what is the actual 4-digit year representation for 19-JUN-99? Is it 06/19/0999 or 06/19/1999?
And what about 24-SEP-07? 09/24/0207 or 09/24/2007?

If the latter is the answer to both then it's not SQL Server, it's how you bring the data in and how much guessing you want SQL Server to do. You also need to check "two digit year cutoff option".|||MY problem with the dates is that 19-JUN-99 is supposed to be 1999, but somehow this date was loaded as June 19, 999 instead. So it is coming over correctly. There's just some data cleaning to be done.

This is what happens when you use someone elses data :mad:|||These 2 calls produce the right result:

select cast('24-SEP-07' as datetime) --09/24/2007
select cast('19-JUN-99' as datetime) --06/19/1999|||Check how you set up the linked server to Horacle. See if there is any datetime/currency setting that can affect this.

Error converting data type DBTYPE_DBTIMESTAMP to datetime

Hi

I am trying to transfer data from Access 2000 database to SQL server 2005 via SSIS. My access database table has a field with data type Date/Time and SQL server table has a field with data type datetime. When I am running the SSIS package, it is throwing the following error and stops execution.

[SQL Server Destination [12466]] Error: An OLE DB error has occurred. Error code: 0x80040E07. An OLE DB record is available. Source: "Microsoft OLE DB Provider for SQL Server" Hresult: 0x80040E07 Description: "Error converting data type DBTYPE_DBTIMESTAMP to datetime.".

Please help.

Thanks in advance,
Ramzee

According to BOL (http://msdn2.microsoft.com/en-us/library/ms175970.aspx) the types can be converted, so that should work. Have you identified the values that fail? Using the error output is a very easy way to capture this data. My best guess for now is the values are out of range for the SQL datetime type. A derived column transformation could be used to massage these values depending on requirements.|||

Thanks Darren!

I used the script component to identify the wrong date entries. Now it works!

Thanks once again.

|||

I am having the same error. A simple select from Query Analyzer

Select * from HCPROD..HC.UNIT U

Even if I specifically reference a number column I get the same error

Select BLOCK_ID from HCPROD..HC.UNIT U

The way I see it is that there is a column, probably the DATE_MODIFIED column, somewhere in the database that has a date & time value that the driver cannot process. Why the error occurs even if I do not reference the column is a mystery.

I am using Microsoft OLE DB Provider for Oracle

Note that I have insured that every date field in the table is in 20th or 21st century

|||Some more information. We are using Oracle version 10g. I wonder if there is an updated driver from Microsoft? We searched and could not find one ourselves.|||SELECT TOP 10 * FROM [ORACLE_LOOKUP]..[SchemaName].[TableName] AlaisName

Error converting data type DBTYPE_DBTIMESTAMP to datetime

SELECT * FROM
OPENQUERY (
ORACLE_LOOKUP,
'SELECT TO_CHAR(DateColumnName) FROM TableName')

The following workaround was foud on the internet

A workaround would be to use the OPENQUERY function in SQL Server. This
function allows you to execute a pass-through query on the linked server.
By issuing a pass-through query, you can then take advantage of Oracle's
built-in functions to convert the date to a character data type or NULL.
Examples of both are as follows:

In the following example, the column "hiredate" is converted to a string in
the format of MM-DD-YYYY.

SELECT *
FROM OPENQUERY(, 'SELECT empno,
TO_CHAR(hiredate,''MM-DD-YYYY'') FROM scott.emp')

In this example we use a combination of two Oracle built-in functions,
DECODE and GREATEST to convert any hiredate that is earlier than 01/01/1753
(SQL Server's lower bound) to NULL.

SELECT *
FROM OPENQUERY(,'SELECT DECODE (hiredate,
GREATEST(hiredate, TO_DATE(''01/01/1753'',''MM/DD/YYYY'')), hiredate, NULL)
FROM scott.emp')

In case you are not familiar with Oracle built-ins, here is a description of
the two used in the query above.

DECODE is similar to a case statement in SQL Server. Its syntax is as
follows :

DECODE(, , , , ,...,
)

The is compared to each value. If there is a match,
the corresponding is returned. Otherwise, the value is
returned.

GREATEST returns the greatest value in the list of expressions.

GREATEST(, , ...)
Reply With Quote

Friday, March 9, 2012

Error converting data type DBTYPE_DBTIMESTAMP to datetime

Hi

I am trying to migrate data from oracle to sql server 2005 in sql server management studio express, most of my data in oracle have been successfully moved into sql server 2005 by using storage procedure. But some of data can't move across becasue i got this error:

Error converting data type DBTYPE_DBTIMESTAMP to datetime

Does anyone know why? Please help?

Thanks

Li

There is "invalid" datetime data in one of the columns, thi could be based on a out-of-range value or another date representation which does not fit the destination one. If you cannot identitfy the values which cannot be imported, try to import the data to SQL Server using a VARCHAR or CHAR destination data type and find out which values are malicious using the ISDATE Function.

HTH, jens K. Suessmeyer.


http://www.sqlserver2005.de

Error converting data type DBTYPE_DBTIMESTAMP to datetime

Hi

I am trying to transfer data from Access 2000 database to SQL server 2005 via SSIS. My access database table has a field with data type Date/Time and SQL server table has a field with data type datetime. When I am running the SSIS package, it is throwing the following error and stops execution.

[SQL Server Destination [12466]] Error: An OLE DB error has occurred. Error code: 0x80040E07. An OLE DB record is available. Source: "Microsoft OLE DB Provider for SQL Server" Hresult: 0x80040E07 Description: "Error converting data type DBTYPE_DBTIMESTAMP to datetime.".

Please help.

Thanks in advance,
Ramzee

According to BOL (http://msdn2.microsoft.com/en-us/library/ms175970.aspx) the types can be converted, so that should work. Have you identified the values that fail? Using the error output is a very easy way to capture this data. My best guess for now is the values are out of range for the SQL datetime type. A derived column transformation could be used to massage these values depending on requirements.|||

Thanks Darren!

I used the script component to identify the wrong date entries. Now it works!

Thanks once again.

|||

I am having the same error. A simple select from Query Analyzer

Select * from HCPROD..HC.UNIT U

Even if I specifically reference a number column I get the same error

Select BLOCK_ID from HCPROD..HC.UNIT U

The way I see it is that there is a column, probably the DATE_MODIFIED column, somewhere in the database that has a date & time value that the driver cannot process. Why the error occurs even if I do not reference the column is a mystery.

I am using Microsoft OLE DB Provider for Oracle

Note that I have insured that every date field in the table is in 20th or 21st century

|||Some more information. We are using Oracle version 10g. I wonder if there is an updated driver from Microsoft? We searched and could not find one ourselves.|||SELECT TOP 10 * FROM [ORACLE_LOOKUP]..[SchemaName].[TableName] AlaisName

Error converting data type DBTYPE_DBTIMESTAMP to datetime

SELECT * FROM
OPENQUERY (
ORACLE_LOOKUP,
'SELECT TO_CHAR(DateColumnName) FROM TableName')

The following workaround was foud on the internet

A workaround would be to use the OPENQUERY function in SQL Server. This
function allows you to execute a pass-through query on the linked server.
By issuing a pass-through query, you can then take advantage of Oracle's
built-in functions to convert the date to a character data type or NULL.
Examples of both are as follows:

In the following example, the column "hiredate" is converted to a string in
the format of MM-DD-YYYY.

SELECT *
FROM OPENQUERY(, 'SELECT empno,
TO_CHAR(hiredate,''MM-DD-YYYY'') FROM scott.emp')

In this example we use a combination of two Oracle built-in functions,
DECODE and GREATEST to convert any hiredate that is earlier than 01/01/1753
(SQL Server's lower bound) to NULL.

SELECT *
FROM OPENQUERY(,'SELECT DECODE (hiredate,
GREATEST(hiredate, TO_DATE(''01/01/1753'',''MM/DD/YYYY'')), hiredate, NULL)
FROM scott.emp')

In case you are not familiar with Oracle built-ins, here is a description of
the two used in the query above.

DECODE is similar to a case statement in SQL Server. Its syntax is as
follows :

DECODE(, , , , ,...,
)

The is compared to each value. If there is a match,
the corresponding is returned. Otherwise, the value is
returned.

GREATEST returns the greatest value in the list of expressions.

GREATEST(, , ...)
Reply With Quote

Error converting data type DBTYPE_DBTIMESTAMP to datetime

Hi

I am trying to transfer data from Access 2000 database to SQL server 2005 via SSIS. My access database table has a field with data type Date/Time and SQL server table has a field with data type datetime. When I am running the SSIS package, it is throwing the following error and stops execution.

[SQL Server Destination [12466]] Error: An OLE DB error has occurred. Error code: 0x80040E07. An OLE DB record is available. Source: "Microsoft OLE DB Provider for SQL Server" Hresult: 0x80040E07 Description: "Error converting data type DBTYPE_DBTIMESTAMP to datetime.".

Please help.

Thanks in advance,
Ramzee

According to BOL (http://msdn2.microsoft.com/en-us/library/ms175970.aspx) the types can be converted, so that should work. Have you identified the values that fail? Using the error output is a very easy way to capture this data. My best guess for now is the values are out of range for the SQL datetime type. A derived column transformation could be used to massage these values depending on requirements.|||

Thanks Darren!

I used the script component to identify the wrong date entries. Now it works!

Thanks once again.

|||

I am having the same error. A simple select from Query Analyzer

Select * from HCPROD..HC.UNIT U

Even if I specifically reference a number column I get the same error

Select BLOCK_ID from HCPROD..HC.UNIT U

The way I see it is that there is a column, probably the DATE_MODIFIED column, somewhere in the database that has a date & time value that the driver cannot process. Why the error occurs even if I do not reference the column is a mystery.

I am using Microsoft OLE DB Provider for Oracle

Note that I have insured that every date field in the table is in 20th or 21st century

|||Some more information. We are using Oracle version 10g. I wonder if there is an updated driver from Microsoft? We searched and could not find one ourselves.|||SELECT TOP 10 * FROM [ORACLE_LOOKUP]..[SchemaName].[TableName] AlaisName

Error converting data type DBTYPE_DBTIMESTAMP to datetime

SELECT * FROM
OPENQUERY (
ORACLE_LOOKUP,
'SELECT TO_CHAR(DateColumnName) FROM TableName')

The following workaround was foud on the internet

A workaround would be to use the OPENQUERY function in SQL Server. This
function allows you to execute a pass-through query on the linked server.
By issuing a pass-through query, you can then take advantage of Oracle's
built-in functions to convert the date to a character data type or NULL.
Examples of both are as follows:

In the following example, the column "hiredate" is converted to a string in
the format of MM-DD-YYYY.

SELECT *
FROM OPENQUERY(, 'SELECT empno,
TO_CHAR(hiredate,''MM-DD-YYYY'') FROM scott.emp')

In this example we use a combination of two Oracle built-in functions,
DECODE and GREATEST to convert any hiredate that is earlier than 01/01/1753
(SQL Server's lower bound) to NULL.

SELECT *
FROM OPENQUERY(,'SELECT DECODE (hiredate,
GREATEST(hiredate, TO_DATE(''01/01/1753'',''MM/DD/YYYY'')), hiredate, NULL)
FROM scott.emp')

In case you are not familiar with Oracle built-ins, here is a description of
the two used in the query above.

DECODE is similar to a case statement in SQL Server. Its syntax is as
follows :

DECODE(, , , , ,...,
)

The is compared to each value. If there is a match,
the corresponding is returned. Otherwise, the value is
returned.

GREATEST returns the greatest value in the list of expressions.

GREATEST(, , ...)
Reply With Quote

Error converting a text field to a datetime datatype, who can figure this anyone??

Hello,
I'm trying to convert data on a text field to datetime datatype. This data
has invalid dates such as 10/30, 2/31235, 32/1/2004, 345/2345 etc.
I have been trying to convert whatever I can but I keep getting errors when
I perform a
SELECT CONVERT(datetime, text_field) FROM Old_data
Obviously my select statemt is abruptly terminated when I hit one of these
records with a invalid date.
I tried looping throught the records using a curson and try to capture and
check for errors:
SET @.user_1_date = convert(datetime, @.USER_1)
IF @.@.ERROR <> 0
BEGIN
PRINT 'ERROR FOUND'
END
ELSE
BEGIN
print 'NO ERROR FOUND'
END
I'm using PRINT for simplicity but the conversion error can not be
controlled through this method. SQL halts when it tries to convert the date.
I would love to be able to trap the error and skip the record and move on to
the new one. The bulk of the data is good, but there are over 175,000
records and I can not do this manually.
Any thoughts or suggestions?
ThanksAsked and answered by lw in the .programming group. Please don't
multi-post.
"Sd" <nowhere@.sadfs.com> wrote in message
news:3ac3890e730ead0e80076b10ff132aaa@.ne
ws.teranews.com...
> Hello,
> I'm trying to convert data on a text field to datetime datatype. This data
> has invalid dates such as 10/30, 2/31235, 32/1/2004, 345/2345 etc.
> I have been trying to convert whatever I can but I keep getting errors
when
> I perform a
> SELECT CONVERT(datetime, text_field) FROM Old_data
> Obviously my select statemt is abruptly terminated when I hit one of these
> records with a invalid date.
> I tried looping throught the records using a curson and try to capture and
> check for errors:
> SET @.user_1_date = convert(datetime, @.USER_1)
> IF @.@.ERROR <> 0
> BEGIN
> PRINT 'ERROR FOUND'
> END
> ELSE
> BEGIN
> print 'NO ERROR FOUND'
> END
> I'm using PRINT for simplicity but the conversion error can not be
> controlled through this method. SQL halts when it tries to convert the
date.
> I would love to be able to trap the error and skip the record and move on
to
> the new one. The bulk of the data is good, but there are over 175,000
> records and I can not do this manually.
> Any thoughts or suggestions?
> Thanks
>
>|||Sd (nowhere@.sadfs.com) writes:
> I have been trying to convert whatever I can but I keep getting errors
> when I perform a
> SELECT CONVERT(datetime, text_field) FROM Old_data
> Obviously my select statemt is abruptly terminated when I hit one of these
> records with a invalid date.
> I tried looping throught the records using a curson and try to capture and
> check for errors:
But since SQL Server thinks conversion error is reason enough for aborting
the batch, that does not fly.
But this should do:
SELECT CASE WHEN is_date(text_field) = 1
THEN convert(datetime, text_field)
END
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Error converting a text field to a datetime datatype, who can figure this anyone??

Hello,
I'm trying to convert data on a text field to datetime datatype. This data
has invalid dates such as 10/30, 2/31235, 32/1/2004, 345/2345 etc.
I have been trying to convert whatever I can but I keep getting errors when
I perform a
SELECT CONVERT(datetime, text_field) FROM Old_data
Obviously my select statemt is abruptly terminated when I hit one of these
records with a invalid date.
I tried looping throught the records using a curson and try to capture and
check for errors:
SET @.user_1_date = convert(datetime, @.USER_1)
IF @.@.ERROR <> 0
BEGIN
PRINT 'ERROR FOUND'
END
ELSE
BEGIN
print 'NO ERROR FOUND'
END
I'm using PRINT for simplicity but the conversion error can not be
controlled through this method. SQL halts when it tries to convert the date.
I would love to be able to trap the error and skip the record and move on to
the new one. The bulk of the data is good, but there are over 175,000
records and I can not do this manually.
Any thoughts or suggestions?
ThanksAsked and answered by lw in the .programming group. Please don't
multi-post.
"Sd" <nowhere@.sadfs.com> wrote in message
news:3ac3890e730ead0e80076b10ff132aaa@.news.teranews.com...
> Hello,
> I'm trying to convert data on a text field to datetime datatype. This data
> has invalid dates such as 10/30, 2/31235, 32/1/2004, 345/2345 etc.
> I have been trying to convert whatever I can but I keep getting errors
when
> I perform a
> SELECT CONVERT(datetime, text_field) FROM Old_data
> Obviously my select statemt is abruptly terminated when I hit one of these
> records with a invalid date.
> I tried looping throught the records using a curson and try to capture and
> check for errors:
> SET @.user_1_date = convert(datetime, @.USER_1)
> IF @.@.ERROR <> 0
> BEGIN
> PRINT 'ERROR FOUND'
> END
> ELSE
> BEGIN
> print 'NO ERROR FOUND'
> END
> I'm using PRINT for simplicity but the conversion error can not be
> controlled through this method. SQL halts when it tries to convert the
date.
> I would love to be able to trap the error and skip the record and move on
to
> the new one. The bulk of the data is good, but there are over 175,000
> records and I can not do this manually.
> Any thoughts or suggestions?
> Thanks
>
>|||Sd (nowhere@.sadfs.com) writes:
> I have been trying to convert whatever I can but I keep getting errors
> when I perform a
> SELECT CONVERT(datetime, text_field) FROM Old_data
> Obviously my select statemt is abruptly terminated when I hit one of these
> records with a invalid date.
> I tried looping throught the records using a curson and try to capture and
> check for errors:
But since SQL Server thinks conversion error is reason enough for aborting
the batch, that does not fly.
But this should do:
SELECT CASE WHEN is_date(text_field) = 1
THEN convert(datetime, text_field)
END
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp