0

how can I convert to T-Sql this one?

IIf([ESSValue]<>0,Int([ESSValue]*100),"")
4
  • If/else logic in sql is normally done with the case when keywords. Commented Nov 1, 2017 at 14:07
  • 2
    This is TSQL. If you are doing operation on integers, you can't return string value. Try null instead. Commented Nov 1, 2017 at 14:07
  • What type is [ESSValue] ?? Why you convert it to integer?? Why default is "" ... In Sql you cant return two different types for the same field Commented Nov 1, 2017 at 14:07
  • [ESSValue] float type Commented Nov 1, 2017 at 14:09

2 Answers 2

1

I think the following pretty much does what you want:

select coalesce(cast(EssValue * 100 as int), 0)

Here is the thinking. The comparison to zero is unimportant, because 0 times any value is going to be zero. The iif() returns an integer (I think) because the "then" argument is an integer; the empty string gets converted to zero.

I'm not 100% certain about the last statements with regard to MS Access, but that is how iif() works in SQL Server.

I should add. Although I don't approve of iif() for conditional expressions (because case is the standard and more powerful), SQL Server does support it. So you could write:

IIf([ESSValue]<>0, cast([ESSValue]*100 as int), '')

Note: As I mentioned earlier, the '' will be converted to 0.

Sign up to request clarification or add additional context in comments.

Comments

0
CASE WHEN ESSValue <> 0 
     THEN CAST(ESSValue * 100 AS INT)
     ELSE NULL
END as fieldname

For case expression the default is NULL if doesn't meet any condition, so you dont really need the ELSE condition

1 Comment

The original statement never returns NULL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.