String concatenation in custom DBMS producing strange results - concatenation

When trying a simple CONCAT I am getting syntax errors. The connection uses a proprietary ODBC driver, which I'm sure is the issue, but want to know if there is anything I can do from my end. The following is the query I initially tried:
SELECT CONCAT('DF', '01', ServiceCode) AS RecordID
FROM ServiceCodes
However this throws a syntax error near , ServiceCode, no other information. So I tried:
SELECT 'DF' + '01' + ServiceCode AS RecordID
FROM ServiceCodes
But this returns the error term_expr type not supported which I assume is a specific error from the driver.
So then I tried:
SELECT CONCAT(CONCAT('DF', '01'), ServiceCode) AS RecordID
FROM ServiceCodes
Which seems to have worked! Returning a string like DF01MOT. This works across all servers and so I added extra fields producing the query:
SELECT CONCAT(CONCAT('DF', '01'), ServiceCode) AS RecordID, 'DF' AS Environment
FROM ServiceCodes
This also seems to have worked, except on 1 server. When running the above query on this one server it will now return DF0DFMO instead of DF01MOT, last 2 chars change in each row depending on ServiceCode, adding further fields changes this again such as:
SELECT CONCAT(CONCAT('DF', '01'), ServiceCode) AS RecordID, 'DF' AS Environment, '01' AS SO
FROM ServiceCodes
This returns DF0DF01 for this field in ALL rows.
So why would this be happening? Why is it only on one server? Driver is the same on all servers but is this an issue with the driver? What can I do to get around it?
EDIT: To avoid confusion I have modified the title and tags. This is actually accessing a custom DBMS provided by a third party from with a DTS package in SQL Server Enterprise Manager. I do believe contacting the third party is the only option.

Having seen your comments, the SQL Server tag is actually a bit misleading here. You are running on a vendor DB using their driver. Clearly their DB supports a CONCAT function that only takes two arguments, rather than the SQL Server 2012 function which can take more. That explains your first issue.
As for the other results you are seeing lower down, you'd need to raise this with the vendor. The version of SQL Server you're using for DTS doesn't really have any relevance here.

Related

SQL Server 2019 CHARINDEX returns weird result

When I run the following query in SQL Server 2019, the result is 1, whereas it should be 0.
select CHARINDEX('αρ', 'αυρ')
What could be the problem?
As was mentioned in the comments it may be because you have not declared your string literals as Unicode strings but are using Unicode characters in the strings. SQL Server will be converting the strings to another codepage and doing a bad job of it. Try running this query to see the difference.
SELECT 'αρ', 'αυρ', N'αρ', N'αυρ'
On my server, this gives the following output:
a? a?? αρ αυρ
Another issue is that CHARINDEDX uses the collation of the input which I think is probably not set correctly in this instance. You can force a collation by setting it on one of the inputs. It is also possible to set it at the instance, database and column level.
There are different collations that may be applicable. These have different features, for example some are case sensitive some are not. Also not all collations are installed with every SQL Server instance. It would be worth running SELECT * from sys.fn_helpcollations() to see the descriptions of all the installed ones.
If you change your query to this you should get the result you are looking for.
SELECT CHARINDEX(N'αρ' COLLATE Greek_BIN, N'αυρ')

Extract from Oracle to SQL Server causing failures and incorrect row counts

I have a SQL Server extract that is pulling data from an Oracle Database. The issue I'm having seems a bit off.
There is a single column in the query based on a left join that once I added it in, either
A) The extract just fails without giving any type of an error message in the job history
B) The wrong number of records is imported (i.e. running the query directly though oracle shows a return count of 2000 rows and I get 1895 on import).
Removing the join and the column resolves the issue, but unfortunately it is information I have to have. I've researched this some online and about the only information I found (something about adding to the connection string) did not solve the issue. I have also tried to account for any problems that could arise due to having null values in the column by adding a CASE statement in the extract query like this (is it a GUID value that is causing the problem, please note the pseudo code):
,CASE
WHEN oracle.guid IS NULL
THEN '00000000-0000-0000-0000-000000000000'
ELSE REPLACE(REPLACE(oracle.guid, '{', ''), '}', '')
END AS sql.guid
As you can see here I am performing a replace in the query as the Source value is bracketed and the destination values needs to have these removed.
This is coming from Oracle 12C to SQL Server 2016. The connection is being made with the OraOLEDB12.dll provided from the ODAC installer package.

Select from IBM DB2 using Openquery() and member

I want to fetch some data from an IBM DB2 server using SQL Server 2008.
For this I have tried to use:
SELECT * From Openquery(LINKED_SRV,'Select * from dta.Filename')
which gives me data, albeit rather old data.
I have since gathered that the updated data is in another Member on the server, and that the default member is the outdated one I tried to import data from.
I am told the current Member is PROD2017. I have read some IBM DB2 documentation where a Library.Filename(Member) convention is used, but using:
SELECT * From Openquery(LINKED_SRV,'Select * from dta.Filename(PROD2017)')
but this gives me an error that ( is not supported.
Of several attempts which has failed I have also tried to use:
SELECT * From Openquery(LINKED_SRV,'Select * from dta.Filename"(PROD2017)"')
which actually gives a result set, but it is the same result set as the original one omitting the membership, so I'd reckon there is some issue with the Query either way.
How can I find data using openquery() from an IBM DB2 server using Sql server, when the DB2 server uses membership?
Cenderze,
From my SQL server I can query my DB2 machine through a linked server without OPENQUERY. For example:
select t1.ordno from LinkedServer.Database.Library.Table t1
Note the use of aliasing and the 4-part identifier. Replace these with your specifics and let me know what happens.
M
I received a Notable Question award for this question today and I saw that it doesn't answer how I got this issue solved.
Mind you, this is a 3 year old question so my specifics may be a little bit off but I remember it as the following:
I queried a member for all production of 2017, called PROD2017
I understood this as a book analogy, where PROD2017 was the name of the book and that my query returned a chapter. For me, the query returned the wrong chapter (I believe January's sales or something where I wanted August's).
I told the company responsible for the data in the IBM DB2 connection and they had to redo their "sorting of the book", so that the query would return the correct month's data.
I believe they had to do this each month, but once again I'm iffy on the details seeing as how long ago this was.
Hopefully this can help someone, though :)

Queries return null when fields in the SELECT are re-ordered

We're working with a customer that has a SQL Server of unknown vintage running on a server we do not have direct access to. Our only link is via a DSN name that is the same on every client machine, but we do not know the details of that DSN. I mention this because I think there is a driver issue, and I can't tell you what driver it is.
We are using VBA to do some basic SQL reporting. The problem is that when we are fetching data from a table that has a column called "description", any attempt to retrieve data after that in the select statement will cause the RecordSet to return null values even though there is data in the fields. For instance....
SELECT description, one, two, three FROM tNasty
will cause...
DS!description
to print the expected value, but...
DS!one
will return null (and two and three). Now if all we do is re-arrange the fields to...
SELECT one, two, three, description FROM tNasty
Then all the values work fine!
I copied the data to my own machine's Express instance, and connected to it using a connection string instead of the DSN. Then it works fine no matter what the order is. This seems to imply it's something in that DSN.
Has anyone seen anything like this before? Or have suggestions on how to figure out what those DSN settings may be without being able to see them through the GUI?

Database Connectivity using Linked Server. (Numeric Values)

So I established a connection from my SQL Server 2008 Express Edition to our Oracle Database.
When I query varchar based values, it is fine, but for any numeric value, it throws me an error like this one.
Msg 9803, Level 16, State 1, Line 1
Invalid data for type "numeric".
How do I get around this?
I've run into this myself. Your error is caused by Number fields not being properly handled by the linkage. The easiest fix is to cast to a varchar inside your OPENQUERY, and to cast back to a number on the other side
SELECT CONVERT(INT, YourField ) AS YourField
FROM OPENQUERY (LINKEDSERVER,
'SELECT TO_CHAR(YourField ) AS YourField FROM RemoteTable');
Maybe this helps...
http://msdn.microsoft.com/en-us/library/ms151817.aspx
One individual said that they found the answer in Oracle's Metalink DocID 369814.1. They said the solution is to install an Oracle OLEDB driver with minimum version 10.2.0.2.20.
They installed an Oracle client 10.2.0.3 and then applied the applicable patch (described in the Metalink DocID 369814.1).
I found this post at http://forums.oracle.com/forums/thread.jspa?threadID=337842&start=15&tstart=135.
This seems like a better solution than converting to a char datatype, then converting back to a numeric datatype. I assume that those conversions will cost you in performance.
We had the same problem in one of our environments and the problem was that we had set up the linked server incorrectly.
We changed it to Microsoft OLD DB Provider for Oracle and the problems was resolved. Hope this helps.

Resources