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

Dealing with custom date formats in T-SQL Expand / Collapse
Author
Message
Posted Monday, March 26, 2012 9:51 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 11:01 AM
Points: 4,656, Visits: 11,131
Jeff Moden (3/23/2012)
Gianluca, my ol' and trusted friend,

Very nice and well written article with lots of well fomatted, easy to read, documented code. Nicely done.


Thanks, you're very kind, as usual.


Shifting gears, I have a couple of questions, please.

1. What is the configuration and speed of the computer you did the testing on?
2. Which edition and version of SQL Server and Windows did you run your tests on?
3. When you did your duration testing, where did you have the result set return to? A table? Grid mode on the screen? A throw-away variable? or ???
4. When you did your duration testing, how did you measure the duration? STATISTICS TIME ON? SQL Profiler? or ???
5. When you did your duration testing, what was the configuration of your test table? Single column of dates? Indexes or not?
6. Is there any chance of you attaching your test data generator and your test harness to this thread so I can make sure that my testing is done the same way you did it?
7. Since I don't even know how to spell C#, would it be possible for you to script the CLR assembly out in a "Paul White" fashion so that I could install it on my machine for a little testing of my own?




1. Computer specs:
CPU: Intel i7 2820QM quad-core w/ hyperthreading @2.30 GHz (3.2 GHz with turbo boost)
RAM: 16 GB
DISK: 7200 RPM

And now the tricky part: actually it's a virtual machine with the following configuration:
CPU: 2 cores
RAM: 2 GB
DISK: Virtual dynamic disk on 7200 RPM physical disk

2. Editions:
OS: Windows 2008R2 x64 Enterprise Evaluation
RDBMS: SQL Server 2012 RC0

3. A throw-away variable

4. A trace

5. Single column table, with no indexes.

6. Attached to this post.

7. Attached as well.

I'm repeating the tests using this test harness, but it definitely takes time. The code repeats the tests on 10K, 100K and 1M rows for each method and that takes about 2 hours on my machine (excluding the recursive ITVF that would take waaaaay too long).
I'll post the results as soon as I am ready.

Thanks again, Jeff! I hope you find something I overlooked.


--
Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba


  Post Attachments 
Assembly.sql.txt (5 views, 12.51 KB)
harness.sql.txt (9 views, 5.94 KB)
Post #1272805
Posted Monday, March 26, 2012 9:57 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 11:01 AM
Points: 4,656, Visits: 11,131
L' Eomot Inversé (3/24/2012)
Really good article, I like it very much. So please don't take the following as suggesting that it isn't, because that would be utterly wrong.


Thank you, Tom.
Of course you like it: the last paragraph is yours!!

There are a couple of things that perhaps should have been mentioned (to warn people that they need to be considered when relevant, not to offer solutions in the article because covering everyting would make the article too long and complicated and anyway most people don't need to worry about either of these things).

1) Everything is varchar. But I may want to be able to handle languages that don't work with varchar (256 is quite a small number, there are non-alphabetic languages that have to be coped with and 256 characters is sometimes far too few). I don't think there's any need to cope with more than one language at a time, but if there were that would make it even harder to fit into varchar.
2) None of your functions will work if you are outside the Gregorian calendar. We can use T-SQL cast/convert to get Kuwaiti Hijri, as for example
select convert(varchar(32),CONVERT(datetime2,'2012-03-23 21:00:00.000'),131)

using SQL Server's capability to handle Kuwaiti dates (but I wouldn't bet on it delivering correct dates in the future, as one form or another of fundamentalist may succede in forcing a change in Kuwaiti dates). But this will not work for Saudi format, or Iranian format, or.... (the year numbers will be different hardly ever, the month numbers will be different a little of the time, the day numbers are likely to be different a lot of the time). So the argument that built in date handling by CONVERT solves the Hijri problem doesn't work, because only Kuwaiti dates can be handled. Islamic Hijri isn't the only calendar in current use whose months and years don't match up with the Gregorian calendar, even the Julian calendar would be a trivial example if anyone actually used it any more.

So people need to watch out for alphabets which don't have 256 or fewer characters and for calendars which have different (possibly variable) year lengths and different (possibly variable) month lengths from our Western culture.


You're right, Tom.
I think that both problems should not be handled in T-SQL directly, as it's not the right tool for this task IMHO. The code is already complicated enough and also suffers from other limitations that I didn't mention. For instance, I don't know if any language around the world has month names made of multiple words: that would not parse correctly. The same can be said for weekday names.
T-SQL is good for a lot of other things, but I would definitely avoid it for this task.


--
Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Post #1272811
Posted Monday, March 26, 2012 10:31 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 11:01 AM
Points: 4,656, Visits: 11,131
OK, test finished and results are the following:





The same behaviour seen on 10K rows seems to apply to 1M rows.


--
Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba


  Post Attachments 
Table.png (63 views, 10.53 KB)
chart.png (60 views, 27.75 KB)
Post #1272838
« Prev Topic | Next Topic »

Add to briefcase «««1234

Permissions Expand / Collapse