Using SQL Dateparts


I don't know what my problem is with my short term memory, but certain things defy my ability to remember. Which way do I turn the faucet? Did I pass my street to the barbershop? What the heck DID I do last weekend?

In all seriousness, I was posed that last question one week after my wedding and was unable to answer for at least a minute. Oy yoy yoy.

Anyway, my point is that I have to consult SQL Books Online almost every time I need to use a Datepart of minute. I am always *pretty* sure it isn't “mm”, and I guess it is “mi”, but I still look it up. Well, this last time something clicked and I thought, “Why couldn't I just use the whole name instead?”.

So I tried it. And it worked. Yay.

So instead of this:

select dateadd(mi, 10, getdate()) 
select dateadd(mm, 1, getdate())

I can write:

select dateadd([minute], 10, getdate()) 
select dateadd([month], 1, getdate())

You don't have to use the brackets, but because those are keywords, SQL will highlight them and make my code look yucky.

Now, I realize that the hardcore will laugh and use the abbreviations anyway. And although I am not so much of a retard to HAVE to do this, I am all about not having to think when reading through someone else's code.

And since .NET uses “mm” to define “minute”, spelling it out ain't all bad.

,

Leave a Reply

Your email address will not be published. Required fields are marked *