jeudi 8 janvier 2015

How can I make this query sargable?


I have a query which joins two tables based on a Timestamp value (don't ask).


One of these tables stores the data in a column with a Timestamp data type. The other stores it as a varchar.


Previously, the query converted the timestamp column to a varchar for the join. As I understand it this is not sargable, so I'd like to do the reverse. (The "Timestamp" table is much larger and efficiency is more of an issue).



SELECT * FROM TABLE1 INNER JOIN TABLE2 ON UPPER(master.dbo.fn_sqlvarbasetostr(cast(TimestampColumn as binary(8)))) = VARCHARTIMESTAMPCOLUMN


I've tried:



CONVERT(ROWVERSION, N'0x0000000003306BDD')


No error, but no match on the table (when a row definitely exists).



CONVERT(ROWVERSION, CAST(N'0x0000000003306BDD' as bigint))


This gives an error converting nvarchar to bigint.


How can I convert the varchar value into a timestamp rather than the other way around?





Aucun commentaire:

Enregistrer un commentaire