SQL Server Linked Server To Postgres - sql-server

I am running SQL Server 2008 R2 and have a linked server connection to a Postgres database, and use openquery() to communicate between the two. If I create a view on sql server from the Postgres database all field types are char regardless of the field type on my Postgres database.
For example - this query
SELECT *
FROM OPENQUERY(postgrez,
'Select
badgenumber As "badgenum"
,lastunlockdate As "Last Unlock"
,currentDate As "Today"
From doorswipesystem')
Has all fields as char on my SQL Server side - but on the Postgres side badgenumber is an int, and the two date fields are dates. How can I in my OpenQuery keep the original data type?
EDIT
I tried the suggestion posted in my comments by #Kentaro and used this syntax
SELECT Cast(badgenum As Int) As badgenum, Cast(lastunlockdate as datetime) As lastunlockdate,
Cast(Today As datetime) as currentDate
FROM OPENQUERY(postgrez,
'Select
badgenumber As "badgenum"
,lastunlockdate As "Last Unlock"
,currentDate As "Today"
From doorswipesystem')
but I got a compile error of:
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
I verified field types in my postgres database as well and they are int & date. The only thing that I can think of that could be causing the issue is NULL dates for the two date fields?

Related

valid date format for sybase, oracle and sql server

I am working on a replication project where data is replicated on oracle as well as sql server from sybase database.
Basically one of the tables in sybase get populated with the data to be replicated
Sybase column contains data like -
{"a":"b","c":"d","created_date":"'Feb 13 2018 1:33AM'"}
So basically when we are forming query to replicate on sql server and oracle we are using date string 'Feb 13 2018 1:33AM' to convert into date for oracle and sql server.
This date string works fine with the sql server but failed with the error like -
ORA-01858 A NON NUMERIC CHARACTER WAS FOUND WHERE A NUMERIC WAS EXPECTED
So which date format should I use in Sybase so that it will work for both oracle and sql server to replicate.
Oracle will implicitly try to convert strings to dates using the TO_DATE( date_string, format_model, nls_params ) function. Without a format model, Oracle will use the default date format which is a session parameter (so is a per-user setting) stored in the NLS_SESSION_PARAMETERS table and you can find the default value using:
SELECT value
FROM NLS_SESSION_PARAMETERS
WHERE parameter = 'NLS_DATE_FORMAT'
This means the conversion will implicitly be:
TO_DATE(
'Feb 13 2018 1:33AM',
(SELECT value FROM NLS_SESSION_PARAMETERS WHERE parameter = 'NLS_DATE_FORMAT')
)
and if the format model does not match your string then you will get an exception.
You can either set the output format to match Oracle's default or you could alter the default date format in the current Oracle session to match your current data using:
ALTER SESSION SET NLS_DATE_FORMAT = 'Mon DD YYYY HH12:MIAM';
You can also create a logon trigger to change the NLS settings when the user connects to the database and starts a session.
The other alternative is that instead of trying to use a string you use a timestamp literal, since you can specify a time component (which you can't with a date literal), and then let Oracle cast it back to a date:
TIMESTAMP '2018-02-13 01:33:00'
or you could explicitly call TO_DATE in your replication query for Oracle and specify the date format:
TO_DATE( 'Feb 13 2018 1:33AM', 'Mon DD YYYY HH12:MIAM' )

How to insert date from SQL Server by using LinkedServer to Oracle in TimeStamp(6) format

I have some records i've to migrate to Oracle server from SQL Server by using LinkedServer.
The date in SQL Server: 2014-12-31 11:16:39.000
Date should be converted to this format for Oracle: timestamp(6).
For this operation I've tried some scripts. These scripts work succesfully outside of insert script for Oracle Migrating. But any result of these isn't relevant for Oracle date type.
Scripts executed :
CONVERT(varchar(24), BEGIN_DATETIME, 121) AS BEGIN_DATE`
tried 20, 21, 120
CURRENT_TIMESTAMP AS BEGIN_DATE
CAST(BEGIN_DATETIME as timestamp)
TO_TIMESTAMP(BEGIN_DATE, 'YYYY-MM-DD HH24:MI:SS')
returns to_timestamp' is not a recognized built-in function name
And the scripts return an error:
The OLE DB provider "OraOLEDB.Oracle" for linked server "LNK_DEV" supplied invalid metadata for column "BEGIN_DATE". The data type is not supported.
I solved my problem without formating or changing date value in SQL Server while migrating. I tried LinkedServer script in diffrent type.
Script Type-1 for LinkedServer
INSERT INTO
[LNK_DEV]..[TEST].[DIAG_TABLE]
SELECT
[BEGIN_DATE] = CONVERT(varchar(24), BEGIN_DATE, 121) AS BEGIN_DATE
FROM
TEST.DIAG_TABLE_SQLSERVER
This script returns error : The OLE DB provider "OraOLEDB.Oracle" for linked server "LNK_DEV" supplied invalid metadata for column "BEGIN_DATE". The data type is not supported.
Script Type-2 for LinkedServer
INSERT INTO
OPENQUERY([LNK_DEV], 'SELECT BEGIN_DATE FROM TEST.DIAG_TABLE')
SELECT
BEGIN_DATE
FROM
TEST.DIAG_TABLE_SQLSERVER
This query has been completed successfully and the records have been moved to Oracle Server correctly.

SQL Server - Select YEAR error

Currently using SQL Server 2008. In an effort to debug some bad date data being processed, the following code was written with an example of the bad data.
SELECT ISDATE('10-22-002')
SELECT YEAR('10-22-002')
Running the statements on database A, the results are: '1' and '2002'.
Running the statements on database B, the results are: '1' and an error.
The date format is MDY on all sessions before running the statements.
Msg 241, Level 16, State 1
Conversion failed when converting datetime from character string.
Everything I'm able to find says the date format is set at either server or session level. Is there a setting at the DB level for this?
You need to cast '10-22-002' as a datetime.
SELECT YEAR(cast('10-22-002' as datetime))

How do I convert an Oracle TIMESTAMP data type to SQL Server DATETIME2 data type while connected via Link Server.?

I have tried some examples but so far not working.
I have a Link Server (SQL Server 2014) to an Oracle 12C Database.
The table contain a datatype TIMESTAMP with data like this:
22-MAR-15 04.18.24.144789000 PM
When attempting to query this table in SQL Server 2014 via link server I get the following error using this code:
SELECT CAST(OracleTimeStampColumn AS DATETIME2(7)) FROM linkServerTable
Error:
Msg 7354, Level 16, State 1, Line 8
The OLE DB provider "OraOLEDB.Oracle" for linked server "MyLinkServer" supplied invalid metadata for column "MyDateColumn". The data type is not supported.
While the error is self explanatory, I am not certain how to resolve this.
I need to convert the timestamp to datetime2. Is this possible?
You can work around this problem by using OPENQUERY. For me, connecting to Oracle 12 from SQL 2008 over a linked server, this query fails:
SELECT TOP 10 TimestampField
FROM ORACLE..Schema.TableName
...with this error:
The OLE DB provider "OraOLEDB.Oracle" for linked server "ORACLE" supplied invalid metadata for column "TimestampField". The data type is not supported.
This occurs even if I do not include the offending column (which is of type TIMESTAMP(6). Explicitly casting it to DATETIME does not help either.
However, this works:
SELECT * FROM OPENQUERY(ORACLE, 'SELECT "TimestampField" FROM SchemaName.TableName WHERE ROWNUM <= 10')
...and the data returned flows nicely into a DATETIME2() field.
One way to solve the problem is to create a view in oracle server and convert the OracleTimeStampColumn compatible with sql server's datetime2datatype. You can change the time format to 24 hours format in oracle server's view and mark the field as varchar. Then you can convert the varchar2 column to datetime2 when selecting the column in SQL Server.
In Oracle Server
Create or Replace View VW_YourTableName As
select to_char(OracleTimeStampColumn , 'DD/MM/YYYY HH24:MI:SS.FF') OracleTimeStampColumn from YourTableName
In SQL Server
SELECT CAST(OracleTimeStampColumn AS DATETIME2(7)) FROM **linkServerVIEW**

Query in SQL Server Management Studio fails to pass date to linked InterSystems server

I have a SQL Server and Cache server and need to combine some data. Most all functions are working, except when I attempt to pass a date with a variable or a parameter.
Below is a test I ran (one of many).
declare #myDate datetime
set #myDate = convert(datetime,'2012-02-01',120)
select * from ccms..dbo.dcdnstat where timestamp > '2012-02-01' -- WORKS
exec( 'select * from dbo.dcdnstat where cdn = ?', 21004) at ccms -- WORKS
exec( 'select * from dbo.dcdnstat where timestamp > ?',#myDate) at ccms -- ERROR 7213
select * from ccms..dbo.dcdnstat where timestamp > #myDate -- ERROR 7322
Msg 7213, Level 16, State 1, Line 9 The attempt by the provider to
pass remote stored procedure parameters to remote server 'ccms'
failed. Verify that the number of parameters, the order, and the
values passed are correct. Msg 7322, Level 16, State 2, Line 11 A
failure occurred while giving parameter information to OLE DB provider
"MSDASQL" for linked server "ccms".
I have tried different date formats, and as shown above I can query on other fields with variables and I can query on date if I use a specific value.
Instead of a "DATETIME" type for your stored procedure parameter, just use "DATE"
(as long as you don't need the time requirements)
I had a problem similar to this today. It had to do with the fact that the calling server and the linked server were not the same version. One was SQL Server 2005 and the other was SQL Server 2008. The problem was due to the fact that the column being queried against at the remote end (2008) was a datetime2 and the calling server (2005) does not support datetime2.
I know this is a little late but I stumbled upon it looking for something similar with the Cache system. Looks like we are working on the same backend system. Mine is the Avaya CCMS system. Here is what I do to pass dates as variables:
DECLARE #myDate DATETIME
SET #myDate = CAST('2012-07-01' AS DATETIME)
SELECT SUM(CallsOffered), SUM(CallsAnswered), SUM(SkillsetAbandonedAftThreshold)
FROM AvayaCCMS..dbo.mSkillsetStat
WHERE Timestamp = #myDate
I was doing something very similar using a Sybase database as the linked server. Just as MobileMon said, I was able to change the datetime to date and it works fine now.
declare #myDate date
set #myDate = convert(datetime,'2012-02-01',120)

Resources