SQL SERVER – A Simple Puzzle and Simple Solution of Datatype and Computed Column

A reader’s interview riddle about datatypes has a neat answer in a computed column. Christmas is just near and happy holidays to all of you. Today is Christmas eve and I decided to share something very simple but interesting with you. Recently some one reading my SQL Server Interview Questions and Answers book asked me following question.

SQL SERVER - A Simple Puzzle and Simple Solution of Datatype and Computed Column

“Pinal,

Instead of puzzle, or difficult interview question, I was asked following riddle in my interview. I could not answer it, do you have any idea.

Riddle: Create a table with two columns but you are allowed to specify datatypes only once. Additionally, write a mechanism that your data is copied from first column to second column without using any update statement. Also the column name should not contain any alphanumeric character.

How do I do this?”

For a moment I was confused too. As the requirement were very interesting, coping data without using update statement, no use of alphanumeric statement etc.

Finally I came up with following small and quick script.

CREATE TABLE MyTabs ([!] INT, [!!] AS [!]);
INSERT INTO MyTabs VALUES (1);
SELECT * FROM MyTabs;

If you are free, can you come up with further optimize script?

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.

SQL Scripts
Previous Post
SQL SERVER – Solution to Puzzle – Simulate LEAD() and LAG() without Using SQL Server 2012 Analytic Function
Next Post
SQL SERVER – Merry Christmas and Happy Holidays – Database Properties – Number of Users

Related Posts

26 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.