Puzzle: Write a Shortest Code to Produce Zero.
It has been a long time since we have puzzle on this blog. So I have decided to post this puzzle. You have to write a shortest code which produces digit 0 (zero).
If you think this puzzle is easy, it is not true. I have two very simple but interesting condition for you.
Condition 1: Do not use numbers and arithmetic calculation
Condition 2: Do not use 0 in the code
Condition 3: Do not use function len()
Well, that’s it. So if you were thinking to write code like SELECT 1-1 or PRINT 0 or SELECT LEN(”). You can’t do that. You are not allowed to use numbers, arithmetic calculation or the digit 0 in the code.
Let me show you one valid solution.
SELECT ISDATE('a')
Now, here is the challenge for you. The above code produces the result 0 (zero) and the length of the code is 18.
Here is the final condition for you,
Condition 4: Your Solution should have length lesser than 18 characters.
Now think of the interesting solution and post the result in the comment.
I will be happy to see what you can come up with. If you are also subscribed to my newsletter at https://blog.sqlauthority.com/contact-me/sign-up/ please expect a surprise gift for you if you get the correct answer.
Reference: Pinal Dave (https://blog.sqlauthority.com)
726 Comments. Leave new
here’s mine
select @@rowcount/2
Hope this has to be approved, don’t want to give it away too soon.
print @@error
how about print IsDate(‘a’). just kidding. i came up with: select @@error. i also subscribed to your blog.
SELECT @@NESTLEVEL
—
The above code (2 hyphens) when executed has 0 rows written on the status bar
Hence it is the shortest code
also this one: select floor(”)
print abs(”)
SELECT SIN($)
There is a way to exploit an implicit conversion of an empty string to number in mathematical function. All these produce 0:
— 13
print abs(”)
print sin(”)
print tan(”)
— 14
print sign(”)
print sqrt(”)
etc…
There’s also a way to use built-in variables:
— 13
print @@ERROR
— 14 (English, so technically not 100% correct)
print @@LANGID
— 17
print @@TRANCOUNT
The ISDATE takes an empty string as well, no need to pass ‘a’:
— 16
print ISDATE(”)
One interesting case is SOUNDEX, which technically produces ‘0000’, so not sure if that counts:
— 17
print SOUNDEX(”)
If logical operations are not considered arithmetic, then this will also do:
print 1^1
Or this: print ~-1 :-)
Select round(3,-1)
SELECT @@ERROR
SELECT ISDATE(”) – trivial, sorry
sELECT LEN(”)
SELECT ABS(”)
Here’s my solution Select @@Error
PRINT LOG(1)
or if your not allowed to use numer 1
PRINT ABS(”)
The Answer is SELECT ABS(”)
select @@Error
Hi ! the shortest I could came up is:
print floor(rand())
ops! wrong place!!! Can i delete it? : )
select $;