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
Solved

How can I rest days to filter datetime field

Posted on 2010-08-20
4
370 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Present Absent from working date rage 11 36
Iteration Help (Asp.net VB) 5 24
Microsoft Access Delete all Records from table but Max 2 24
How to repeat the data 4 15
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 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