Solved

sql 2005 where clause with smalldatetime

Posted on 2015-02-05
4
271 Views
Last Modified: 2015-02-06
I have a sql 2005 database that has a smalldatetime field.  I am creating an asp.net application with vb.net code behind that will return a dataset from that table and fill a grid.  On my form there is a data picker.  When you select a date from the date picker it should return all records from the table that have the selected date.  The date picker returns a date in this format..
mm/dd/yyyy  here is the query I am working on but cant figure out how to get it to work... This is one of many different way I have tried...any help would be great - Todays_Date is the field with smalldatetime data type - in the example below I am just plugging in any date that is formatted how the date picker would

SELECT     KEYField, GUID, FKEYEmployee, RadioEntity, FKEYRadio_Number, Todays_Date, TimeOut, TimeIn, Location, Note, EnteredBY, EnteryTime, Status, 
                      FKEYTract, Check_Out_By, Check_Out_Time, Check_In_By, Check_In_Time
FROM         [tblCheckIn-Out]
WHERE     (TO_CHAR(Todays_Date, 'MM/DD/YYYY') = '2 / 4 / 2015')

Open in new window

0
Comment
Question by:David Modugno
  • 2
4 Comments
 
LVL 12

Expert Comment

by:Habib Pourfard
Comment Utility
The following query will work if you pass '02/04/2015' instead of '2 / 4 / 2015':

SELECT     KEYField, GUID, FKEYEmployee, RadioEntity, FKEYRadio_Number, Todays_Date, TimeOut, TimeIn, Location, Note, EnteredBY, EnteryTime, Status, 
                      FKEYTract, Check_Out_By, Check_Out_Time, Check_In_By, Check_In_Time
FROM         [tblCheckIn-Out]
WHERE     CONVERT(char(10), Todays_Date, 101) = '02/04/2015'

Open in new window

0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
For best performance you should avoid applying functions on each row of data to achieve a filter. Plus it is better to avoid the data type conversion from smalldatetime as well.

So, Instead of moving each and every row of data into a varchar in the format mm/dd/yyyy,
alter your single parameter into 2 smalldatetime values like this:

WHERE (
                [Todays_Date] >= convert(smalldatetime, @your_param, 101)
        AND
                [Todays_Date] < DATEADD(day,1, convert(smalldatetime, @your_param, 101) )
             )

This is known as using SARGable predicates.

The SQL may look more complex, but it will be faster.

Note: Please do NOT use "between", see: "Beware of Between"
0
 

Author Closing Comment

by:David Modugno
Comment Utility
Thanks for the help.. it worked perfectly
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
Excellent! Thanks for the feedback. Cheers, Paul
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now