Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Variables in SQL

  • 16-01-2013 06:59PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 419 ✭✭Mort5000


    Teradata Forum

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


  • Registered Users, Registered Users 2 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, Registered Users 2 Posts: 9,599 ✭✭✭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
Advertisement