StealthRT
StealthRT

Reputation: 10552

mysql if else for asp.net gridview

I am trying to figure out a way to do this:

If (blah = 0 then 'Yes' else if blah = 1 then 'no' else 'maybe')

in mysql. I am trying to do this since I inserted all my data into a gridview via:

    Dim strSQL As String
    strSQL = "SELECT id, billing_first_name, billing_last_name, billing_email, billing_address, billing_city, billing_state, billing_zip, " & _
                "total, price_mode, process_status, 1 as num_of_items " & _
             "FROM orders " & _
             "ORDER BY created_on DESC, processed_on DESC LIMIT 0, 20;"

    Dim dtReader As MySqlDataReader
    objCmd = New MySqlCommand(strSQL, objConn)
    dtReader = objCmd.ExecuteReader()

    grdView.DataSource = dtReader
    grdView.DataBind()

If I can do the if/then/else somewhere in vb.net code above instead of in the database then that would be nice as well. I'm more used to doing it that way with normal query code.

I can currently do this with only if and then else, not if, elseif and then else.

SELECT IF(process_status = 1, 'SUCCEEDED', 'blah') as message, id,...

Thanks!

Upvotes: 0

Views: 271

Answers (3)

spencer7593
spencer7593

Reputation: 108450

A terser alternative to a CASE expression is to nest two IF functions in an expression, e.g.

SELECT If(blah=0,'Yes',IF(blah=1,'no','maybe')) AS blah

Upvotes: 1

Guerra
Guerra

Reputation: 2790

SELECT field1,field2,field3,
CASE field3
WHEN 0
THEN 'YES'
WHEN 1
THEN 'NO'
ELSE
'MAYBE'
END AS blah
FROM table

Upvotes: 1

Icarus
Icarus

Reputation: 63964

In SQL, that's done like this:

select 
      case blah when 1 then 'yes' 
                when 0 then 'no' 
      else 'maybe' end as blah ,
      columnb  ,
      columnc
from table

Upvotes: 1

Related Questions