6 Replies - 2697 Views - Last Post: 10 October 2012 - 02:36 PM Rate Topic: -----

#1 mani9597  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-July 12

how to use if statement in sql

Posted 09 October 2012 - 02:45 AM

for ex


select if (invoice.category)= "DR" then drug else

This post has been edited by Atli: 09 October 2012 - 02:48 AM
Reason for edit:: Use [code] tags when posting code.

Is This A Good Question/Topic? 0
  • +

Replies To: how to use if statement in sql

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 5,756
  • Joined: 08-June 10

Re: how to use if statement in sql

Posted 09 October 2012 - 02:55 AM

The CASE syntax is probably what you are looking for here.

That example is a bit weird though. Depending on what "drug" is supposed to be (a value or a reference to a column?), then there are far better alternatives to using conditional logic like you are attempting to do there.
Was This Post Helpful? 0
  • +
  • -

#3 mani9597  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-July 12

Re: how to use if statement in sql

Posted 09 October 2012 - 02:59 AM

View PostAtli, on 09 October 2012 - 02:55 AM, said:

The CASE syntax is probably what you are looking for here.

That example is a bit weird though. Depending on what "drug" is supposed to be (a value or a reference to a column?), then there are far better alternatives to using conditional logic like you are attempting to do there.



"dr" means field value of category column
'drug' means we are return to the application

anyway how to use if condition in sql

thanks for ur reply
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 5,756
  • Joined: 08-June 10

Re: how to use if statement in sql

Posted 09 October 2012 - 03:08 AM

View Postmani9597, on 09 October 2012 - 09:59 AM, said:

"dr" means field value of category column
'drug' means we are return to the application

So, basically what you are trying to do there is translate the acronym stored in the table into a full word by using conditionals in the SQL query? That makes no sense. Why not just store the full word in the table with the acronym (in a different column, obviously) and return that directly? That would be far more efficient.

Not to mention that by using conditionals, you'd have to rewrite the query each time you added, removed or modified the values in the table.

View Postmani9597, on 09 October 2012 - 09:59 AM, said:

anyway how to use if condition in sql

I already answered that. The CASE statement is what you are looking for if you want to use conditionals in a SQL query.

This post has been edited by Atli: 09 October 2012 - 03:09 AM

Was This Post Helpful? 2
  • +
  • -

#5 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 385
  • View blog
  • Posts: 2,652
  • Joined: 12-January 10

Re: how to use if statement in sql

Posted 09 October 2012 - 05:10 AM

here is for if else in sql

here
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 5,756
  • Joined: 08-June 10

Re: how to use if statement in sql

Posted 09 October 2012 - 08:56 AM

Those are for T-SQL statements (procedures and such), not for normal SQL queries.
Was This Post Helpful? 0
  • +
  • -

#7 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 782
  • View blog
  • Posts: 1,663
  • Joined: 30-January 09

Re: how to use if statement in sql

Posted 10 October 2012 - 02:36 PM

View PostAtli, on 09 October 2012 - 09:08 PM, said:

View Postmani9597, on 09 October 2012 - 09:59 AM, said:

"dr" means field value of category column
'drug' means we are return to the application

So, basically what you are trying to do there is translate the acronym stored in the table into a full word by using conditionals in the SQL query? That makes no sense. Why not just store the full word in the table with the acronym (in a different column, obviously) and return that directly? That would be far more efficient.

For future readers, a lookup table would be more efficient still, even though all queries requiring lookup table information necessitate an extra line of SQL to JOIN the information in. The benefits come mainly from the lookup data being stored and referenced in a manner that DBs can handle easily. DBs like integer-key values, as they take up small storage space and allow for easy comparison.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1