Reputation: 2313
From SQL Server , I need to be able to pull some information from an Oracle database based on a date column.
SET @foo = 'SELECT * from OPENQUERY(' + @LinkedServer +
', ''SELECT *
FROM Table1
WHERE date_revised > '''''+@myDate+'''''')'
Don't mind if the numbers of '
is off... I cut things out to make this shorter. I have tried using convert()
on the SQL Server side, but I cannot seem to find a format that Oracle accepts, so it keeps throwing errors.
At a minimum, I require date, hours, and minutes. When testing values in SQL Developer (Oracle) to figure out acceptable formats, I keep running into this behavior:
select to_date('2010-11-15 12:21:00', 'yyyy/mm/dd hh:mi:ssam') from dual
15-NOV-10
Clearly, I specify I want time, but it just doesn't agree with me. I've been stuck on this issue way too long.
In short, how do I format a SQL Server datetime
into a format that Oracle's to_date
function will accept, and how do I make that function properly show date and time?
Upvotes: 0
Views: 7707
Reputation: 11
I took Jeff's answer and wrapped it in a function that returns a to_date string.
CREATE FUNCTION [dbo].[fnOracleTo_DateStringFromMsSqlDate]
(
@InputDate datetime
)
RETURNS varchar(100)
AS
BEGIN
DECLARE @year varchar(4),
@month varchar(3),
@day varchar(2),
@hour varchar(2),
@minute varchar(2),
@second varchar(2),
@date smalldatetime,
@OracleTime varchar(50),
@returnString varchar(100)
SET @year = YEAR(@InputDate)
SET @month = Month(@InputDate)
SET @day = DAY(@InputDate)
-------------------------------------------------------------------------
SET @hour = DATEPART(HH, @InputDate)
SET @minute = DATEPART(MINUTE, @InputDate);
SET @second = datepart(SECOND, @InputDate);
SET @OracleTime = @day + '-' + @month + '-' + @year + ' ' + @hour + ':' + @minute + ':' + @second ;
set @returnString = 'to_date(' + char(39) + @OracleTime + char(39) + ',' + char(39) + 'DD-MM-YYYY HH24:MI:SS' + char(39) + ')'
return @returnString;
END
Upvotes: 0
Reputation: 2313
I came up with my own solution since Oracle was being uncooperative with me. I basically take the DATEPARTS
of everything and construct my own date-string in the same format in oracle. Thanks for your input all. When I get more time, I will try to get this working as it should. This bandaid will do for now.
DECLARE @year varchar(4),
@month varchar(3),
@day varchar(2),
@hour varchar(2),
@meridian varchar(2),
@minute varchar(2),
@second varchar(2),
@date smalldatetime,
@OracleTime varchar(50);
SET @date = (SELECT MAX(Processed) FROM MES_CAPP.Signoff)
SET @year = YEAR(@date)
SET @month = DATENAME(m,@date)
SET @day = DAY(@date)
-------------------------------------------------------------------------
IF (DATEPART(hh, @date) >= 12)
BEGIN
SET @hour = DATEPART(hh, DATEADD(hh, -12, @date))
SET @meridian = 'PM'
END
ELSE
BEGIN
SET @hour = DATEPART(hh, @date)
SET @meridian = 'AM'
END
SET @minute = DATEPART(MINUTE, @date)
SET @second = '00'
SET @OracleTime = @day + '-' + @month + '-' + @year + ' ' + @hour + ':' + @minute + ':' + @second + ' ' + @meridian
Upvotes: 0
Reputation: 160
Assuming @mydate is DATETIME the trick is to change the datetime to a string with a known format on SQL Server and use the format to change it back to a DATE on Oracle.
SET @foo = 'SELECT * from OPENQUERY(' + @LinkedServer +
', ''SELECT *
FROM Table1
WHERE date_revised > TO_DATE('''''
+ CONVERT( varchar( max ), @myDate, 120 )
+ ''''', ''''yyyy-mm-dd hh24:mi:ss'''') '')'
The 120 part tells SQL Server to format the date into a string with the format yyyy-mm-dd hh24:mi:ss, the format part tells Oracle how to turn the string back into a date.
Upvotes: 1
Reputation: 107696
SET @FOO = (SELECT * ...
I frown at that because you're assigning the result of a query, and *
(all columns) as well, into a variable?
Anyway, a_horse_with_no_name has sort of given you the format of the datetime literal you need to present to Oracle, being timestamp
.
SET @query = 'SELECT * FROM Table1
WHERE date_revised > timestamp ''2010-11-15 12:21:00''';
(I leave the exercise of producing the YYYY-MM-DD HH:MM:SS text to you)
Upvotes: 2