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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Conditional field in SQL query

Conditional field in SQL query

Scheduled Pinned Locked Moved Database
databasejavahelpquestion
3 Posts 2 Posters 0 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
    Dewald
    wrote on last edited by
    #1

    Could someone help me with this one perhaps? Let's say I have a simple SQL query like:

    SELECT EmployeeID, EmployeeName, YearStarted FROM Employees

    I'd like to return a fourth column in the result set of which the value is conditional upon the value of another column. For instance, if the value of YearStarted is older than 2007, return 'veteran' otherwise return 'spring chicken'. I'm thining something like:

    SELECT EmployeeID, EmployeeName, YearStarted, IF(YearStarted<2007,'Veteran','Spring Chicken') FROM Employees

    but obviously such an IF statement doesn't exist in SQL. Is there another way to do it?

    R 1 Reply Last reply
    0
    • D Dewald

      Could someone help me with this one perhaps? Let's say I have a simple SQL query like:

      SELECT EmployeeID, EmployeeName, YearStarted FROM Employees

      I'd like to return a fourth column in the result set of which the value is conditional upon the value of another column. For instance, if the value of YearStarted is older than 2007, return 'veteran' otherwise return 'spring chicken'. I'm thining something like:

      SELECT EmployeeID, EmployeeName, YearStarted, IF(YearStarted<2007,'Veteran','Spring Chicken') FROM Employees

      but obviously such an IF statement doesn't exist in SQL. Is there another way to do it?

      R Offline
      R Offline
      R Giskard Reventlov
      wrote on last edited by
      #2

      select
      EmployeeID,
      EmployeeName,
      YearStarted,
      case
      when YearStarted < 2007 then 'Veteran'
      else 'Spring Chicken'
      end NewColumn
      from
      Employees

      me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven

      D 1 Reply Last reply
      0
      • R R Giskard Reventlov

        select
        EmployeeID,
        EmployeeName,
        YearStarted,
        case
        when YearStarted < 2007 then 'Veteran'
        else 'Spring Chicken'
        end NewColumn
        from
        Employees

        me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven

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

        Excellent! Thanks a mil.

        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