Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COLLATE DATABASE_DEFAULT agaisnt an IN () clause

I have a problem returning records in a sproc. No error is thrown but I think data coming from one server environment is not mixing well in another.

set @shipedon =  YEAR(@shipdate) * 10000 + MONTH(@shipdate) * 100 + DAY(@shipdate)

This works:

SELECT   [ITEM_KEY],
         max([REVISION]) Rev
FROM     SERVER2.[BOMSystem].[dbo].[ITEM]
WHERE    ITEM_KEY collate DATABASE_DEFAULT in
         ('391000180', '391000189', '391000191', '391000201',
          '391000214', '391000215', '391000216', '391000226')
AND      DATE_EFF_FROM <= @shipedon 
GROUP BY ITEM_KEY

shows 8 rows like:

391000180   0001
391000189   0001
391000191   0001

This fails:

SELECT   [ITEM_KEY],
         max([REVISION]) Rev
INTO     #rev
FROM     SERVER2.[BOMSystem].[dbo].[ITEM] 
WHERE    ITEM_KEY collate DATABASE_DEFAULT in (@items)                                                       
AND      DATE_EFF_FROM  <= @shipedon
GROUP BY ITEM_KEY

SELECT * from #rev shows no results.

SELECT @items = SUBSTRING(
(SELECT distinct ',' +''''+ ltrim(rtrim(ItemNumber )) +'''' 
collate DATABASE_DEFAULT 
FROM #ShipTemp   
FOR XML PATH('')),2,20000) 
like image 227
SteveO Avatar asked Feb 20 '14 14:02

SteveO


People also ask

What does COLLATE Database_default mean?

You can also use the database_default option in the COLLATE clause to specify that a column in a temporary table use the collation default of the current user database for the connection instead of tempdb. Casting the collation of an expression.

What is the purpose of including a COLLATE clause in a column definition?

As with database definitions, you can add the COLLATE clause when you define a column of characters. This allows you to apply a specific collation to the data in the column without affecting the rest of the database.

What is COLLATE Latin1_General_CS_AS in SQL Server?

According the SQL Server Books Online, the characters included in range searches depend on the sorting rules of the collation. Collation Latin1_General_CS_AS uses dictionary order so both upper and lower case characters of the specified range are included.

Why we use COLLATE in SQL Server?

Collations in SQL Server provide sorting rules, case, and accent sensitivity properties for your data. Collations that are used with character data types, such as char and varchar, dictate the code page and corresponding characters that can be represented for that data type.


1 Answers

There is no need to concatenate all of them into a comma delimited string.

IN accepts a sub query that returns a single column of items to be tested. Just use

SELECT [ITEM_KEY],
       max([REVISION]) Rev
INTO   #rev
FROM   SERVER2.[BOMSystem].[dbo].[ITEM]
WHERE  ITEM_KEY COLLATE DATABASE_DEFAULT IN (SELECT LTRIM(ItemNumber )
                                             FROM   #ShipTemp)
       AND DATE_EFF_FROM <= @shipedon
GROUP  BY ITEM_KEY 

Also no need to RTRIM as trailing spaces aren't significant in a comparison and if ItemNumber is numeric you should use a numeric datatype, not a string.

like image 141
Martin Smith Avatar answered Oct 26 '22 17:10

Martin Smith