Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 1234»»»

SQL_VARIANT 1 Expand / Collapse
Author
Message
Posted Wednesday, November 9, 2011 8:21 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Tuesday, October 14, 2014 10:58 AM
Points: 5,333, Visits: 25,272
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
Post #1203233
Posted Wednesday, November 9, 2011 11:36 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 11:42 AM
Points: 13,295, Visits: 11,086
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1203273
Posted Wednesday, November 9, 2011 11:58 PM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Friday, May 9, 2014 12:47 AM
Points: 3,448, Visits: 4,407
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
Post #1203286
Posted Thursday, November 10, 2011 12:19 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 4:59 PM
Points: 4,023, Visits: 5,324
Interesting question, thanks

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Post #1203300
Posted Thursday, November 10, 2011 2:51 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 6:18 PM
Points: 6,048, Visits: 8,330
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! ;)

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
Post #1203340
Posted Thursday, November 10, 2011 3:14 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 12:37 PM
Points: 1,007, Visits: 990
Nice one, thanks
Iulian
Post #1203357
Posted Thursday, November 10, 2011 3:16 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, October 13, 2014 8:02 AM
Points: 1,258, Visits: 13,556
nice question!!!

thanks Ron!!!



rfr.ferrari
DBA - SQL Server 2008
MCITP | MCTS

remember is live or suffer twice!
Post #1203359
Posted Thursday, November 10, 2011 4:54 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, October 19, 2014 10:29 AM
Points: 1,880, Visits: 3,460
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.
Post #1203405
Posted Thursday, November 10, 2011 5:41 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 5:44 AM
Points: 1,856, Visits: 2,187
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
Post #1203442
Posted Thursday, November 10, 2011 6:35 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Friday, May 9, 2014 12:47 AM
Points: 3,448, Visits: 4,407
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.
Post #1203486
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse