What general tips do you have for golfing in T-SQL? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to T-SQL. Please post one tip per answer.
Thanks to Marcog for the original idea. :)
My general bag of tricks::
For example These two are semantically equivalent.
You can use
Here's a funny one. This will convert the values in a column into a single tuple.
EDIT: Thank you for the comments. It seems like the shortest way of rolling up without the XML tags is:
Note: if XML is a valid output you can omit the outer select and parens. Also the
It is possible to use some bitwise operators in T-SQL.
I don't have a concrete example, but I believe it is a good-to-know fact when golfing in T-SQL.
Code compression using SQL
SQL is wordy,scores high, and as much as we love'em,
How this works:
The upfront cost is close to 100 bytes and each row in the replacement table costs another 6 bytes. This kind of logic won't be very effective unless you're working with a lot of code which you can't trim down or the challenge is compression-based.
Here's an example
The challenge is to get the last 10 multiples of 2,3, and 5 leading up to n. Let's say this (343 bytes golfed) is the best solution I could come up with:
Example after code is compressed
This executes the same code as above, is ~302 bytes golfed.