Dates with a Hint of Nuttiness

Meant to post this yesterday evening. Oops!

Day 2 – Today was fairly uneventful. The highlight of my day was when I found my stash of pistachios. There is a saying that you are what you eat. I like to eat nuts like pistachios. Therefore, I am nuts. Hmm… that could explain quite a few things.

Ever have one of those days when your code fix breaks something else? Yeah, it was one of those days. Thankfully, I resolved the coding issues… for now. It turned out that I just needed some good test cases.

So far there haven’t been any major production issues. I don’t think the servers have realized the rest of the team is out of town. They are blissfully unaware, so far. I probably just jinxed myself. Note to self: stock up on pixie dust.

I finally broke out the chocolate. Been sharing it with our adopted developer. I think she needs it more than I. She keeps laughing and giggling at her computer which may or may not be a good sign, depending on how you look at it. Note to self: stay away from the project she’s working on, if at all possible.

Found some odd code in a function that doesn’t make a lot of sense.  Here is basically what I found:

set @currentDate = dateadd(dd,datediff(dd,1,@currentDate),1)
set @ageInDays = datediff(dd,@dateOfBirth,@currentDate)

What I don’t understand is what was done to @currentDate prior to calculating the age in days. When I add some code to test it out, it doesn’t seem to do anything to the current date or age calculation. I am guessing they are trying to add one day to the current date. To test it out, here is what I did:

declare @dateOfBirth datetime
,@currentDate datetime
,@ageInDays int

set @dateOFBirth = '1/1/1975'
set @currentDate = getdate()

select @currentDate currentDatePrior

set @currentDate = dateadd(dd,datediff(dd,1,@currentDate),1)
set @ageInDays = datediff(dd,@dateOfBirth,@currentDate)

select @currentDate currentDateAfter, @ageInDays ageInDays

Here are the results from running the above code:

The only difference I can see is that after using the dateadd(), the time is removed from the currentDate. Whether or not that was the intention, I do not know. If so, then there are different ways of doing this. To me, it would be a bit more straightforward to do this:

select cast(convert(varchar(10), getdate(), 101) as datetime) sameDateDifferentWay

You would get the same result and it may be a bit more obvious what the intention is. This is a great example of why comments would be beneficial. Then again, that’s just my opinion. 🙂

Advertisements

2 thoughts on “Dates with a Hint of Nuttiness

    1. Hi Brian,

      Thanks for reading and commenting. 🙂 You are welcome for the info. I hope it helped.

      I’m looking forward to when my team returns. It’s too quiet here without them. 🙂

      Rebecca

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s