Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / October 2007

Tip: Looking for answers? Try searching our database.

problem with query

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hans - 31 Oct 2007 02:30 GMT
Hello group,

I have the following issue:

select convert(varchar, GETDATE(), 105)
------------------------------
30-10-2007

(1 row(s) affected)

select convert(varchar, ini_fecha_actividad, 105) from agenda
------------------------------
28-10-2007
30-10-2007
31-10-2007
01-11-2007

(4 row(s) affected)

select convert(varchar, ini_fecha_actividad, 105) from agenda where
convert(varchar,ini_fecha_actividad,105) between convert(varchar,
GETDATE(), 105) AND convert(varchar, dateadd(day,1,GETDATE()), 105)
------------------------------
30-10-2007
31-10-2007

(2 row(s) affected)

select convert(varchar, ini_fecha_actividad, 105) from agenda where
convert(varchar,ini_fecha_actividad,105) between convert(varchar,
GETDATE(), 105) and convert(varchar, dateadd(day,5,GETDATE()), 105)
------------------------------

(0 row(s) affected)

What is the problem with my last query ? It should return:
30-10-2007
31-10-2007
01-11-2007

Any help will be REALLY appreciated.

        greetings,

                  hans
Plamen Ratchev - 31 Oct 2007 03:26 GMT
Hi Hans,

The problem is that in your WHERE clause you are comparing the dates as
character strings. As such the end period in your BETWEEN becomes
'05-11-2007'. That sorts alphabetically before any of the other dates and
your range does not return any results.

One of your queries returns the desired results but that is just by
coincidence (because in that case the character representation of the end
date is higher in order).

The correct way to return a range of dates is to compare dates in their
native format, not to convert them. To trim the time portion you can use
different techniques, one is via using the DATEADD and DATEDIFF functions.

Here is a query that will return the desired results:

SELECT CONVERT(VARCHAR, ini_fecha_actividad, 105)
FROM Agenda
WHERE ini_fecha_actividad >= DATEADD(day, DATEDIFF(day, '20010101',
CURRENT_TIMESTAMP), '20010101')
   AND ini_fecha_actividad < DATEADD(day, DATEDIFF(day, '20010101',
CURRENT_TIMESTAMP) + 6, '20010101');

Note that I changed the math and added 6 days to the upper boundary to
include the results dates for the next 5 days.

HTH,

Plamen Ratchev
http://www.SQLStudio.com
James Chac - 31 Oct 2007 18:04 GMT
Hans, you should try this approach which compares dates with datetypes.

declare @d1 smalldatetime
set @d1 = '11-01-2007'  -- Nov 1st
select convert(varchar, @d1, 105)
where @d1 between GETDATE() and dateadd(day,5,GETDATE())

answer: 01-11-2007

I removed the references to a table but you get the format - works the same.

Signature

James Chacata
jameschac@hotmail.com

> Hello group,
>
[quoted text clipped - 41 lines]
>
>                    hans
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.