Read multiple columns from single table based on our input string - sql

I want to select multiple columns from single table based on my given input string in sql select statement.
Example :
If input="table_medical" i want to select the columns like medi_col1,medi_col2,medi_col2
If input="table_pharmacy" i want to select the columns like medi_phar1,medi_phar2,medi_phar1
sql("select
case when $input="table_medical" then medi_col1) //like this
please help me to complete this.

If you want this in a single query, then the same number of columns is needed -- and have compatible types.
One method uses union all:
select medi_col1, medi_col2, medi_col2
from t
where #input = 'table_medical'
union all
select medi_phar1, medi_phar2, medi_phar1
from t
where #input = 'table_pharmacy';

SET #input="table_medical";
SELECT
CASE WHEN #input="table_medical" THEN medi_col1 ELSE medi_phar1 END as medi_col1,
CASE WHEN #input="table_medical" THEN medi_col2 ELSE medi_phar2 END as medi_col2
CASE WHEN #input="table_medical" THEN medi_col3 ELSE medi_phar3 END as col3
FROM MyTable
Data types for medi_col1 and medi_phar1 needs to be same (and for the rest of columns too)

Related

Check if the first or second condition exists

I have a little problem selecting queries, namely when I want to check the first condition with the code below
select * from VL_Faktura_Queue where FAK_KundenNr=127849 AND (FAK_BoMatNr LIKE '%verk%' AND FAK_VerrechnetBis ='0001-01-01')
, it shows me one position, but when I add a condition where I want to check if there is a FAK_KundenNr with FAK_BomatNr LIKE '% Verk%' OR FAK_BoMatNr Like 'Zus%' also throws me different values that do not fall under FAK_KundenNr = 127849, as I can easily check that it returns my values for this KundenNr, where there is 1 OR 2 condition.
this is my query:
select * from VL_Faktura_Queue where FAK_KundenNr=127849
AND (FAK_BoMatNr LIKE '%verk%' AND FAK_VerrechnetBis ='0001-01-01') --this would be the first condition
or FAK_BoMatNr like 'Zus%' --and this the second condition
This is the individual selection I should get but in one query at the end
so my question is how can i get in one query select from these two query from the picture, thanks everyone for the help
Your parentheses are not sufficient. AND has precedence over OR, so you have FAK_KundenNr = 127849 AND (<first condition)> OR FAK_BoMatNr like 'Zus%'.
SELECT *
FROM VL_Faktura_Queue
WHERE FAK_KundenNr = 127849
AND
(
(FAK_BoMatNr LIKE '%verk%' AND FAK_VerrechnetBis = '0001-01-01')
or
FAK_BoMatNr LIKE 'Zus%'
);
In your requirement, you need to combine the "AND" operator with other logical "OR" operator.
SELECT *
FROM VL_Faktura_Queue
WHERE
(
( FAK_BoMatNr LIKE '%verk%'
AND FAK_VerrechnetBis = '0001-01-01'
) -- 1st Condition
or
(FAK_BoMatNr LIKE 'Zus%') -- 2nd Condition
)
AND FAK_KundenNr = 127849;
Please check if this solution is working for you.

How to display the whole number only if it starts with two characters otherwise leave it blank. SQL query

I need to display the whole number in a field if it starts with "AB" otherwise do not show/display the number.
Your question is missing code of how you display this (since you wrote you need to display it to the field) so i can't answer you with actual code but here is solution.
If you want to select only rows which column1 starts with AB then use LIKE function. So condition at selecting command is Select * from yourtable where column1 LIKE 'AB%'
If you already selected and displayed data, let's say in datagridview, and you want to fill textbox with string that contains AB then you would go through all rows at specific column and look for it with string.Contains("AB");
So basically you put this command in foreach loop and you have it.
I was wrong. You can use a LIKE, just not in the WHERE clause.
;WITH testdata AS (
SELECT 'aw12354' AS val UNION ALL
SELECT 'a12b344' UNION ALL
SELECT 'AB11111' UNION ALL
SELECT '11AB111' UNION ALL
SELECT '11111AB' UNION ALL
SELECT 'ab22222'
)
SELECT
CASE WHEN val LIKE 'AB%' THEN val ELSE NULL END AS valFull
, CASE WHEN val LIKE 'AB%' THEN SUBSTRING(val,3,len(val)) ELSE NULL END AS valNums
FROM testdata
;
You can also use CLR to build a regex solution, but that is a LOT more involved.

How to check the last two digits?

SUBBIS
SUBB1D
SUBBD3
SUBB12
In above values, how can I check the last two digits (IS, 1D, D3, 12) are numbers using a sql code?
Do you mean to fetch those values? You can do that with like:
where column like '%[0-9][0-9]'
If you need to ensure that the values always end with 2 numbers, you can do it with similar check constraint.
To check the last two digits are numbers in column, you can use the following script.
... WHERE ISNUMERIC(RIGHT(your_column,2)) = 1
Here RIGHT(your_column,2) will return the last two digits from the string.
or
SELECT ISNUMERIC(RIGHT(your_column,2))
will return 1 (if its number) otherwise 0
You can do it this way:
SELECT MyId,
ISNUMERIC(RIGHT(MyColumn,2)) -- your column to check last 2 (if numeric)
FROM (
----- replace with your table
SELECT 1 MyId,'SUBBIS' MyColumn UNION SELECT 2,'SUBB1D' UNION
SELECT 3,'SUBBD3' UNION SELECT 4,'SUBB12'
----- replace with your table
) A
Hope it helps. :)
You can use like and _ "underscore" to get last one digits record columName
SELECT columName FROM sub WHERE columName LIKE "SUBB__" ;
Record :
columName
SUBBIS
SUBB1D
SUBBD3
SUBB12
SUBBBA

Is using sequence in db2 statements possible?

I am trying to execute following statement in DB2.
This works well.
SELECT NEXT VALUE FOR SCPYMNT.REM_QUERY_NO_SEQ
FROM sysibm.sysdummy1
However, this throws a database error.
SELECT (
CASE WHEN PYMT_SYS = 1 THEN NEXT VALUE FOR SCPYMNT.REM_QUERY_NO_SEQ
WHEN PYMT_SYS = 2 THEN 'dummy'
else 'dummy'
END )
FROM sysibm.sysdummy1
So Db2 gives the error below.
Category Timestamp Message
Statusbar 18.04.2016 11:47:39 DB2 Database Error: ERROR [428F9] [IBM][DB2] SQL0348N "NEXT VALUE FOR SCPYMNT.REM_QUERY_NO_SEQ" cannot be specified in this context. SQLSTATE=428F9
It seems to me there is not a syntax error.Does Db2 not let such queries that consists of case conditions and sequence?
#MichaelTiefenbacher,I put select examples as a demonstration.(What I am really trying to achieve is something like below.
SELECT NAME, QUERYNO
FROM FINAL TABLE (INSERT INTO EMPSAMP (NAME, SALARY, QUERYNO)
VALUES('Mary Smith', 35000.00,
CASE WHEN PYMT_SYS = 1 THEN NEXT VALUE FOR REM_SEQ
CASE WHEN PYMT_SYS = 2 NEXT VALUE FOR EFT_SEQ
));
I think question is more clearer now.
Sequences can be used to generate unique keys or numbers when inserting data into tables.
They are not used to generate unique numbers when selecting data.
For that you could either retrieve the field from the table where you used the sequence at insert time or you can use row_number() in the SELECT.
It also would be helpful to tell a little more what you want to achieve.
I found out that the answer is "No" according to IBM documentation.
NEXT VALUE expressions cannot be specified in the following contexts:
CASE expression
Here is the link
Insert from a union of selects:
SELECT NAME, QUERYNO
FROM FINAL TABLE
(
INSERT INTO EMPSAMP
SELECT
'Mary Smith', 35000.00, NEXT VALUE FOR REM_SEQ
FROM SYSIBM.SYSDUMMY1
WHERE PYMT_SYS = 1
UNION ALL
'Mary Smith', 35000.00, NEXT VALUE FOR EFT_SEQ
FROM SYSIBM.SYSDUMMY1
WHERE PYMT_SYS = 2
)

How to quickly compare many strings?

In SQL Server, I have a string column that contains numbers. Each entry I need is only one number so no parsing is needed. I need some way to find all rows that contain numbers from 400 to 450. Instead of doing:
...where my stringcolumn like '%400%' or stringcolumn like '%401%' or stringcolumn like '%402%' or ...
is there a better that can save on some typing?
There are also other values in these rows such as: '5335154', test4559#me.com', '555-555-5555'. Filtering those out will need to be taken into account.
...where stringcolumn like '4[0-4][0-9]' OR stringcolumn = '450'
You don't need the wildcard if you want to restrict to 3 digits.
Use regex to accomplish this.
...where stringcolumn like '4[0-4][0-9]' OR stringcolumn like '450'
one way
WHERE Column like '%4[0-4][09]%'
OR Column LIKE '%500%'
keep in mind that this will pick anything with the number in it, so 5000 will be returned as well
I would do the following:
select t.*
from (select t.*,
(case when charindex('4', col) > 0
then substrint(col, charindex('4', col), charindex('4', col) + 2)
end) as col4xx
from t
) t
where (case when isnumeric(col4xx) = 1
then (case when cast(col4xx as int) between 400 and 450 then 'true'
end)
end) = 'true'
I'm not a fan of having case statements in WHERE clauses. However, to ensure conversion to a number, this is needed (or the conversion could become a column in another subquery). Note that the following is not equivalent:
where col4xx between '400' and '450'
Since the string '44A' would match.