Quarksoft Blog

This is where we post useful information for everyone. Lots of it is technical, but some can be used by anyone.

    Creating Dates in SQL Server… FAST!

    << asp.net  Need to make screen captures for documentation? >>

    Found a great tip online on how to create a date in T-SQL in a very fast way.  The code looks like this:

    declare @year int, @month int, @day int
    select @year = 2006, @month = 1, @day = 16
    select dateadd(month,((@Year-1900)*12)+@Month-1,@Day-1)

    This is especially useful when doing date comparisons in queries.  Many people use the convert function, but the above code executes far faster because it does not have to do any string manipulations.  Credit should be given to Michael Valentine Jones for this tip.  More information about this tip can be found here.  Here is some more great information on date manipulation for SQL Server.

    Leave a Reply