MS-access Pass-Through Query to SQL Server 2008 Adding Prefix - sql-server-2008

I am hoping someone can point me in the right direction with this. I am relatively new to using pass-through queries but think I have a grip of the basics, I have come across a stumbling block however when trying to add a prefix to query result
I have a select query which includes a line to convert a date into the financial year i.e 01/01/2018 would return 2017 as the result using the code below:
[Created FY] = (CASE WHEN Month(create_date) IN (1, 2, 3) THEN DatePart(year,create_date)-1 ELSE DatePart(year,create_date) END),
I would like to add a prefix to the result so that it would read FY2017. The pass-through is running on SQL Server 2008. I have researched and so far have not come up with any resolutions.
Any help with this conundrum would be greatly appreciated.

+ concatenates strings, but numbers must be converted first.
As there is a MONTH() function, there is YEAR(), to simplify the code a bit.
So:
[Created FY] = 'FY' + CONVERT(char(4),
(CASE WHEN MONTH(create_date) IN (1, 2, 3)
THEN YEAR(create_date)-1
ELSE YEAR(create_date) END)
),

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))

When case in sql does not support in cassandra cql

I was using mysql earlier. Now for specific reason had to move to cassandra. Now I am facing some incompatibility issue with my mysql queries in cassandra cql . For example when case in mysql it seems does not support in cql . How to make this sql work in cassandra
SELECT age, sum(case when age>0 and age<=10 then 1 else 0 end) as x0,
sum(case when age>10 and age<=20 then 1 else 0 end) as x1
FROM user_table where location_id=24
I am getting the following error:
SyntaxException: line 1:32 no viable alternative at input 'when' (... age, sum([case] when...)
Thanks in advance.
It's not possible at all to map this query to cql. Things are different with cassandra.
When designing your system you should have in mind, what queries you have to run and design it accordingly. This is given by the fact that you can only access the data in a way like it was written.
Last but not least the part where you are creating sums, is probably something which you would like to do on the client side.
Best

What does the third parameter in CONVERT() do?

Query
Get the Joining year,Joining Month and Joining Date from employee table
This is my query which I have to perform. For this I write the following script:
select
SUBSTRING (convert(varchar,joining_date,103),7,4) ,
SUBSTRING (convert(varchar,joining_date,100),1,3) ,
SUBSTRING (convert(varchar,joining_date,100),5,2)
from
EMPLOYEE
The result is: http://d.pr/i/vObI
But when I changed convert(varchar,joining_date,100) to convert(varchar,joining_date,101)
Result is like this: http://d.pr/i/G5fZ
Can anyone please explain what this parameter means?
There are several different ways that you can format date using convert(varchar.... These are well documented on the MSDN site or different sites online.
Using convert(varchar..., date, 100) places the date in the format:
mon dd yyyy hh:mmAM (or PM)
May 10 2013 12:55PM
Using convert(varchar...date, 101) puts the date in the format:
mm/dd/yyyy
05/10/2013
See Demo
My suggestion would be whenever you implement these conversions, be sure to give a length on the varchar(10), etc.
Based on what it looks like you are returning, you can eliminate some of the convert/substring statements that you are using and implement some other functions to get the same result:
select year(joining_date) as [year] ,
convert(varchar(3),joining_date,100) as [month] ,
day(joining_date) as [day]
from EMPLOYEE
I think there is a better approach to split and get datetime parts using DATEPART:
select DATEPART(YEAR, [joining_date]),
DATEPART(MONTH, [joining_date]),
DATEPART(DAY, [joining_date]) from EMPLOYEE
or if you are interested for example in names use DATENAME:
select DATEPART(YEAR, [joining_date]),
DATENAME(MONTH, [joining_date]),
DATEPART(DAY, [joining_date]) from EMPLOYEE
However according to MSDN for DATENAME: "The return value depends on the language environment set by using SET LANGUAGE".
Regarding your initial question - these parameters are basically styles or I would call them Regional specific codes as described here, and you can just run the different queries against the DB and you will see the strings returned - and you will figure why you get unexpected results. For more infos refer to MSDN: CAST and CONVERT

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.