How to convert IF or nested If statement like EXCEL to dynamic SQL Query in SQL server - sql-server-2008

I have came across one pretty problem in which I need to convert the nested if statement (like we used to write in excel) to dynamic SQL query.
I want to convert this
select 'IIF(A>B,A,IIF(B>C,B,C))'
to like this
select 'CASE WHEN A>B THEN A ELSE CASE WHEN B > C THEN B ELSE C END END'.
Why I am doing this I have formula column in the table user can insert formula in that and I need to convert this to SQL statement.
Do anyone has idea how can I do that?
The above is for only example I will be having 150 rows for different formula's entered by user.
Note : Here do not interpret IIF as SQL server IIF, it can be IF.

Assuming that A,B,C are numbers we can do something like this
SELECT
(
Select MAX(cols)
from
(VALUES (ColA), (ColB), (ColC))tbl(cols)
) as greatest
from yourtable

Related

I want to query the content of the json inside column like below

I am saving JSON data my SQL server 2016 table and I want to query the table by applying where clause on the JSON like below.
select c.customer_details.name.fullName
from j_customer c
where c.customer_details.name.fullName like '%Gopi%';
this is possible in oracle but in mssql it gives the error like below
Cannot call methods on nvarchar(max).
After going thru internet finally I found out that there is a method called JSON_VALUE() which should be used for finding the content of the json. The syntax for mssql is like this.
select *
from j_customer where JSON_VALUE(c.customer_details, '$.name.fullName') = 'C';
As far as I understood it, you have stored your JSON data into the table using a query like below
declare #json nvarchar(max) = '"Customer_details":{"name":{"fullName":"Dhruv Joshi"}}'
INSERT INTO j_customer
SELECT *
FROM OPENJSON(#json)
WITH (---some columns
fullName nvarchar(max) '$.Customer_details.name.fullName'
)
In such cases you can simply query your table like below
select c.fullName
from j_customer c
where c.fullName like '%Gopi%';
In SQL the fully qualified SQL column name usually is like [DatabaseName].[schema].[tablename].[columnname], so here in WHERE clause SQL interprets c.customer_details.name.fullName as c.customer_details a column as c is a table alias. And then name.fullname looks like a method call on the column name which generates the error.

Running a SQL SELECT statement against a MYSQL column of SET type

I'm trying to run a SQL SELECT statement against a column that is of type SET. The table is called myTable and the columns in myTable are called base_props and names. The base_props column is of type SET. The values in base_prop are vb,nt, cnt,poss and loc. So I would like to SELECT entries from the column 'name' where base_props have both the values, vb and poss. The results I'm looking to get may have values other than just vb and poss. So to be clear I would like to select all entries that have the values vb and poss regardless if they have other values as well. I've tried the following SQL queries but I can't get the desired results.
SELECT name from myTable WHERE base_props = 'vb' AND base_props = 'poss'
That query returns an empty result set. I've tried using FIND_IN_SET() and IN() but I couldn't get anywhere with that. I've written SQL statements before but never had to deal with columns that are type SET. Any help is appreciated.
The only thing I can come up with is using the LIKE keyword:
SELECT name FROM myTable WHERE (base_props LIKE '%vb%' AND base_props LIKE '%poss%');
This will make sure both vb and cnt are in the base_props column. Of course you can use cnt, nt and loc in there, or any number of base_props values in the sql, just add more AND statements.
OR as a deleted answer by samitha pointed out, you can use FIND_IN_SET:
SELECT name from myTable WHERE FIND_IN_SET('vb', base_props) AND FIND_IN_SET('poss', base_props);
Comment (by spencer7593): "both of these work, but there is a slight difference. The LIKE operator will actually match any member that includes the search string anywhere in a term; the FIND_IN_SET function will only match an exact member. It's also possible to search for members in set by the order they appear in the SET definition, using the MySQL BITAND operator: for example, to match the 1st and 4th members of the set: WHERE base_props & 1 AND base_props & 8". So for example, if you have 'a' and 'aaa' in your set, then using the LIKE "%a%" method will also return rows containing 'aaa'.
Conclusion: use the FIND_IN_SET solution since it will work for all cases.
FIND_IN_SET return index, Try this
SELECT name from myTable WHERE FIND_IN_SET(base_props, 'vb') > 0 AND
FIND_IN_SET(base_props, 'poss') > 0

Stored procedure to execute a query and return selected values if the query returns only 1 result

So my query is the following, which may return many results:
SELECT P_CODE, NAME FROM TEST.dbo.PEOPLE
WHERE NAME LIKE '%JA%'
AND P_CODE LIKE '%003%'
AND DOB LIKE '%1958%'
AND HKID = ''
AND (MOBILE LIKE '%28%' OR TEL LIKE '%28%')
I would like to integrate this into a Stored Procedure (or View?) so that it will only return a result if the query results in exactly 1 row. If there's 0 or > 1, then it should return no results.
If you just want to return an empty resultset in cases other than 1:
;WITH x AS
(
SELECT P_CODE, NAME, c = COUNT(*) OVER()
FROM TEST.dbo.PEOPLE
WHERE NAME LIKE '%JA%'
AND P_CODE LIKE '%003%'
AND DOB LIKE '%1958%'
AND HKID = ''
AND (MOBILE LIKE '%28%' OR TEL LIKE '%28%')
)
SELECT P_CODE, NAME FROM x WHERE c = 1;
Otherwise, you'll have to run the query twice (or dump the results to intermediate storage, such as a #temp table) - once to get the count, and once to decide based on the count whether to run the SELECT or not.
Effectively you want something akin to FirstOrDefault() from the Linq-to-SQL implementation but done on the server-side which means you will need to execute the query in a stored procedure, dumping the results into a temp table variable and then access ##ROWCOUNT afterwards to get the number of rows that were returned and then decide whether or not to forward the results on to the caller. If you do, be sure to use TOP 1 in the query from the temp table so that you only get a single result out as you desire.
UPDATE:
I described the alternate solution from what Aaron describes in his answer (which I like better).
Removed unnecessary TOP specifier in solution specification.

SQL MS Access Query to Get Text Value from Form Text Box

I have a 'very' long query written through MS Access with lot of unions (around 30) and other dynamic variables.
Now, I need to insert a 'Where' clause in all those unions, and the where clause would be constant and generated through a form (selecting some options, I have written a vb code to create a string containing the where clause.
Is there any way I could use some variables in the SQL query so that the variable gets the where clause from the text result of the form action?
Illustrative: My Query
Select a,b,c,d from
(select x as a,b,c,d from abc <where clause> union
select y as a,b,c,d from abc <where clause> union
select z as a,b,c,d from abc <where clause> union
select p as a,b,c,d from abc <where clause>)
order by a
The should be populated with the text box result which would look something like 'where b=1'
I thought of writing it in the VB code itself, but to numerous linebreaks and chaning nature of the queries, I do not want to manually keep adding " & _ at the end of each line.
Any help would be greatly appreciated.
Regards,
Navs
You can write the WHERE clause like this:
WHERE B=[Forms]![FormName]![FieldName]
so it gets the value from the form. But, if i understand it correctly, in the form there's not just the condition, but there's the whole where clause, that could be 'where b=1' but it could also be 'where c=4' or 'where a=2 and b=6'?
I think there's not a perfect solution to this, you can't use plain Access but you have to write some VBA code, and you can generate the query string with something like this:
$src_query = "Select a,b,c from abc <where clause> union select ..."
$src_query = Replace($src_query,
"<where clause>",
[Forms]![FormName]![WhereClause])
then you just have to modify the query programmatically:
CurrentDb.QueryDefs("QueryName").SQL = $src_query
and now your query is ready to be executed.
You could put the Where clauses in a separate query, and not alter the original
Select *
from MyQuery
Where (a=1 and b=7) or (c=3 and d="StackOverflow")
With little homework, I found how to assign the contents of the text file to a variable. I was then able to change the variable with the code snippets 'Fthiella' had given.

MySQL select with subquery having replace

So I have a data with format like ;1;;2; and then I need to use this number in a query so I thought I'd convert it to 1,2 and use that in a IN condition. In my table, the result should return 2 rows but instead it is returning only 1 row.
My query is like this. The subquery return 1,2 with no problem but only 1 row is retrieve.
select *
from wt_lists
where id IN ((select replace (replace(sendto, ';;',','),';','')
from wt_stats where statsid IN (1)))
But when I try it with this. It returns the correct result, which in my case is 2 rows.
select *
from wt_lists
where id IN (1,2)
What am I missing here?
Comma delimited strings need to be explicitly defined in the query in order to be used in the IN clause - there's countless examples on SO where people need to use dynamic SQL to incorporate user submitted comma delimited strings.
That said, I have a solution using the FIND_IN_SET function:
SELECT DISTINCT wl.*
FROM WT_LISTS wl
JOIN (SELECT REPLACE(REPLACE(ws.sendto, ';;',','),';','') AS ids
FROM WT_STATS ws
WHERE ws.statsid = 1) x ON FIND_IN_SET(wl.id, x.ids) > 0
You are replacing the string:
';1;;2;'
To:
'1,2'
So, you SQL query looks like:
select * from wt_lists where id IN ('1,2') from wt_stats where statsid IN (1)
To use IN clause you need select different values in different rows.
I found this store procedure that does exactly what you need.
http://kedar.nitty-witty.com/blog/mysql-stored-procedure-split-delimited-string-into-rows/
I have not tested, but it is the way.
Obs: Like David said in the comments above, parsing the data in your application is a better way to do this.