SQL Server 2008 R2: Query MS Access from SQL Server - sql-server

Want to retrieve records from Access through linked server in SQL Server and need to convert/cast the column with VARCHAR for some constraint.
My attempt:
SELECT Cola, Colb
FROM MSAccess_LinkedServer...TableName
WHERE CAST([Cola] AS VARCHAR) = 'A123'
Unable to get the result from above query.
But when I remove CAST then I will get the result, but I want to know how to put the cast/convert.

No casting should be needed for text. How about simply:
WHERE [Cola] = 'A123'

Related

cast MS Access DATE() to Getdate()

I have an ODBC linked table in MS Access to SQL Server. In a query in MS Access, I have the following expression on a field:
DATE() - [DATE] (a field).
I need to change MS Access function DATE(), i.e. current date to SQL Server GETDATE(). How can I cast this in the expression builder in MS Access?
You can't really use the GetDate() t-sql server side value.
However, what you could do, is in place of using linked table to the sql server table?
You could create a view and have this
SELECT *, GETDATE() as MyServerDate FROM tblInvoices.
Now, in your client side query, you have a column called MyServerDate.
And thus you could do this:
SELECT *, (DATE() - [MyServerDate] as MydueDate from MyView
Of course the other way would be to use a pass-though query, but they are read-only. So if the sql is only for a report, or some screen display, then you could consider using a 100% server side query. So, you create the query in Access, but set it as a PT query. As a result, the raw SQL syntax in that Access query you build will have to be t-sql sql, not access sql.

COALESCE: SQL Server vs Oracle

I have following script:
SELECT 1
FROM Table t
WHERE COALESCE(NULL, t.ID) = NULL;
t is empty. The query returns 1 for Oracle and it returns nothing for SQL Server.
What is an output of COALESCE operation for SQL Server? Can we fix this code to behave for both DB in the same way?
What's the point of having colaesce here as your first argument is NULL.
Just do this:
SELECT 1
FROM Table t
where t.ID IS NULL;
The problem is not the Coalesce function. If the t table is empty then no rows will be found and returned by SQL Server.

Convert Access query to SQL

I am converting Access query to SQL Server.
I want to convert below lines to SQL
1. Format (210.6, "Standard")
2. Format (210.6, "#,##0.00")
How do i convert it to SQL query.
I have tried with below, but still not able to find the solution.
For the first query, i found below solution, which is correct
1. CONVERT(varchar, CAST(tSRO.OutputF11 AS money), 1)
Now, for second query, i do not know what i have to do.
From SQL Server 2012+ you can use FORMAT:
SELECT FORMAT(210.6, '#,##0.00') -- 210.60
SELECT FORMAT(1210.6, '#,##0.00') -- 1,210.60
LiveDemo
SQL Server before 2012:
SELECT REPLACE(CONVERT(VARCHAR,CONVERT(MONEY, 1210.6),1),'.00','') -- 1,210.60
LiveDemo2
Warning:
This operation is pure for presentation layer and should be done in application.

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

Mysterious:Selecting a large Xml in Sql Server

I am having a column in my table which stores XML data as a varchar(MAX).For example one of my value has around 1 LAC characters.While selecting it from the Table, i end up getting only 43679 characters for all samples.
What is the reason behind this mystery?Please,if there is any way to retrieve the complete data,help.
Try using settings of sql server management studio.
try to select with a cast:
select top 1 cast(ColumnName as xml) from Table

Resources