Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL IF statement

SQL IF statement

Scheduled Pinned Locked Moved Database
databasequestion
7 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DBLearner
    wrote on last edited by
    #1

    We know there's conditional branching facilities in T-SQL and PL-SQL etc. Is there a similar IF clause available for use in flat SQL statements? I can't use stored procs for certain restriction reasons, and my scenario is this. I have one table, with columns [a, b, c, d, e, f] I will pick rows, where column c = column d. However, if column d is blank (null), then column e will be used for the comparison, if e is blank (null), then column f will be used for the comparison. This is easy as pie in T-SQL, PL-SQL etc. Is there a way to do this in flat SQL statements? Thanks.

    L A 2 Replies Last reply
    0
    • D DBLearner

      We know there's conditional branching facilities in T-SQL and PL-SQL etc. Is there a similar IF clause available for use in flat SQL statements? I can't use stored procs for certain restriction reasons, and my scenario is this. I have one table, with columns [a, b, c, d, e, f] I will pick rows, where column c = column d. However, if column d is blank (null), then column e will be used for the comparison, if e is blank (null), then column f will be used for the comparison. This is easy as pie in T-SQL, PL-SQL etc. Is there a way to do this in flat SQL statements? Thanks.

      L Offline
      L Offline
      Luc 648011
      wrote on last edited by
      #2

      Hi, you can use AND and OR operators and effectively build a WHERE clause that calculates this (pseudo-code):

      c=d OR (d=null AND (c=e OR (e=null AND c=f)))

      :)

      D 1 Reply Last reply
      0
      • L Luc 648011

        Hi, you can use AND and OR operators and effectively build a WHERE clause that calculates this (pseudo-code):

        c=d OR (d=null AND (c=e OR (e=null AND c=f)))

        :)

        D Offline
        D Offline
        DBLearner
        wrote on last edited by
        #3

        Thank you! That was fast. :)

        L 1 Reply Last reply
        0
        • D DBLearner

          Thank you! That was fast. :)

          L Offline
          L Offline
          Luc 648011
          wrote on last edited by
          #4

          You're welcome. :)

          1 Reply Last reply
          0
          • D DBLearner

            We know there's conditional branching facilities in T-SQL and PL-SQL etc. Is there a similar IF clause available for use in flat SQL statements? I can't use stored procs for certain restriction reasons, and my scenario is this. I have one table, with columns [a, b, c, d, e, f] I will pick rows, where column c = column d. However, if column d is blank (null), then column e will be used for the comparison, if e is blank (null), then column f will be used for the comparison. This is easy as pie in T-SQL, PL-SQL etc. Is there a way to do this in flat SQL statements? Thanks.

            A Offline
            A Offline
            Ashfield
            wrote on last edited by
            #5

            Yo can also use the CASE statement or (depending on exactly what you want) possibly the COALESCE statement in SQL Server.

            Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

            D 1 Reply Last reply
            0
            • A Ashfield

              Yo can also use the CASE statement or (depending on exactly what you want) possibly the COALESCE statement in SQL Server.

              Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

              D Offline
              D Offline
              DBLearner
              wrote on last edited by
              #6

              Sorry if this sounds dumb, but I thought CASE decision making was only available on the procedure/function side, and not in flat SQL? If not, can you point me to some sample flat SQL code with CASE usage demonstrated? That'll be very helpful.

              A 1 Reply Last reply
              0
              • D DBLearner

                Sorry if this sounds dumb, but I thought CASE decision making was only available on the procedure/function side, and not in flat SQL? If not, can you point me to some sample flat SQL code with CASE usage demonstrated? That'll be very helpful.

                A Offline
                A Offline
                Ashfield
                wrote on last edited by
                #7

                You can use it anywhere, I'm not sure what you mean by Flat SQL - T-SQL is T-SQL, which runs trhe same in stored procs as out of them - the only execeptions are triggers and functions which are more restrictive. select NonEoM = case when [Receive side type] = 'floating' and [Receive Convention] = 'EOM' then 0 when [Pay side type] = 'floating' and [Pay Convention] = 'EOM' then 0 else 1 end from table1

                Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups