You asked: How to find count between two dates mssql

Oh: that age old problem. It depends on what you’re looking for? Years, days? Milliseconds?

/* Days */
select DATEDIFF(D, getdate(), '10/11/06');
select DATEDIFF(Y, getdate(), '10/01/02');

You may need to be careful with using year calculations: obviously MS SQL does not take in account the actual number of months that elapsed during year calculations. So, if you do the datediff for the dates ’10/11/06′ and ‘/12/01/03’, the result is definitely not ‘3’ .

Calculating dates in PostgreSQL is actually easier, where you can use constructs like ‘date ‘2001-10-01’ – date ‘2001-09-28”.

As an aside: you may have struggled with getting the MSQL Developer Edition to run on your high powered laptop. The DE comes with the free install of any of Microsoft Express editions of C#, C++ or Basic. In my case, a couple of months ago, I decided to turn off the automatic ‘start’ options for both SQLExpress and SQL Server Browser. To test the above code, I decided to run the above services, but found out that activating them did not get me connected to anything. Well: not MS SQL.

  • Open to the SQL Server Configuration Manager
  • Go to SQL Server 2005 Network configuration
  • Click Protocols for SQLEXPRESS
  • Enable TCP/IP
  • Restart both (earlier) mentioned services.
  • Go to ODBC Datasources manager.
  • Create a new datasource, select SQL NATIVE CLIENT (yes, you heard it here first). You should see the SQLEXPRESS (or whatever your computer’s name is) in that Server combobox.
  • Use your Windows Logon user/password combination to complete the rest of the setup.

Yeah. Really.

This entry was posted in SQL, You-Asked. Bookmark the permalink.

2 Responses to You asked: How to find count between two dates mssql

  1. alfons says:

    I know of a database where the implementers use a 64 bit integer to represent a date. It’s in milliseconds.
    You figure out what the hard part was.

Comments are closed.