Domanda

I am fairly new to Access but have been tasked at work with developing a query that pulls data from our existing work order tables via an ODBC connection. The problem I am having is that some shipments have multiple loads which need to be grouped based on their ID. The ID however is case-sensitive (ex. 00000A and 00000a are different). How would I go about grouping these entries with the IDs holding as case sensitive? I have tried using many SQL command found online however they always result in an error, is this because the database is not hosted on a server?

I have tried -

SELECT wofile.id collate SQL_Latin1_General_CP1_CS_AS, First(wofile.wo_number) 
AS FirstOfwo_number, First(wofile.cust_name) AS FirstOfcust_name, 
First(woload.ship_act_date) AS FirstOfship_act_date, Last(woload.cons_act_date) 
AS LastOfcons_act_date FROM wofile INNER JOIN woload ON wofile.id = woload.wo_id 
WHERE (((StrComp([wofile].[id],[woload].[wo_id],0))=0)) 
GROUP BY wofile.id collate SQL_Latin1_General_CP1_CS_AS

and

SELECT wofile.id, First(wofile.wo_number) 
AS FirstOfwo_number, First(wofile.cust_name) AS FirstOfcust_name, 
First(woload.ship_act_date) AS FirstOfship_act_date, Last(woload.cons_act_date) 
AS LastOfcons_act_date FROM wofile INNER JOIN woload ON wofile.id = woload.wo_id 
WHERE (((StrComp([wofile].[id],[woload].[wo_id],0))=0)) 
GROUP BY wofile.id Cast(wofile.id As varbinary(100))

and

SELECT wofile.id, First(wofile.wo_number) 
AS FirstOfwo_number, First(wofile.cust_name) AS FirstOfcust_name, 
First(woload.ship_act_date) AS FirstOfship_act_date, Last(woload.cons_act_date) 
AS LastOfcons_act_date FROM wofile INNER JOIN woload ON wofile.id = woload.wo_id 
WHERE (((StrComp([wofile].[id],[woload].[wo_id],0))=0)) 
GROUP BY BINARY wofile.id
È stato utile?

Soluzione

As explained in the Microsoft support article here:

The Microsoft Jet database engine is inherently case-insensitive. When joining tables, it matches lowercase "abc" to uppercase "ABC," which in most cases is desirable.

(The same is true for the newer "Access Database Engine", a.k.a. "ACE", which is used by Access 2010.)

The article goes on to describe several strategies for performing case-sensitive JOINs. Of those, the one that is most likely to work for you (where you need both your JOINs and your GROUP BY clauses to be case-sensitive) is the one they call "Hexadecimal Expansion" where you create another column in each table that will store a hexadecimal representation of the ID value. The function they suggest is...

Public Function StrToHex(X As Variant) As Variant
Dim I As Long, Temp As String
    If IsNull(X) Then Exit Function
    Temp = Space$(Len(X) * 2)
    For I = 1 To Len(X)
        Mid$(Temp, I * 2 - 1, 2) = Right$("0" & Hex$(Asc(Mid$(X, I, 1))), 2)
    Next I
    StrToHex = Temp
End Function

...which will work provided that your ID strings do not contain Unicode characters. That function will convert a string into the hex representation of each character, e.g.,

StrToHex("GORD") --> "474F5244"
StrToHex("gord") --> "676F7264"

Once you've populated your new "surrogate ID" fields with the corresponding hex values you can use those fields in your JOINs and GROUP BY clauses.

For more information see the above-mentioned support article:

How To Perform a Case-Sensitive JOIN Through Microsoft Jet

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top