Using CAST, CONCAT and COLLATE with a LEFT OUTER JOIN - sql-server

I'm trying to CONCAT two columns and also use CAST and COLLATE but keep getting a host of different errors when I try to fix them in a way I think would work.
Basically I am trying to CONCAT two columns together but I get a collation conflict. So then, I try and COLLATE the two columns and I then get a datatype is invalid for COLLATE error. After this I try to CAST the column giving me the error to change it to a varchar but it doesn't work. I'm just unsure how to make all 3 work together.
SELECT TransactionHeader.TransactionType,
TransactionHeader.TicketStub,
CAST ( TransactionHeader.TransactionNumber AS nvarchar(8)) AS [TN],
TransactionHeader.ActualAmount,
Currencies.SwiftCode,
TransactionHeader.CurrencyID,
Divisions.ShortName,
DealHeader.StartDateNumber,
DealHeader.EndDateNumber,
CONCAT (TransactionHeader.TicketStub,
TransactionHeader.TransactionNumber) AS [DealRef]
FROM Company.dbo.TransactionHeader TransactionHeader
LEFT OUTER JOIN Company.dbo.DealHeader DealHeader
ON TransactionHeader.THDealID=DealHeader.DHDealID
LEFT OUTER JOIN Company.dbo.Currencies Currencies
ON TransactionHeader.CurrencyID=Currencies.CRRecordID
LEFT OUTER JOIN Company.dbo.Divisions Divisions
ON TransactionHeader.PrimaryPartyID=Divisions.DVRecordID
WHERE TransactionHeader.TicketStub COLLATE DATABASE_DEFAULT
= TransactionHeader.TransactionNumber COLLATE DATABASE_DEFAULT
All in all, I just want to CONCAT the TicketStub and TransactionNumber Columns but I am not sure how to get past the errors I'm getting. As far as the COLLATE goes I'm still kind of usnsure how it even works, I just know to fix the collation error I need to do it. I am very new to T-SQL and have only been writing it for the past month and a half so please, any advice at all would be very helpful. Thank you!

Collation is a setting that determines how a DB should treat character data at either the server, database, or column level. There's a really good blog on this at red-gate.. Each server, and database, will have a collation. It's common for the databases and server to match, since by default a database will inherit this setting from the model database. It is uncommon to see column level collation, but that seems to be what you have here since all of your tables are coming from the same DATABASE.
You will need to figure out what the collation is on those columns. Dave Pinal has a good write up on this on his blog. You can also do this a few other ways. See the docs for that.
Once you have your collation, you can then collate the CONCAT. It will look something like the below. Here I just use the DATABASE_DEFUALT which would probably work in your case:
CONCAT(TransactionHeader.TicketStub COLLATE DATABASE_DEFAULT,TransactionHeader.TransactionNumber COLLATE DATABASE_DEFAULT) AS [DealRef]
You can find more examples of COLLATE WITH CONCAT in this answer and this one

Related

Why am I getting syntax error of bracket?

I have the following sql query:
\
(
Msg 102, Level 15, State 1, Line 16
Incorrect syntax near ')'.
Everything seems correct. but dont know why this error?
This isn't an answer. Rather it demonstrates what happens after considering all the comments. Here is your query after editing and formatting for readability. Warning - DISTINCT is often a kludge to cover up a logic or schema fault.
select distinct Baan1.t_pono, Baan1.t_oqua, Baan1.t_eono,
Baan1.t_odat, Baan2.t_bano, Baan2.t_orno, Baan2.t_send, Baan3.t_cuno
from BAAN1.baandb.dbo.ttdsls04020 Baan1
join [M3].[v850_Staging] Mthree on
Mthree.PONum1 collate DATABASE_DEFAULT = Baan1.t_eono collate DATABASE_DEFAULT
join BAAN1.baandb.dbo.ttcedi702200 Baan2
<see anything wrong here?>
join [M3].[v850_Staging] Mthree on
Mthree.PONum1 collate DATABASE_DEFAULT = Baan2.t_bano collate DATABASE_DEFAULT
join BAAN1.[baandb].[dbo].[ttcedi010200] Baan3 on
Mthree.Relation collate DATABASE_DEFAULT = Baan3.t_reno collate DATABASE_DEFAULT
<lack of ORDER BY clause is usually a fault>
<I added a statement terminator - which is a good habit you should develop
;
Notice that columns from Mthree are not used in the select list. Notice also that alias MThree is defined TWICE. It is odd that a "staging" table (view?) in one database is used to relate rows between tables in another database. Seems much more work is needed that goes beyond basic syntax issues.

No results with DISTINCT - Multiple tables

I am trying to run the following query which gets data from two tables but I get no results:
SELECT DISTINCT([dbo].[SF].Assignment), [X].Code
FROM [dbo].[SF], [dbo].[X]
WHERE (CHARINDEX([X].Code, [dbo].[SF].Assignment COLLATE SQL_Latin1_General_CP1_CI_AS) > 0)
AND [dbo].[SF].Code = 'NULL'
When I remove the DISTINCT, I get way too many results because of my big data set that causes an out of memory exception.
Removing parentheses with the DISTINCT as well as changing to an INNER JOIN fixed my issues. #Tab Alleman and #squillman can add answer here so I could mark them as correct.

Cannot resolve collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Arabic_100_CI_AS_KS_WS" in UNION ALL operator [duplicate]

This question already has an answer here:
Cannot Resolve Collation Conflict
(1 answer)
Closed 5 years ago.
i have to databases one of these database is in my server and another one is in another server that i have just read access to it just can select data from tables.
now i have made 2 views, first view is from my data base and it's name is "qryTransmittals_Lines_with_CT_CS"
and second view gets it,s data from other server and other database is (qryTqLines)
now i want to union all data from both queries but i got error:
Cannot resolve collation conflict between "SQL_Latin1_General_CP1_CI_AS"
and "Arabic_100_CI_AS_KS_WS" in UNION ALL operator occurring in SELECT
statement column 8.
SELECT qryTqLines.*
FROM qryTqLines
UNION ALL
SELECT qryTransmittals_Lines_with_CT_CS.*
FROM qryTransmittals_Lines_with_CT_CS;
i checked collation of my table in my server is (SQL_Latin1_General_CP1_CI_AS) and about another table i do not have access to modify design of table.
please help how i can solve my problem.
As i was provided link and OP solved his problem, so posting some code from provided link for future readers
Have to make both column with same Collate so we can compare.
1a. here we change collate of search.cat_id to collate SQL_Latin1_General_CP1_CI_AS from Latin1_General_CI_AS
SELECT search.*
FROM categories INNER JOIN search
ON categories.cid = search.cat_id collate SQL_Latin1_General_CP1_CI_AS
OR
1b. here we change collate of categories.cid to Latin1_General_CI_AS from collate SQL_Latin1_General_CP1_CI_AS
SELECT search.*
FROM categories INNER JOIN search
ON categories.cid collate Latin1_General_CI_AS = search.cat_id
reference link
http://www.ashishblog.com/how-to-resolve-the-collation-conflict-and-how-to-check-collate-in-sql-server/

Getting wrong results whith specific "WHERE" condition

gurus.
I'm stuck with my problem and will appreciate any help or suggestion. Please check this pic.
I don't understand why I'm getting wrong result in bottom query. As you can see the difference with previuos query is only in "WHERE" clause, but this difference must lead to the same results since it's one-to-one join.
Important thing is that v_last_part_info is as a view and I changed it recently. I thought it's due to QEP cache, but i tried OPTION (RECOMPLIE) and even solution described here. The result is still same.
Please, help! What am I missing?
P.S.: [OBJECT_ID] is a column name, not built-in function
P.P.S: ANOTHER_DB has different collation that's the reason i need collate database_default
select Tracking
, SoItem
from v_last_part_info
where Tracking = '4170664293'
Tracking SoItem
4170664293 20
--================================================================================--
select
lpi.Tracking
, lpi.SoItem
from v_last_part_info lpi
join ANOTHER_DB..SO_HEADER h on lpi.Tracking = h.[OBJECT_ID] collate database_default
where Tracking = '4170664293'
Tracking SoItem
4170664293 20
--================================================================================--
select
lpi.Tracking
, lpi.SoItem
from v_last_part_info lpi
join ANOTHER_DB..SO_HEADER h on lpi.Tracking = h.[OBJECT_ID] collate database_default
where [OBJECT_ID] = '4170664293'
Tracking SoItem
4170664293 10
Thanks to GarethD I found out the reason. This happened because of row_number() function inside v_last_part_info. Accorgin to the definition at MSDN:
There is no guarantee that the rows returned by a query using
ROW_NUMBER() will be ordered exactly the same with each execution
unless the following conditions are true.
Values of the partitioned column are unique.
Values of the ORDER BY are unique.
Combinations of values of the partition column and ORDER BY columns are unique.
In my case option 2 was not secured.

Joining null in SQL Server, Oracle and informatica

I have two tables to join with a column (say emp_id).. if emp_id in both the tables have null values, how will SQL Server and Oracle treat???
Coz, I read that informatica will neglect the NULL rows when joining..if I handle the null, by substituting -1, a cross-join will happen which i don't want..
What can I do here?
I cannot completely neglect the rows which has NULL.
Thanks
Perhaps you want a left outer join? See wikipedia
Here's how you do it with Oracle
Here's the SQL Server documentation for left outer join.
You can't join on colA = colB and expect NULLs to compare as equal. Depending on your needs (assuming perhaps some sort of table synchronisation need below) three approaches I can think of are
Use COALESCE to substitute a value such as -1 in place of null if a suitable value exists that can never occur in your actual data. COALESCE(Table1.colA,-1) = COALESCE(Table2.colB,-1)
Use both an IS NULL and equality check on all joining columns.
Use INTERSECT (nulls will be treated as equal). Possibly in a derived table that you can JOIN back onto.

Resources