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 str(\)
select abs(”)
PRINT @@LANGID
14 characters:
SELECT @@ERROR
Of course, to make it work correctly, you need to open a new query window, and execute the code without having run any other code (or at least one that would produce an error). :-)
Whoops…I meant to say, “…(or at least one that would NOT produce and error).
SELECT CHAR(48)
select @@rowcount
select sin(”)
14 Characters is the best I can come up with
select char(48) will give 0
SELECT 1%1
NICE!
Not sure if this counts. “fake” arithmetic calculation because it is technically “invalid” :-)
PRINT tan(”)
PRINT @@ERROR
print Abs(”)
SELECT LOG(1)
select str(0,1)
PRINT XACT_STATE()
select CHECKSUM(”)
It’s Monday!
select @@ERROR
The puzzle doesn’t put any restrictions on the output. It doesn’t say that other characters can’t also be produced. I’m going to propose the following solution: $
Here is the output in SSMS:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ‘$’.
A zero is included as required.
So, here are a few options:
SELECT ISDATE(”)
SELECT CHAR(48)
SELECT STR(”)
SELECT STR($)
SELECT $
SELECT €
SELECT £
SELECT ¢
One less character if using PRINT:
PRINT ISDATE(”)
PRINT CHAR(48)
PRINT STR(”)
PRINT STR($)
PRINT $
PRINT €
PRINT £
PRINT ¢