Data Type

  • Nice question! I had to guess the correct answer. According to the script below the correct answer may not be so simple.

    -- Code from the QotD

    select * into #temp from

    (

    select NULL col1, NULL col2

    union all

    select null, 'Que'

    ) A

    -- Show what's in the temp table

    Select * From #temp;

    -- Query the data types for the columns in the temp table

    SELECT

    c.name AS [Column]

    , t.name AS [Data Type]

    , c.collation_name

    , t.max_length

    , t.[precision]

    , t.scale

    FROM

    tempdb.sys.columns AS c

    INNER JOIN tempdb.sys.types AS t ON t.system_type_id = c.system_type_id

    WHERE

    object_id = object_id('tempdb..#temp')

    ORDER BY

    c.column_id;

    -- Clean up

    Drop Table #temp;

    Results:

    Column Data Type collation_name max_length precision scale

    col1 int NULL 4 10 0

    col2 varchar SQL_Latin1_General_CP1_CI_AS 8000 0 0

    Not quite the expected length of 3?

    Enjoy!

  • Dave62 (5/10/2013)


    Not quite the expected length of 3?

    Try using c.max_length instead of t.max_length.

    The column t.max_length is the maximum for the type (varchar); c.max_length is the maximum for this specific column.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Hugo Kornelis (5/10/2013)


    Try using c.max_length instead of t.max_length.

    The column t.max_length is the maximum for the type (varchar); c.max_length is the maximum for this specific column.

    Ah, that's more like it. Thanks Hugo!

    I haven't finished my first cup of coffee yet. :doze:

  • Interesting QoTD...

    Thanks..

  • Great question. I was close but got it wrong. Still, a great teaching question.

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • I was pretty sure what the types were, but ran this to make sure.

    SELECT * FROM tempdb.INFORMATION_SCHEMA.COLUMNS WHERE OBJECT_ID('tempdb..' + table_name)=OBJECT_ID('tempdb..#temp')

  • I got it right and I would like to brag about my educated guess, but I am afraid it was just a lucky guess.

    Thanks, Yogesh, and have a great weekend, everyone.

  • Revenant (5/10/2013)


    I got it right and I would like to brag about my educated guess, but I am afraid it was just a lucky guess.

    Thanks, Yogesh, and have a great weekend, everyone.

    +1. Just had a feeling. Got lucky on this one!

  • I recently ran into this issue with a coworker of mine while working through a data-warehousing job and although I thought I had found a reference elsewhere on the internet specifically for MS SQL Server, it actually just generically says that the implementation of casting a NULL literal can be handled differently internally per DBMS.

    Great question of the day!

  • Thanks for that tricky question. 🙂

  • Thanks for that tricky question on data type. 🙂

  • Tried to find a document on why the default is an INT for this type of scenario and my google-fu is failing.

    nice question.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Nice one...

  • Very tricky for me...Great one.....

  • Dave62 (5/10/2013)


    Nice question! I had to guess the correct answer. According to the script below the correct answer may not be so simple.

    -- Code from the QotD

    select * into #temp from

    (

    select NULL col1, NULL col2

    union all

    select null, 'Que'

    ) A

    -- Show what's in the temp table

    Select * From #temp;

    -- Query the data types for the columns in the temp table

    SELECT

    c.name AS [Column]

    , t.name AS [Data Type]

    , c.collation_name

    , t.max_length

    , t.[precision]

    , t.scale

    FROM

    tempdb.sys.columns AS c

    INNER JOIN tempdb.sys.types AS t ON t.system_type_id = c.system_type_id

    WHERE

    object_id = object_id('tempdb..#temp')

    ORDER BY

    c.column_id;

    -- Clean up

    Drop Table #temp;

    Results:

    Column Data Type collation_name max_length precision scale

    col1 int NULL 4 10 0

    col2 varchar SQL_Latin1_General_CP1_CI_AS 8000 0 0

    Not quite the expected length of 3?

    Enjoy!

    You can use this as well

    EXEC tempdb..sp_help '#temp';

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537

Viewing 15 posts - 16 through 30 (of 41 total)

You must be logged in to reply to this topic. Login to reply