Friday, May 21, 2010

Use XQuery to convert a timestamp value to a Base64 string

SQL is a very powerful language, even with its limitations. SQL doesn't have the ability to convert Timestamp values to their base64 representations. However, you can use XQuery to do the conversion for you.

In the example below "Version" is of type Timestamp. I didn't notice any performance degradation during my testing.

SELECT  t1.Version
      , cast(N'' as xml).value('sql:column("t1.Version")', 'varchar(20)') 
FROM Table1 t1

This example opens up a new way to solve problems in SQL. It is intriguing to me to think about the other uses for leveraging XQuery to pick up SQL's slack.