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. Is there an equivalent of the case construct in tSql?

Is there an equivalent of the case construct in tSql?

Scheduled Pinned Locked Moved Database
sql-servercsharpdatabasesysadminhelp
6 Posts 5 Posters 3 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.
  • J Offline
    J Offline
    Jason Jystad
    wrote on last edited by
    #1

    The Case construct I am referring to is like this: Case {expression} {result 1} : {code block} {result 2} : {code block} {result 3} : {code block} {result 4} : {code block} Else {code block} End Case I would like to do something like this in tSQL on SQL Server 2000. Rather than using daisy chained If statements. Any ideas? Jason Jystad Cito Technologies www.citotech.net Sonork ID 11.9918 >-------------------------------------------------< Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to one instruction that doesn't work. >-------------------------------------------------<

    A G C N 4 Replies Last reply
    0
    • J Jason Jystad

      The Case construct I am referring to is like this: Case {expression} {result 1} : {code block} {result 2} : {code block} {result 3} : {code block} {result 4} : {code block} Else {code block} End Case I would like to do something like this in tSQL on SQL Server 2000. Rather than using daisy chained If statements. Any ideas? Jason Jystad Cito Technologies www.citotech.net Sonork ID 11.9918 >-------------------------------------------------< Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to one instruction that doesn't work. >-------------------------------------------------<

      A Offline
      A Offline
      AndyG
      wrote on last edited by
      #2

      In SQL Server BOL, click the index tab then type CASE. Thats it. In case you don't have BOL installed, here's a site. http://www.sqlteam.com/item.asp?ItemID=922 Andy Gaskell, MCSD

      J 1 Reply Last reply
      0
      • A AndyG

        In SQL Server BOL, click the index tab then type CASE. Thats it. In case you don't have BOL installed, here's a site. http://www.sqlteam.com/item.asp?ItemID=922 Andy Gaskell, MCSD

        J Offline
        J Offline
        Jason Jystad
        wrote on last edited by
        #3

        Thanks! You are correct, I do not have access to the BOL from where I am. Thanks for the site, I was looking for something like that online, but I wasn't having much luck. Jason Jystad Cito Technologies www.citotech.net Sonork ID 100.9918 >-------------------------------------------------< Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to one instruction that doesn't work. >-------------------------------------------------<

        1 Reply Last reply
        0
        • J Jason Jystad

          The Case construct I am referring to is like this: Case {expression} {result 1} : {code block} {result 2} : {code block} {result 3} : {code block} {result 4} : {code block} Else {code block} End Case I would like to do something like this in tSQL on SQL Server 2000. Rather than using daisy chained If statements. Any ideas? Jason Jystad Cito Technologies www.citotech.net Sonork ID 11.9918 >-------------------------------------------------< Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to one instruction that doesn't work. >-------------------------------------------------<

          G Offline
          G Offline
          Gerald Schwab
          wrote on last edited by
          #4

          http://msdn.microsoft.com/library/en-us/tsqlref/ts_ca-co_5t9v.asp?frame=true

          1 Reply Last reply
          0
          • J Jason Jystad

            The Case construct I am referring to is like this: Case {expression} {result 1} : {code block} {result 2} : {code block} {result 3} : {code block} {result 4} : {code block} Else {code block} End Case I would like to do something like this in tSQL on SQL Server 2000. Rather than using daisy chained If statements. Any ideas? Jason Jystad Cito Technologies www.citotech.net Sonork ID 11.9918 >-------------------------------------------------< Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to one instruction that doesn't work. >-------------------------------------------------<

            C Offline
            C Offline
            Carlos Antollini
            wrote on last edited by
            #5

            YES... Like this

            Select
            CASE FieldNumber
            WHEN 0 THEN "Cero"
            WHEN 1 THEN "One"
            .
            .
            .
            WHEN 9 THEN "nine"
            ELSE "No Value"
            END
            From
            MyTable

            Best Regards Carlos Antollini.

            1 Reply Last reply
            0
            • J Jason Jystad

              The Case construct I am referring to is like this: Case {expression} {result 1} : {code block} {result 2} : {code block} {result 3} : {code block} {result 4} : {code block} Else {code block} End Case I would like to do something like this in tSQL on SQL Server 2000. Rather than using daisy chained If statements. Any ideas? Jason Jystad Cito Technologies www.citotech.net Sonork ID 11.9918 >-------------------------------------------------< Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to one instruction that doesn't work. >-------------------------------------------------<

              N Offline
              N Offline
              Nick Parker
              wrote on last edited by
              #6

              Yes, you are correct, it is a lot eaiser to write a Case statement than a nested if statement, especially inside those small stored procedure window of SQL Server 2000.

              (CASE WHEN Cust_Name = 1 THEN 'Good Customer' ELSE 'BAD Customer' END)

              or you can do nested Case statements such as:

              (CASE WHEN Cust_Name = 1 then 'Good Customer' ELSE(CASE WHEN Cust_Name = 2 THEN 'SO-SO Customer' ELSE 'Bad Customer' END) END)

              Hope this helps. Nick Parker

              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