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 SIN(”)
or
SELECT SIN(”)
Hi Pinal,
One possible solution that immediately comes to my mind is (thanks for your hint :))-
select isdate(”)
Will post if I find any other way.
PRINT @@ERROR
SELECT ISDATE(”)
Select ascii(”)
SELECT ISDATE(1) i guess adn length of that is 16 characters
select ISNUMERIC(‘a’)
select abs(”)
select CEILING(”)
select FLOOR(”)
select SIGN(”)
–All queries listed above first converts ” value to int which results to zero. try this, select cast(” as int)
–Then they resuts zero as per the their functionality.
select str(”) — results, 9 spaces then 0 i.e., ” 0″
select str(”,1) — results 0
–First, str will implicitly convert ” value to int, which result zero. try this, select cast(” as int)
–Since we have not passed length parameter to the function, length of the result of query will be 10.
–To display zero without spaces then pass length parameter as 1 (Second query returns zero without prefixing spaces)
select abs(”)
Sorry above contains numeric value, but here is another solution iguess. SELECT ABS(”) length of that is 13 characters
select 1^1
print str(”)
SELECT CAST(” AS INT)
SELECT CHAR(48)
select sign(”)
Ouch…my previous posts were a mess…lots of errors! Sorry!
Now I’m on my SQL and I can test what I say … and here’s my ‘with 14 chars I beat you’ solution =D
select @@Error
best I can do is:
SELECT @@ERROR
— bit of a cheat…
select @@ERROR
— or
select @@trancount
oops, posted and cannot delete….
print char(48)
SELECT $