Answered IF Select Statement failing

  • Tuesday, February 12, 2013 4:00 PM
     
     

    My statement below is failing withthe below:

    Msg 156, Level 15, State 1, Line 1

    Incorrect syntax near the keyword 'SELECT'.

    Msg 102, Level 15, State 1, Line 1

    Incorrect syntax near '<'.

    Msg 102, Level 15, State 1, Line 10

    What is wrong with the below statement?

    IF Select DATENAME(dw,GETDATE()) = 'Monday'

    Begin

    Update statement is here

All Replies

  • Tuesday, February 12, 2013 4:02 PM
    Moderator
     
     Answered

    You don't need SELECT in that statement, e.g.

    IF DATENAME(weekday, CURRENT_TIMESTAMP) = 'Monday'

        BEGIN

          update statement here

       END



    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

  • Tuesday, February 12, 2013 4:05 PM
    Moderator
     
     
    IF DATENAME(dw,GETDATE()) = 'Monday'

    Begin

  • Tuesday, February 12, 2013 4:15 PM
     
     

    The short answer is that you do not need the SELECT. The right answer is that you do not know how to write SQL and are still writing BASIC. SQL is a declarative language, so we do not use IF-THEN flow control or loops. We do not write code that is language dependent like you did. We also have the ANSI/ISO Standard CURRENT_TIMESTAMP now. Here is the skeleton.

    UPDATE Foobar
       SET floob = ???
     WHERE DATEPART(DW, CURRENT_TIMESTAMP)) = 1;

     


    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking in Sets / Trees and Hierarchies in SQL

  • Tuesday, February 12, 2013 4:18 PM
     
      Has Code

    Hi

    Please try

    if(datename(Dw,getdate())='Monday')
    begin
    Update statement is here
    


    Ahsan Kabir Please remember to click Mark as Answer and Vote as Helpful on posts that help you. This can be beneficial to other community members reading the thread. http://www.aktechforum.blogspot.com/