Tushar T.
Tushar T.

Reputation: 345

Can we use ? operator in sql

Is it possible to use this sql query?

select ([discount_type]='Percent') ? [Percent]+'%' : [Amount]+'RS' as [Discount] from [admin].[discount] where [discount_id]=2

Upvotes: 1

Views: 54

Answers (1)

David Hedlund
David Hedlund

Reputation: 129802

You can make a CASE (which is pretty much like a switch in many other languages)

SELECT
   CASE [discount_type]
   WHEN 'Percent' THEN [Percent] + '%'
   ELSE [Amount] + 'RS' END as [Discount]
FROM [admin].[discount]
WHERE [discount_id] = 2

Note that the rest of the query uses your code as is, i.e. assuming that Percent can be concatenated with a string without prior conversion.

Upvotes: 3

Related Questions