| Author | Topic | 
                            
                                    | DTFanYak Posting Veteran
 
 
                                        52 Posts | 
                                            
                                            |  Posted - 2005-05-20 : 12:34:55 
 |  
                                            | I have what I hope is a simple question.  I've got a MSDE table with the following fields:tblCustomercustID (int)custFName (varchar 30)custLName (varchar 30)custEmail (varchar 75)custJDate (smalldatetime)custPhone (biginit)So, I'm trying to write a query that will return all Customers who joined on a certain date (regardless of the time field for that date).  When the user signs up, their information is entered into this table and custJDate is set from a variable (I'm using VB.NET) which is Now().  So, you might have 3 users with todays date but they are not showing up when I run this query: SELECT custID, custFName + ' ' + custLName AS custFullName,      custEmail, custJDate, custPhone FROM tblCustomerWHERE custJDate = '5/20/2005'The date (that you see in the single quotes) is actually a variable (dtDateToCheck) of datatype DateTime which the user selects from a Calendar.  The strange thing is that if I set the tblCustomer.custJDate to "5/20/2005 12:00:00 AM", then the query will return the record.  But if the time in that field is anything else, the query doesn't return that record.So it seems to me it is a time issue.  But I don't know how to work around that.Any and all help will be GREATLY appreciated  DTFanEver-hopeful programmer-in-training  |  | 
       
                            
                       
                          
                            
                                    | SeventhnightMaster Smack Fu Yak Hacker
 
 
                                    2878 Posts | 
                                        
                                          |  Posted - 2005-05-20 : 12:55:38 
 |  
                                          | ...Where convert(datetime,convert(varchar,custJDate,101)) = '5/20/2005'Corey  Secret Service Agent: Mr. President, you're urinating on me.President Lyndon Johnson: I know I am.  It's my prerogative. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | DTFanYak Posting Veteran
 
 
                                    52 Posts | 
                                        
                                          |  Posted - 2005-05-20 : 13:23:28 
 |  
                                          | quote:That did it.  I know this is a stupid question but if "dtDateToCheck" is stored as a DateTime, then why would I have to convert the smalldatetime to a varchar to compare the two?  Unless it's because the dtDateToCheck is only storing the date (and not the time)?Regardless, THANK YOU for the answer (it's been driving me kinda nuts).DTFanEver-hopeful programmer-in-trainingOriginally posted by Seventhnight
 ...Where convert(datetime,convert(varchar,custJDate,101)) = '5/20/2005'
 
   |  
                                          |  |  | 
                            
                       
                          
                            
                                    | SeventhnightMaster Smack Fu Yak Hacker
 
 
                                    2878 Posts | 
                                        
                                          |  Posted - 2005-05-20 : 13:32:49 
 |  
                                          | it is being stored as a date and a time, but the conversion 'convert(varchar,dtDateToCheck,101)' basically strips the time to leave midnight.  converting back to a date keeps a date comparison rather than a text comparison.Corey  Secret Service Agent: Mr. President, you're urinating on me.President Lyndon Johnson: I know I am.  It's my prerogative. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | AjarnMarkSQL Slashing Gunting Master
 
 
                                    3246 Posts | 
                                        
                                          |  Posted - 2005-05-20 : 13:39:27 
 |  
                                          | And when you populate a datetime field, if you only supply the date portion, it defaults to midnight.  So, in your VB application, instead of using Now() which has both date and time, if you used Date(), you would put only dates with 00:00:00 time into your table.---------------------------EmeraldCityDomains.com |  
                                          |  |  | 
                            
                       
                          
                            
                                    | DTFanYak Posting Veteran
 
 
                                    52 Posts | 
                                        
                                          |  Posted - 2005-05-20 : 14:08:03 
 |  
                                          | Thanks for the help and for both answers.  I'm going to go ahead and change the Now() to Date().  The conversion thing is something feel quite confident in saying I never would have figured out.  I'm so glad this was answered before the weekend or my weekend would have been spent poring through SQL books.  That's not necessarily a bad thing but being that I've been doing that all week I'm kinda looking forward to a break.  Thanks again.  This site rocks.DTFanEver-hopeful programmer-in-training   |  
                                          |  |  | 
                            
                       
                          
                            
                                    | rkasseStarting Member
 
 
                                    14 Posts | 
                                        
                                          |  Posted - 2005-05-21 : 17:52:01 
 |  
                                          | quote:Using the convert function on the datetime column custJDate will force a table scan. Try something like this which will allow the index on custJDate to be used:...Where custJdate >= Dateadd(d, datediff(d, 0, @DateToCheck), 0)AND custJdate < Dateadd(d, datediff(d, 0, @DateToCheck), 1) ...For a full explanation and a good article on working with sql server dates see:http://www.sql-server-performance.com/fk_datetime.aspOriginally posted by Seventhnight
 ...Where convert(datetime,convert(varchar,custJDate,101)) = '5/20/2005'
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | praveenbattulaStarting Member
 
 
                                    4 Posts | 
                                        
                                          |  Posted - 2010-06-15 : 23:07:32 
 |  
                                          | I believe this could be the efficient way of doing it, instead of using Convert method.[url]http://praveenbattula.blogspot.com/2010/06/compare-date-only-in-datetime-field-in.html[/url]Please let me know, what do you think...thanksRare Solutionshttp://praveenbattula.blogspot.com |  
                                          |  |  | 
                            
                       
                          
                            
                                    | madhivananPremature Yak Congratulator
 
 
                                    22864 Posts | 
                                        
                                          |  Posted - 2010-06-16 : 03:07:45 
 |  
                                          | quote:SELECT * FROM [Employee] WHERE DATEADD(dd,(DATEDIFF(dd,0,DateSent)),0) = DATEADD(dd,(DATEDIFF(dd,0,@Date)),0);can be effeciently converted toSELECT * FROM [Employee] WHERE DateSent>= DATEADD(dd,(DATEDIFF(dd,0,@Date)),0) and DateSent< DATEADD(dd,(DATEDIFF(dd,0,@Date))+1,0) MadhivananFailing to plan is Planning to failOriginally posted by praveenbattula
 I believe this could be the efficient way of doing it, instead of using Convert method.[url]http://praveenbattula.blogspot.com/2010/06/compare-date-only-in-datetime-field-in.html[/url]Please let me know, what do you think...thanksRare Solutionshttp://praveenbattula.blogspot.com
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | kukleiStarting Member
 
 
                                    3 Posts | 
                                        
                                          |  Posted - 2010-07-05 : 18:52:27 
 |  
                                          | quote:Madhivanan, why do you think your method is more efficient than the one described on the link from praveenbattula. As a matter of fact the one on the link has only one = operator while you are using the between like expression which will force more calcs on the server.praveenbattulaYour link is exactly what I needed. ThanksKleidiOriginally posted by madhivanan
 
 quote:SELECT * FROM [Employee] WHERE DATEADD(dd,(DATEDIFF(dd,0,DateSent)),0) = DATEADD(dd,(DATEDIFF(dd,0,@Date)),0);can be effeciently converted toSELECT * FROM [Employee] WHERE DateSent>= DATEADD(dd,(DATEDIFF(dd,0,@Date)),0) and DateSent< DATEADD(dd,(DATEDIFF(dd,0,@Date))+1,0) MadhivananFailing to plan is Planning to failOriginally posted by praveenbattula
 I believe this could be the efficient way of doing it, instead of using Convert method.[url]http://praveenbattula.blogspot.com/2010/06/compare-date-only-in-datetime-field-in.html[/url]Please let me know, what do you think...thanksRare Solutionshttp://praveenbattula.blogspot.com
 
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | stepsonAged Yak Warrior
 
 
                                    545 Posts | 
                                        
                                          |  Posted - 2010-07-06 : 01:39:06 
 |  
                                          | How about SELECT * FROM [Employee] WHERE datediff(d,dateSent,@date)=0 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | madhivananPremature Yak Congratulator
 
 
                                    22864 Posts | 
                                        
                                          |  Posted - 2010-07-06 : 02:18:54 
 |  
                                          | quote:If the date column is idexed, praveenbattula's query will not make use of it thus causing a table scan. My method uses functions on the date variables so index can be usedMadhivananFailing to plan is Planning to failOriginally posted by kuklei
 
 quote:Madhivanan, why do you think your method is more efficient than the one described on the link from praveenbattula. As a matter of fact the one on the link has only one = operator while you are using the between like expression which will force more calcs on the server.praveenbattulaYour link is exactly what I needed. ThanksKleidiOriginally posted by madhivanan
 
 quote:SELECT * FROM [Employee] WHERE DATEADD(dd,(DATEDIFF(dd,0,DateSent)),0) = DATEADD(dd,(DATEDIFF(dd,0,@Date)),0);can be effeciently converted toSELECT * FROM [Employee] WHERE DateSent>= DATEADD(dd,(DATEDIFF(dd,0,@Date)),0) and DateSent< DATEADD(dd,(DATEDIFF(dd,0,@Date))+1,0) MadhivananFailing to plan is Planning to failOriginally posted by praveenbattula
 I believe this could be the efficient way of doing it, instead of using Convert method.[url]http://praveenbattula.blogspot.com/2010/06/compare-date-only-in-datetime-field-in.html[/url]Please let me know, what do you think...thanksRare Solutionshttp://praveenbattula.blogspot.com
 
 
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | madhivananPremature Yak Congratulator
 
 
                                    22864 Posts | 
                                        
                                          |  Posted - 2010-07-06 : 02:19:49 
 |  
                                          | quote:It is simple but will not make use of index if it is defined on the date columnMadhivananFailing to plan is Planning to failOriginally posted by stepson
 How about SELECT * FROM [Employee] WHERE datediff(d,dateSent,@date)=0
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | kukleiStarting Member
 
 
                                    3 Posts | 
                                        
                                          |  Posted - 2010-07-08 : 20:26:55 
 |  
                                          | Thanks madhivanan for the heads up. However, praveen's link uses the same functions that your solution uses. Why is it that one forces a table scan why the other (yours) doesn't?This is praveen linkSELECT * FROM [Employee] WHERE DATEADD(dd,(DATEDIFF(dd,0,DateSent)),0) = DATEADD(dd,(DATEDIFF(dd,0,@Date)),0); |  
                                          |  |  | 
                            
                       
                          
                            
                                    | kukleiStarting Member
 
 
                                    3 Posts | 
                                        
                                          |  Posted - 2010-07-08 : 20:28:46 
 |  
                                          | quote:Very simple and elegant stepson. I wonder why this one does not make use of indexes?Thanks for the feedback, guysOriginally posted by madhivanan
 
 quote:It is simple but will not make use of index if it is defined on the date columnMadhivananFailing to plan is Planning to failOriginally posted by stepson
 How about SELECT * FROM [Employee] WHERE datediff(d,dateSent,@date)=0
 
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | tkizerAlmighty SQL Goddess
 
 
                                    38200 Posts |  | 
                            
                       
                          
                            
                                    | ahmeds08Aged Yak Warrior
 
 
                                    737 Posts | 
                                        
                                          |  Posted - 2010-11-14 : 23:55:38 
 |  
                                          | u can compare by using the convert function.the date format which you need to take is 101.becoz this format returns only the datepart. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | madhivananPremature Yak Congratulator
 
 
                                    22864 Posts | 
                                        
                                          |  Posted - 2010-11-15 : 05:06:30 
 |  
                                          | quote:Did you read previous replies?MadhivananFailing to plan is Planning to failOriginally posted by ahmeds08
 u can compare by using the convert function.the date format which you need to take is 101.becoz this format returns only the datepart.
 
 |  
                                          |  |  | 
                            
                            
                                |  |