SQL SERVER – Puzzle – Write a Shortest Code to Produce Zero

Puzzle: Write a Shortest Code to Produce Zero.

SQL SERVER - Puzzle - Write a Shortest Code to Produce Zero 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)

SQL Function, SQL Scripts, SQL Server
Previous Post
SQL SERVER – Puzzle – Incorrect Results with Decimal
Next Post
SQL SERVER – Select Unique Data From a Column Exist in Two Different Table

Related Posts

726 Comments. Leave new

  • You said shortest code that produces 0 – what you didn’t explicitly say is that it only produces 0 and nothing else. So…

    $

    (1 character)

    Reply
  • You didn’t specify a min SQL Server version:

    SELECT ISJSON(”)
    (17 characters)

    I also shortened yours:

    SELECT ISDATE(1)
    (16 characters)

    Reply
  • Rakesh C Sunagar
    August 30, 2017 1:05 pm

    select left($,1) –length 16

    Reply
  • Andrey Nikolov
    August 30, 2017 2:30 pm

    print abs(”)

    Reply
  • Here are some examples. Choose which one you like:
    select @@ERROR
    select @@TRANCOUNT
    select @@NESTLEVEL
    select IS_MEMBER(‘sys’)
    select DIFFERENCE(‘a’, ”)

    Reply
  • select @@langid,
    @@Nestlevel,
    @@fetch_status,
    str(”)

    some of them are env specific

    Reply
  • whats the correct answer Pinal?

    Reply
  • Edward Carandang
    August 30, 2017 6:08 pm

    SELECT @@ERROR

    Reply
  • print str($)

    Reply
  • What is the answer?? You said you would post it on Monday. It’s now Wed!

    Reply
    • Well, the answers are all posted in the comment.

      I did not expect to have 661 comments and I am going through all of this to make sure that I do not miss any shortest answer.

      Reply
  • print abs($)

    Reply
  • print $

    Reply
  • the rules were slightly confusing. If rule #1 means no numbers at all, then rule #2 is redundant, as 0 is a number. So, assuming #2 is not redundant, that must mean that #2 does not exclude 0 somehow. The way I can see that as being true is that rule #1 excludes numbers when used with arithmetic. But it does not exclude numbers when not used with arithmetic. Or perhaps there is a subtle distinct between digit and number?

    So, the selection criteria for a winner seems to be

    select *
    from answers
    where not (usesNumbers and usesArithmetic) — Rule #1
    and not (usesDigitZero) — Rule #2
    and not (usesLenFunction) — Rule #3

    This logic would seem to say that numbers without arithmetic are OK, and arithmetic without numbers are OK, so long at the numbers used don’t include 0.

    It is unclear whether “select pi()-pi()” would be valid or not. pi is indeed a number (albeit irrational), but does not use any of the digits 0-9, and one could argue that it is not the number pi that is used, but the function pi(). So it doesn’t violate rule #1, because although it uses arithmetic it doesn’t use a number. But if you say it does violate because pi is a number, and therefore uses a number an arithmetic both, than that would mean the interpretation of ‘number’ is broader than just digits 0-9, but includes other things which are representations of numbers, such as pi, or such as $ or £.

    Anyway, fun excercise. I don’t think people need to ask PInal what is “the right answer”. There could be multiple “right answers” if they are the same length and don’t violate the rules. Which rules seem open to multiple interpretations. Rather, it’s fun to just see the large variety of responses, and perhaps even learn from them.

    Reply
    • Thank you Joe!

      Actually, I am going through exactly the same feeling. There are so many right answers and so many winners :-)

      Reply
  • Select @@ERROR

    Reply
  • select abs(”)

    Reply
  • SELECT ISnumeric(‘a’)

    Reply
  • SELECT ISDATE(¥) –16

    Reply
  • Doncho nailed it with 7: print $

    Reply
  • SELECT CONVERT(int, .1)

    Reply
  • SELECT 1^1

    Reply

Leave a Reply