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
Two shorter queries:
select isdate(”)
select ‘zero’
Okay, so the best one I got is:
print @@error
This one isn’t bad either:
print isdate(”)
SELECT @@ERROR
13 Character:
PRINT @@ERROR
SELECT @@ERROR
–14 characters
Over 300 comments – amazing!
Results will be published on Monday!
Select isdate(”)
PRINT @@ERROR
select isdate(‘a’)
select = 6 char
isdate(‘a’) = 11 char
+1 space = 18 char
select $
select left($,1)
print tan($)
I give up…how are we meant to reply ????
Just leave a comment and it will be visible early next week!
Hi
SELECT ABS(”)
Select @@CURSOR_ROWS
select ‘0’
Since it is not clearly specified, I assume zero as a string could be acceptable.
alternate answer:
select str(”)
select cast(” as int)
print char(48)
select convert(float,”)
SELECT POWER(0, 1)
SELECT @@TRANCOUNT
SELECT @@ERROR
SELECT @@FETCH_STATUS
select checksum(”)
print @@error
select char(48)
I do feel kinda lame with this answer, but well: “PRINT ISDATE(‘a’)” does the trick in 17 chars :P
Those will do the trick aswell, depending on your system settings obviously:
PRINT @@TRANCOUNT
PRINT @@LANGID
This reminds me my old days when I used to program in assembler (Z80 micropocessor).
To set the accumulator to zero we began using ld a,0 (load a=0) but later on realized it took 2 bytes to do so, since everything was recorded in an ePROM (erasable Programmable Read Only Memory) we changed it to XOR a (1 byte) saving a lot of space.
SELECT
My answer has a backslash directly after the word SELECT without a space. 7 characters long with a result of 0.00 is SQL 2008.
PRINT @@ERROR
Select ABS(”)
Select @@NESTLEVEL
Select @@ERROR
Select DATALENGTH(”)