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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Variables in SQL

  • 16-01-2013 6: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,566 ✭✭✭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