How can I achieve this without Case Statement in SQL Server - sql-server-2008

is there any alternative to case statement I want achieve below
*select
case when Etype=1 then 'Earthquake'
else 'Normal'
end
as Etype

something like this will do, although not sure what's the benefit of not using case:
select substring('EarthquakeNormal',power(sign(Etype-1),2)*11,11)
I'm assuming Etype is always >=0 if is >0 then the power is not required.
Also, please note that this solution is for SQL Server 2008 but from SQL Server 2012 onward there are at least another 3 better ways of doing it. IIF , choose and even parsename.

Related

Using CASE WHEN Expression with where condition SQL Server

I have my new Spring Boot project with SQL Server and I need to replace my MySQL native query on the Repository method in my old project with SQL Server native query. It's a complex query with the case when expression in where condition. When I try testing that query in SQL Server Management Studio it shows errors like the image below.
enter image description here
And here's my old native query use with MySQL on the Repository method I want to replace it with SQL Server
enter image description here
Please help me to find the solution.
Thank you in advance!!
This is what you have and what you should have posted as text within your question. As text it becomes searchable and copyable by people trying to help YOU.
case when #num = 1 then p.merchant_name = #query else 1=1 end
CASE is an expression in TSQL. It is not a control-of-flow construct like it is in many other languages. To use an "optional" filter, you need to construct a boolean expression using CASE which handles the "optional" attribute correctly. Often this is done with a bit more complexity using CASE like this:
case when #num = 1 and p.merchant_name <> #query then 0 else 1 end = 1
So here, CASE is used to return a value that can be tested in a comparison. There is no magic in using 0 or 1. Use any values of any type.
When #num is 1 and the values do NOT match, the THEN branch (0) is returned.
When #num is 1 and the values match, the ELSE branch (1) is returned.
When #num is anything but 1, the ELSE branch (1) is returned.
So when the CASE expression returns 0 (really - anything but 1), the row is ignored (removed from the resultset).
Given that your query is actually constructed in an application, you should considering dynamically building the query and adding parameters as needed. That will likely generate a more efficient query that can be better optimized by the database engine. Alternatively you can review this kitchen sink discussion and Erland's discussion of dynamic search conditions. TBH it looks like someone used #num as a kludge to avoid adding parameters for the eight specific filter values. If I want to filter on both merchant name and store name, I can't with this approach.

SQL Sum Case Statement - group by case

I'm simply trying to write an sql query to sum a group of fields.
I have 2 columns, one of which is Account Name, the other is Amount
I'd only like to sum two of the account names:
=sum(case when [Account Name] like Salaries or 'Other Income', Fields!Amount.Value)
the following works fine,
=sum(Fields!Amount.Value)
but I'm trying to narrow down the number of account names that are included in the sum. I'm trying to figure out how to use a case statement then group by that case statement.
I'm going to take a stab in the dark here because I think from your example that you're using SQL Server Reporting Services (SSRS), and the syntax is quite different to T-SQL or ANSI SQL, which is what most people think of when you say "SQL".
If that's the case, and I have no experience with this so this may be completely wrong, I'm guessing you might need to try something using either iif or switch like this:
=sum(iif(Fields![Account Name].Value = 'Salaries'
or Fields![Account Name].Value = 'Other Income', Fields!Amount.Value, 0))

How to write two case statements with in the single case in sql

I have tried a lot. But its showing the error. Any 1 please help me.
My Query (Please see in Image)
enter image description here
You cannot nest case statements, each one needs to stand alone. And you cannot give them different names.
You can try to trick with Coalesce, like
SELECT COALESCE
(
CASE WHEN condition1 THEN calculation1 ELSE calcelse1 END,
CASE WHEN condition2 THEN calculation2 ELSE calcelse2 END,
etc...
)
but I think it wont solve your problem directly.
IMHO you need to enhanced each of your case statements. Or you are going to implement this with a pivot statement. And on first sight, you need to think about redesigning your data model ...

Converting boolean to positive or negative sign in MySQL

In quite a few of my MySQL queries I have a piece of computation
(CAST(return_item as SIGNED)*2-1)
Essentially that piece of code converts true => 1 and false => -1, and it works great!
What I don't like about it is its readability in the SQL statements.
Is there a cleaner way to write this statement making it clearer on its intent?
return_item is BIT(1) which represents a boolean value in the system.
If you want to make your intent clear, there is hardly a better way than using a conditional:
IF(return_item, 1, -1)
The most standard SQL way to do this would be a CASE statement:
CASE return_item WHEN 1 THEN 1 ELSE -1 END
This is pretty readable, and will run in and be familiar to users of other database products.
Since the type in question is usable as a boolean, this also works, but would be less portable (Postgres's boolean type would work, but MS SQL Server's bit would not):
CASE WHEN return_item THEN 1 ELSE -1 END
try this:
if(return_item=true, 1, -1)
how about SELECT POWER(-1, 1-SIGN(return_item)) ?

How do SQL sServer and Crystal Reports link together for case...then 0?

I'm new to CR & SQL Server. I'm trying to understand how you can code a select case statement with "case some-condition then 0" and "case some-other-condition then 1" and have it connect in Crystal reports....if I want a particular year of data to show, how do I use this particular style of case statement in the SQL Server coding and how do I get it to connect to variable names in CR?
Your question is a little bare on specifics but hopefully this helps. In sqlserver a case statement works like this. You can get a lot more detail via books online. You didn't specify which version of SQL Server so I linked to 2008 R2.
SELECT
SomeColumn,
SomeOtherColumn,
(CASE MyColumn
WHEN 'First' THEN 1
WHEN 'Second' THEN 2
WHEN 'Third' THEN 3
ELSE 4
END) AS MyNewColumn /* the name can be same as original column if you want */
FROM MyTable
As for mapping the value to Crystal you can treat MyNewColumn just like any other column name with regards to comparing to parameters etc.