Access to SQL: IFF function - sql

I have not used access before but I have to convert an access query into SQL so I can write a report in crystal.
The query currently uses the IFF function in its select statement which appears to determine what value will be returned depending on the table's value for a particular column.
So for instance if the value is "CR" it should be returned as "credit" and if it's "NCR" it should be returned as "non-credit"
Can I do something like this in SQL?

Use a CASE expression.
CASE WHEN SomeColumn = 'CR' THEN 'credit'
WHEN SomeColumn = 'NCR' THEN 'non-credit'
END

You can use the CASE statement:
SELECT CASE WHEN [value] = 'CR' THEN 'Credit' WHEN [Value] = 'NCR' THEN 'non-credit' END

In addition to CASE expressions, Oracle database also supports the DECODE() function.
SELECT DECODE(value, 'CR', 'Credit', 'NCR', 'Non-Credit') from table;
There are few cases where it may be useful to use a DECODE() function rather than a CASE expression, but I would recommend sticking with CASE.

Related

access statement convert to Sql

how can I convert to T-Sql this one?
IIf([ESSValue]<>0,Int([ESSValue]*100),"")
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.
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

Check if a string contains only number

I am trying to check if a string contains only valid number in the following format
123.456
123
.356
But it should reject anything that contains non-numbers including double dots. Here are some invalid formats
d123.456
123d
12d3
d.1256
12d.456
12.d12
12.d45d
12.45.56
I have done the following
SELECT CASE WHEN '123.00' NOT LIKE '%[^0-9.]%' THEN 'Valid' ELSE 'Invalid' END
When seems to work except for the case where there is more than one dot in the string.
How can I tweak the regular expression to only allow one dot otherwise return 'Invalid'?
I would suggest try_convert():
select (case when try_convert(col, float) is not null then 'valid' else 'invalid' end)
The one possible downside is exponential format; 1e6 is a valid number for instance.
An alternative is the where approach; you just need more complete logic:
select (case when col like '%[^0-9.]%' then 'invalid'
when col like '%.%.%' then 'invalid'
else 'valid'
end)
There's a sql server built in function:
Select CASE WHEN isnumeric([fieldname]) THEN 'Valid' ELSE 'Invalid" END
If you're not tied to regular expressions, SQL Server has an ISNUMERIC function you can use for this.
ISNUMERIC returns 1 when the input expression evaluates to a valid numeric data type; otherwise it returns 0.
TRY_PARSE will let you compare the input value to any/all numeric datatypes you decide to allow -- for example:
SELECT
TRY_PARSE('123.456' as int) as [int],
TRY_PARSE('123.0' as float) as [float],
TRY_PARSE('d123.456' as int) as [int],
TRY_PARSE('d123.456' as float) as [float]
FWIW -- ISNUMERIC is often suggested, and is certainly the best-sounding function name :-) -- but doesn't work the way most folks seem to expect. (It allows math and currency symbols, etc.)
Alternative solution for NOT LIKE clause is:
where REGEXP_LIKE(column, '^[[:digit:]]+$')

SQL CASE returning two values

I'm writing my first SQL CASE statement and I have done some research on them. Obviously the actual practice is going to be a little different than what I read because of context and things of that nature. I understand HOW they work. I am just having trouble forming mine correctly. Below is my draft of the SQL statement where I am trying to return two values (Either a code value from version A and it's title or a code value from version B and its title). I've been told that you can't return two values in one CASE statment, but I can't figure out how to rewrite this SQL statement to give me all the values that I need. Is there a way to use a CASE within a CASE (as in a CASE statement for each column)?
P.S. When pasting the code I removed the aliases just to make it more concise for the post
SELECT
CASE
WHEN codeVersion = A THEN ACode, Title
ELSE BCode, Title
END
FROM Code.CodeRef
WHERE ACode=#useCode OR BCode=#useCode
A case statement can only return one value. You can easily write what you want as:
SELECT (CASE WHEN codeVersion = 'A' THEN ACode
ELSE BCode
END) as Code, Title
FROM Code.CodeRef
WHERE #useCode in (ACode, BCode);
A case statement can only return a single column. In your scenario, that's all that is needed, as title is used in either outcome:
SELECT
CASE
WHEN codeVersion = "A" THEN ACode,
ELSE BCode
END as Code,
Title
FROM Code.CodeRef
WHERE ACode=#useCode OR BCode=#useCode
If you actually did need to apply the case logic to more than one column, then you'd need to repeat it.
Here is what I normally use:
SELECT
CASE
WHEN codeVersion = "A" THEN 'ACode'
WHEN codeVersion = "B" THEN 'BCode'
ELSE 'Invalid Version'
END as 'Version',
Title
FROM Code.CodeRef
WHERE
CASE
WHEN codeVersion = "A" THEN ACode
WHEN codeVersion = "B" THEN BCode
ELSE 'Invalid Version'
END = 'Acode'
my suggestion uses an alias. note on aliases: unfortunately you can't use the alias 'Version' in a where/group by clause. You have to use the whole case statement again. I believe you can only use an alias in an Order By.

SQL CASE in Query - Odd behavior

Using a Case Statement in a query seems to work fine as long as the only case you want to match to is a string. But if you want to put ">" greater than (date or number), etc, SSMS automatically adds apostrophes around your statement (below) as if it were a string.
How can I get it to accept "greater than date" or "greater than number", etc??
There are two types of CASE expression:
The simple CASE expression compares an expression to a set of simple expressions to determine the result.
The searched CASE expression evaluates a set of Boolean expressions to determine the result.
You want a searched CASE expression. This means that the WHEN should come immediately after the CASE keyword.
CASE WHEN SoldDate <= SubjectDate THEN ...
There are two types of CASE expressions (from msdn):
A Simple CASE compares the specified expression for EQUIVALENCY ONLY
SELECT FieldName CASE WHEN 'Blah' THEN 'Foo' ELSE 'Bah' END
A Searched CASE can do inequalities too but has more verbose syntax:
SELECT CASE WHEN FieldName >= 'Blah' THEN 'Foo' ELSE 'Bah' END
You are trying to use the Simple syntax and need to use the Searched syntax by explicitly writing out the fieldname and comparison for each evaluation.
Your CASE statement syntax is incorrect. WHEN should come directly after CASE
This:
CASE SoldDate WHEN ...
Should be this:
CASE WHEN SoldDate <= SubjectDate THEN SoldFor ELSE EstSalePrice END
Additional Information
Take a look at the MSDN docs for additional examples of CASE syntax.

How to return different strings from a Boolean type in MySQL?

If I have a column set up as Boolean in MySql, a query returns the value as either 0 or 1.
Is it possible to do something like this
SELECT `bool_value` AS "yes" OR "no"
What I mean is, return two different strings based on whether it is true or false.
SELECT CASE WHEN bool_value <> 0 THEN "yes" ELSE "no" END
You need the case statement.
SELECT (CASE WHEN column <> 0 THEN 'yes' ELSE 'no' END) As Value
http://dev.mysql.com/doc/refman/5.0/en/case.html
MySql supports the standdard SQL CASE statement, which other answers use. MySQL also has the shorter, but non-standard IF statement
SELECT IF(bool_value,'Yes','No')
See
MySQL Flow Control Functions: IF