Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Variables in SQL

Options
  • 16-01-2013 7:59pm
    #1
    Registered Users Posts: 56 ✭✭


    Hi all,

    I have a very simple sql query that uses date_from and date_to in WHERE clause. Every day I run my query I have to change dates. Is this possible to use variables instead of 'hard coded' dates?
    I would like define one variable as today's date and second variable as today - 7 days. How do go around this?
    I use Teradata sql, not sure exactly if that does make any difference for sql.

    regards,
    Sagitt


Comments

  • Registered Users Posts: 419 ✭✭Mort5000


    Teradata Forum

    Looks like there is a system calendar utility table/view there to help you out.


  • Registered Users Posts: 1,456 ✭✭✭FSL


    Use a combination of Datediff and Gedate in your query. i.e the difference in days between the date in the table and the current date is less than 7 will give you today and the previous 6 days use less than 8 if you want today and the previous seven days.


  • Registered Users Posts: 9,555 ✭✭✭DublinWriter


    There should be a date variable that you can use.

    In Oracle it would be simply:

    select * from table where sale_date >= sysdate -7 and sale_date <= sysdate

    All DBMS have a current date variable that you should be able to use.


Advertisement