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
print abs(”)
print @@ROWCOUNT
Having never answered a puzzle before – does it go right here in a reply?
select char(48)
SELECT ISDATE(”) –17
SELECT ISJSON(”) –18
SELECT @@TRANCOUNT –18
select @@ERROR
Select abs(”)
PRINT @@ERROR
–13 characters
Print abs(”)
print char(48) –14 characters
SELECT sin(”)
Print isdate(‘a’)
print @@ERROR — 13 chars, no numbers this time!
print @@ERROR
SELECT left($,1) or SELECT left(\,1) would work.
print instead of SELECT will be a character shorter too.
SELECT @@error
The above is 14 characters and returns 0 presuming it is the first and only thing being run.
Another option I can think of would be to write a function (or stored procedure) that takes 0 parameters, call it “a” and all it does is return 0. I am assuming that one other condition is that it runs on a freshly installed SQL Server instance.
Was just thinking about this, and this one is a little shorter:
PRINT @@error
It is 1 character shorter (13 characters)
print CHAR(48)
or
print log(‘1’)
select @@ERROR
SELECT @@ROWCOUNT
PRINT @@ERROR