Adal Raja
Adal Raja

Reputation: 29

how to use if in a select statement

my goal is to the following:

select name, address, day,hoursworked { if hoursworked>8 then (hoursworked -8) as OT else '' } from testtable

how can i accomplish that. i have tried to use case, but i keep getting error regarding can't convert varchar to an INT. i need that in a select statement.

Upvotes: 1

Views: 55

Answers (1)

ChrisV
ChrisV

Reputation: 1309

The problem is that you're establishing the type of the column with hoursworked-8, i.e. the column should be an int, but then you're also selecting '', which is varchar. Use NULL or 0 instead.

SELECT name, address, day, CASE WHEN hoursworked > 8 THEN hoursworked-8 ELSE NULL END AS overtime

Upvotes: 4

Related Questions