Palindromic Transact SQL

Palindromes are words or phrases that read the same backwards and forwards. By the same token, Palindromic SQL executes just as well backwards and forwards

A phrase such as …

  • “Did I do, O God, did I as I said I’d do? Good, I did”
  • “Marge lets Norah see Sharon’s telegram”
  • “No, son, onanism’s a gross orgasm sin: a no-no, son”

… is a palindrome

You’ll notice that punctuation is allowed in palindromes. This means that one can actually construct palindromes fairly simply in Transact SQL, using the PUBS database. For example, the following…

Select zip [srohtua morf] from authors [piz tceles]

will execute happily in either direction just as long as you move the brackets. It is cheating of a sort, though not so brazen as Stephen Fry’s

“Rettebs, I flahd noces, eh? Ttu, but the second half is better.”

or the dreadful

“My girlfriend has a freaking weird name: Eman Driewgnikaerfasahdneirflrigym.”

using the same detestable cheating trick one could come up with…

Select title, pub_name from (select title, pub_name from titles inner join publishers on titles.pub_ID=publishers.pub_ID) [)DI_bup.srehsilbup=DI_bup.seltit no srehsilbup nioj renni seltit morf eman_bup ,eltit tceles( morf eman_bup ,eltit tceleS]

As if to legitimise the construction of palindromes, Transact SQL Even has a string function Reverse that makes the whole process easier

There was, for a short while, a craze for asking interview candidates to write code that checked the validity of a palindrome. It is a silly question, as it is hardly in the van of commercial applications for SQL. I’d prefer to ask questions that are of direct relevance to the rough and tumble of commercial SQL programming. However, I’d be delighted if someone could provide a model answer by writing the shortest palindrome checker in Transact SQL. Remember that some of the most famous palindromes ignore spaces, capitalisation and punctuation. In the meantime, what other tricks can one use for writing palindromic SQL that executes on the PUBS database? String literals? Comments? Executable strings?