C# .NET - Data access between Two Months - Asked By Deven Thakur on 27-Jul-16 08:03 AM

I have saved employee salary details in a database with sql server table having a field Varchar type and stored salary month such as 31-Jan-2016 or 29-Feb-2016 , 31-Mar-2016 etc.

    At front end I extracting the months from these dates and showing two drop downs having only month and year like Jan-2016, Feb-2016 and so on

From Month   and   To Month (Drop down options)

In From Month Drop-down options all months are showing Jan-2016, Feb-2016 and son on..  And same for the To Month drop-down list.

Now I want to retrieve consolidated salary data for an individual employee who chooses From drop down option such as Jan-2016 and To drop down option Apr-2016 from drop down lists.

so how can I extract data in between  Jan-2016 to Apr-2016 all values for a particular employee from the database?????

please help...



Robbe Morris replied to Deven Thakur on 27-Jul-16 09:10 AM
This was a rookie mistake made by "someone":

"details in a database with sql server table having a field Varchar type"

A varchar is a string not a mathematical date.  Thus, any query you run to get records by a date range will be inaccurate if it works at call.

You'll need to CAST/CONVERT the column to a date, datetime, smalldatetime, etc... in the WHERE clause of your query.

Which, will hurt your query performance.

Ideally, you'd fix the database and alter this date oriented column to a proper date type.  Absent that, you are stuck with always converting the values and getting worthless performance.
Deven Thakur replied to Robbe Morris on 28-Jul-16 01:25 AM
I know this that varchar is difficult to maintain at front end and retrieving data...

        But what can we do now in existing conditions.????
Robbe Morris replied to Deven Thakur on 28-Jul-16 08:57 AM
You have to do what I said, convert or cast the column as a DateTime oriented data type in your where clause.  That said, if your team has trouble with something simple like working with date oriented columns in the UI, they need some training or a good book.  This is basic level application development.  If they were to ever do this at another job, they'd likely get fired.