In: SQL Server
Written by: Taylor Gerring
I stumbled upon an interesting concept for dealing with dates: store them in Julian day format. At first glance, I only knew the format to be some sort of ID linking to a calendar table, but in fact, the IDs were designed to match to the equivalent Julian date. This is an ingenious way to store a date for a really great reason: you get an easy-to-index key and also can coerce a meaning from the data, all in 4 bytes. Need to link to a calendar table to determine which fiscal quarter the date falls in? No problem, the join is right there. Only need the actual date for output? No join needed, just convert the value to Gregorian date. Here are a couple of conversion functions for that specific task.
Convert Julian to Gregorian:
1 2 3 4 5 6 7 8 | create function dbo.f_JulianToGregorian ( @JulianDay int ) returns datetime as begin return dateadd(dd, @JulianDay - 2415021, 0) end |
Convert Gregorian to Julian:
1 2 3 4 5 6 7 8 | create function dbo.f_GregorianToJulian ( @GregorianDate datetime ) returns int as begin return datediff(dd, 0, @GregorianDate) + 2415021 end |
Some examples:
select dbo.f_JulianToGregorian(2454467) [J2G] , dbo.f_GregorianToJulian('2008-01-01') [G2J] , dbo.f_JulianToGregorian(dbo.f_GregorianToJulian('2008-01-01')) [G2J2G] , dbo.f_GregorianToJulian(dbo.f_JulianToGregorian(2454467)) [J2G2J]
And results:
J2G G2J G2J2G J2G2J ----------------------- ----------- ----------------------- ----------- 2008-01-01 00:00:00.000 2454467 2008-01-01 00:00:00.000 2454467
In case you’re wondering, 2415021 is the number of days between January 1, 4713 B.C. (the start of the Julian calendar) and January 1, 1900 (the start of Microsoft’s date counting). Because we are limited to SQL Server’s date calculation ranges, we can only convert back to 1753 A.D. If you are fortunate enough to be working on a SQL Server 2008 system, feel free to change the datetime to datetime2, or better yet, the simple type of date. Either of those new data types will give you access to years 1 through 9999 A.D. Although I’m sure this is not a concern for most people, unfortunately, SQL Server does not provide native functionality to deal with B.C. dates.
In: SQL Server
Written by: Taylor Gerring
I stumbled upon a few little gems that SQL Server 2008 includes that I hadn’t yet heard about. Although, very few are deploying SQL 2008 to production currently, these enhancements might make life programming in 2008 much easier.
Read the rest of this entry »
In: SQL Server
Written by: Taylor Gerring
Anyone who has worked with a database for even a short amount of time has undoubtedly checked the number of rows in a table with a statement akin to this:
SELECT COUNT(*) FROM dbo.TableName
It’s quick and easy, but is expensive in terms of logical reads. Also, things get complicated when you want to check the numbers of rows for all user tables in an entire database – you might think about resorting to cursors and dynamic SQL.
Fortunately, SQL Server 2005 shipped with a whole slew of Dynamic Management Views that make monitoring your database much simpler. With a quick query, we can view the rowcount for all tables in a database – and no table scan required.
Read the rest of this entry »
In: SQL Server
Written by: Taylor Gerring
I came upon the need to calculate the number of weekdays between two dates. SQL Server offers several utilities for calculating dates, but nothing for this specific purpose. After staring at a calendar for some time and going through several revisions, I came up with a solution that seems to work pretty well. I do rely on 1 helper UDF, but it’s a pretty standard function that can easily be used elsewhere. Please note that I’ve designed the main function to be language independent—that is, it does not depend on @@DATEFIRST to be set any particular way.
Read the rest of this entry »
In: SQL Server
Written by: Taylor Gerring
A very common task for database developers taking reigns of an existing project is to normalize data. I needed a convenient way to split delimited values into a table for transformation purposes. After finding a wonderful algorithm, I decided to enhance it and transform it to a User-Defined Function in the process. Be aware that the function depends on a Tally table to exist – much like you might have with a Calendar table to join against.
Read the rest of this entry »
