Results 1 to 5 of 5

Thread: If Statement.

  1. #1
    Join Date
    Jun 2002
    Location
    Singapore
    Posts
    71

    Unanswered: If Statement.

    Hello All.

    I am writing a simple script to check if the system date getdate() is found in my financial_periods table and datediff between start date and getdate() is equal to 0. If yes, perform some tasks.... but I keep getting an error message that reads Server: Msg 170, Level 15, State 1, Line 7
    Line 7: Incorrect syntax near ')'.


    and line 7 is the if command below.


    if (select getdate() from financial_periods where DATEDIFF(day, getdate(), start_date)) = 0
    BEGIN
    ...
    ...
    ...
    ...
    END





    Please advise what's wrong. Thank you.

  2. #2
    Join Date
    Apr 2004
    Posts
    64

    Wink

    Your WHERE clause has no comparison... You need something like
    if (select getdate() from financial_periods where DATEDIFF(day, getdate(), start_date) = 0) = a_valid_date

    or

    if exists (select getdate() from financial_periods where DATEDIFF(day, getdate(), start_date))...

    The first method will compile but probably won't run... The EXISTS method is better.
    Last edited by TechnicalAli; 07-05-04 at 07:13.

  3. #3
    Join Date
    Jul 2004
    Posts
    60
    the exists method is the way to go, but dont forget to move the "=0" inside the last paren also!! else you will continue your syntax error. also dont really need to 'select getdate()' as your 'if exists' condition. using 'select 1' is functionally the same thing and will save you the cost (albeit parsimonious) of the function call.
    peace

  4. #4
    Join Date
    Feb 2004
    Posts
    492
    Quote Originally Posted by oddity
    (...) using 'select 1' is functionally the same thing (...)
    much better: reading the 'select getdate()'-part in the IF-statement might work confusing to a reader; using select 1 instead is pretty much standard in such statements and is easier to read.

  5. #5
    Join Date
    Jun 2002
    Location
    Singapore
    Posts
    71
    Hello All.

    Thank you for all your advises.

    How silly of me to have missed out the comparison clause !!!!


    Best regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •