BETWEEN DATES SQL Server 2008

When dealing with dates, remember there is not such field as date in SQL Server 2005/2008. There is datetime field.

For reference there is a date type in MySQL which I think is very useful.

There could be problems when you use between dates in SQL Server 2008. The problem comes because datetime has time information also and that give a new meaning when you are doing comparision.

Lets get the current date.

select getdate()

2012-04-10 17:21:12.090  -- output

If I do

select getdate() as today where GETDATE()='2012-4-10'

-- I get zero result

This is because when comparing dates, sql server 2008/2005 compares time information also. if the time information is 00:00:00, in that case it will print the date but that is not the case. Therefore it does not finds a match.

Using between with dates

Keep this information, if there is no time information, everything will be simple but if there is time information, it gives a new meaning to the day.

This entry was posted in SQL Server 2008. Bookmark the permalink.

1 Response to BETWEEN DATES SQL Server 2008

  1. Pingback: Using SQL BETWEEN in SQL SERVER 2008 | Developer's Blog

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s