Solved

How can I rest days to filter datetime field

Posted on 2010-08-20
4
372 Views
Last Modified: 2012-05-10
This is the scenario:
I have an .aspx page where the user inserts the days (integer) and other 2 criteria, then I send this data to an storeprocedure to do the filter and show the data:

      @Dias int,
      @TipoEmpleo varchar(50),
      @Localidad varchar(50)
      
      
      AS

      SELECT * FROM Ofertas WHERE TipoEmpleo = @TipoEmpleo AND UbicacionEmpleo = @Localidad AND Fecha > (Fecha - @Dias)

I need help with the  DateTime field (Fecha), I need the storeprocedure filter using the data is stored into “Fecha” less the integer data is stored into @Dias.

Thank you
Regards
Search.jpg
0
Comment
Question by:aperez_eitsa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Expert Comment

by:jbeasle3
ID: 33488235
You do know that Fecha is always greater than Fecha - @Dias. In other words Today is always greater than Today less 3 days.
However, using that logic. I would do something like this:
SELECT * FROM Ofertas WHERE TipoEmpleo = @TipoEmpleo AND UbicacionEmpleo = @Localidad AND Fecha > DateAdd(Day, -@Dias, Fecha)
 
0
 

Author Comment

by:aperez_eitsa
ID: 33489617
I was reviewing this... I did the changes you  suggest me, but it looks than the query do nothing, the issue is in here:

Fecha > DateAdd(Day, -@Dias, Fecha)

What I saw, we are comparing "Fecha" with "Fecha" -@Dias...

what we should compare is "Today" with "Fecha" -@Dias...

so, my question now would be how to get "Today" on sql??

Regards
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33489699
SELECT * FROM Ofertas
WHERE TipoEmpleo = @TipoEmpleo
AND UbicacionEmpleo = @Localidad
AND Fecha > dateadd(d, -@Dias, getdate())

-- returns all records where Fecha is less than @Dias days old
-- getdate() includes the time portion so if you search at Aug 22 12:30, the query is actually

AND Fecha > 'Aug 20 12:30'
0
 

Author Closing Comment

by:aperez_eitsa
ID: 33489721
Thank you, now it works....

Thank you for your help.

Regards
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 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