Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Comparison of dates

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-12-12 : 08:37:46
Karunakaran writes "Hi,

I want some datas of the users who have registered with me between date1 and date2.

I tried the following queries but it didnt gave me any answers.

Select count(userids) from table1 where dateofreg between 01/11/2001 and 30/11/2001

Select count(userids) from table1 where dateofreg between 01-11-2001 and 30-11-2001

I should get the count as 621.Instead I got the answer as 0.

Please help me to solve this problem

Regards,
Karunakaran"

mono
Starting Member

36 Posts

Posted - 2001-12-12 : 08:48:48
Put the date in quotes or (better IMO) use
convert(datetime, '30/11/2001', 103) 

to ensure the date is correctly interpreted.

I haven't tested it but it would not suprise me if 01-11-2001 were evaluated to -2011 ;-)

If your dateofreg is char that could be the problem. It would need to be similarly converted.


hth,


Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2001-12-12 : 11:18:11
01/11/2001 will perform the division arithmetic, take the decimal number as a reult and convert it to a date.
It will do this in integer values so end up with 0 which is '1 jan 1900'
You expression will become

between '1 jan 1900' and '1 jan 1900'
similaly
01-11-2001 = -2011 = 30 jun 1894

try
between '20011101' and '20011130'

note that this will still miss everything on the day of 20011130.


==========================================
Cursors are useful if you don't know sql.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -