SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL_VARIANT 1


SQL_VARIANT 1

Author
Message
bitbucket-25253
bitbucket-25253
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29765 Visits: 25280
Comments posted to this topic are about the item SQL_VARIANT 1

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)

Group: General Forum Members
Points: 123811 Visits: 13344
Good question, thanks. The MSDN article doesn't specify the precedence of the date datatype, so there was some guessing involved.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
vk-kirov
vk-kirov
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5278 Visits: 4408
Another great question about SQL_VARIANT, thanks!

Koen Verbeeck (11/9/2011)
The MSDN article doesn't specify the precedence of the date datatype

Another MSDN article specifies that: http://msdn.microsoft.com/en-us/library/ms173829(v=SQL.105).aspx
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSC-Dedicated
SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)

Group: General Forum Members
Points: 30618 Visits: 7611
Interesting question, thanks

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Hugo Kornelis
Hugo Kornelis
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29863 Visits: 12835
Nice question, Ron!

I only got it right because I figured that the relative order of the date and time related values would be "logical". And since the conversion to smalldatetime would chop off the seconds and the conversion to date the entire time portion, the only logical ordering of those three would be date / smalldatetim / datetime. So I knew that I was looking for an answer where the number 2 was before 3, and 3 before 5 - with the other two numbers at any possible location.
My luck was that there was only one solution left after applying this filter! Wink

PS: Is the "weird" order of the colB values accidental, or a deliberate ploy to make the question even harder?


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Iulian -207023
Iulian -207023
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3857 Visits: 1248
Nice one, thanks
Iulian
rfr.ferrari
rfr.ferrari
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3309 Visits: 13646
nice question!!!

thanks Ron!!!


rfr.ferrari
DBA - SQL Server 2008
MCITP | MCTS

remember is live or suffer twice!
the period you fastest growing is the most difficult period of your life!

Nils Gustav Stråbø
Nils Gustav Stråbø
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5889 Visits: 3575
Good question, but I'm not sure that the explanation is 100% correct.

Because values from different data type families must be explicitly cast before being referenced in comparison predicates, the effects of the The values in the following table are examples of the rules regarding data type precedence.


The values in the sql_variant column are not casted/converted into a common data type prior to the ORDER BY.
If there is data from different data type families, then the values are sorted based on the data type family precedence.

A quick example is if you add three values from the "Exact numeric" family and one from "Unicode" family.

create table #test(id int, val sql_variant)

insert into #test values(1,'2')
insert into #test values(2,1)
insert into #test values(3,3)
insert into #test values(4,0)

select *,sql_variant_property(val,'BaseType') from #test order by val


/* Result */
id val (No column name)
1 2 varchar
4 0 int
2 1 int
3 3 int



As you can see when running this query, there is no casting of values between int and the varchar values in this column. The varchar (Unicode family) has lower precedence than int (Exact numeric family) and will always be lowest value.
Thomas Abraham
Thomas Abraham
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5421 Visits: 2256
Hugo Kornelis (11/10/2011)
Nice question, Ron!

I only got it right because I figured that the relative order of the date and time related values would be "logical". And since the conversion to smalldatetime would chop off the seconds and the conversion to date the entire time portion, the only logical ordering of those three would be date / smalldatetim / datetime.


I used the same process, but was idiot enough to stop when I figured out the hierarchy of the data types (including the inferred position of date). Would have been beneficial to actual DO the process of type conversion! Well, at least NOW my mind is working this morning! ;-)

Thanks for the question.

Please don't go. The drones need you. They look up to you.
Connect to me on LinkedIn
vk-kirov
vk-kirov
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5278 Visits: 4408
Nils Gustav Stråbø (11/10/2011)
Good question, but I'm not sure that the explanation is 100% correct.


I'm sure that the part of the explanation was deleted by accident:
Because values from different data type families must be explicitly cast before being referenced in comparison predicates, the effects of the something is missing here The values in the following table are examples of the rules regarding data type precedence.


I think the meaning of the explanation is that a programmer must use something like "CAST(sql_variant_value AS datatype)" or "CONVERT(datatype, sql_variant_value, style)" rather than "sql_variant_value" by itself.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search