Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 18th, 2006, 11:34 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 479
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to surendran Send a message via Yahoo to surendran
Default between date function

Hi friends,
I have a field call [Connected Date], data type(smalldatetime)
I want to sort the data's between two dates (01/08/2006 - 01/10/2006)
i use following
SELECT * FROM Localfile WHERE [Connected Date] between CONVERT(varchar(25),19/10/2006) and CONVERT(varchar(25),19/10/2006)

but i'm getting following error
Syntax error converting character string to smalldatetime data type.



surendran
(Anything is Possible)
http://ssuren.spaces.msn.com
__________________
surendran
(Anything is Possible)
http://www.suren.info
http://ssuren.spaces.msn.com
 
Old October 19th, 2006, 01:56 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

Have you checked your date format, is correct in your select statement?

Jaime E. Maccou
 
Old October 19th, 2006, 09:31 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Surendran,

First, your conversion is incorrect... since you didn't put any quotes around the dates, you end up with a division problem instead of a date answer. Second, the dates in your WHERE clause don't even come close to what you listed in your description. Third, never use BETWEEN because of the times associated with dates.

To get the data between 01/08/2006 and 01/10/2006 as you requested, then you have to do it like this...

 SELECT *
   FROM LocalFile
  WHERE [Connected Date] >= '01/08/2006'
    AND [Connected Date] < '01/11/2006'

Notice the second date is 1 more than you wanted... that's so all times on 01/10/2006 will be included. You can't use BETWEEN because it would include midnight on 01/11/2006 and you don't want that.

The BIG reason for doing this is so that the [Connected Date] in the WHERE clause remains "sargable" so that if an index is present, it can be used.

--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
Date function issue Grafixx01 Access 2 April 11th, 2007 02:32 PM
Date Function desireemm SQL Language 1 March 26th, 2007 04:28 AM
ASP Date Function cancer10 Classic ASP Databases 3 January 5th, 2007 06:35 PM
Date Picker Function MunishBhatia Javascript How-To 1 December 18th, 2006 01:30 AM
ASP Date Function Ken11 Classic ASP Basics 3 September 30th, 2003 02:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.