Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

add 30 days before trigger date programatically?

Posted on 2007-04-11
4
Medium Priority
?
237 Views
Last Modified: 2008-02-01
this is my query
SELECT ID, Contractname, triggerdate, Amount, ClientID FROM legalcontract
triggerdate is the date where we need to follow up to client for possible renewal.   i want to add to my query a field called 30days  which is 30 days before the triggerdate.  

how can u add that programmaticaly in tsql?
0
Comment
Question by:gianitoo
[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 5

Expert Comment

by:sphillips1971
ID: 18892790
You can pass a negative to the DATEADD function to perform a date subtraction:

SELECT DATEADD(day, -30, triggerdate) AS 30days
FROM legalcontract

Scott
0
 
LVL 1

Author Comment

by:gianitoo
ID: 18892851
can my query look like this  
SELECT ID, DATEADD(day, -30, triggerdate) AS 30days, triggerdate,Contractname, startdate, enddate, Amount, ClientID FROM legalcontract
0
 
LVL 5

Accepted Solution

by:
sphillips1971 earned 2000 total points
ID: 18892869
Yes, you can add that as a column anywhere in your SQL

Scott
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 18893112
I know the answer has already been accepted but you want still want to consider this approach :-) ...

I suggest using a computed, or virtual, column.  This will keep the column definition in the table itself and not in any particular SELECT.  First, you add the column definition once to the table:

ALTER TABLE legalcontract
ADD notifydate AS DATEADD(DAY, -30, triggerdate)

Then you can SELECT it just like any other column.  For example:

SELECT ID, Contractname, notifydate, Amount, ...
FROM legalcontract
WHERE notifydate ...
ORDER BY notifydate
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

670 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