I have simple query like
SELECT * FROM temp t WHERE t.id IN (:IDs)
When executed, it prompts me (Oracle SQL Developer) for entering value if IDs variable.
When I enter for example 169, everything runs smoothly, but when I try to enter multiple IDs, like 169,170,171, I get error Invalid Number even while putting it into ''.
I'm used to working with MS SQL and MySQL, so this is little confusing to me.
Anyone any suggestions.
The problem is the varying-IN list. In SQL Developer, when you are prompted to enter the value for the bind variable, you are simple passing it as 169,170,171 which it is not considering as a set of values.
What you could do is, have multiple binds -
SELECT * FROM temp t WHERE t.id IN (:ID1, :ID2)
When prompted, enter value for each bind.
UPDATE Alright, if the above solution looks ugly, then I would prefer the below solution -
WITH DATA AS
(SELECT to_number(trim(regexp_substr(:ids, '[^,]+', 1, LEVEL))) ids
FROM dual
CONNECT BY instr(:ids, ',', 1, LEVEL - 1) > 0
)
SELECT * FROM temp t WHERE it.d IN
(SELECT ids FROM data
)
/
If you put them into ", you get error. Oracle doesn't accept ". You should use just numbers without ".
i.e: (169,170,171,...)
You can define a substitution variable as an array like so:
define IDS = (169,170,171);
and then use it like so:
SELECT * FROM temp t WHERE t.id IN &IDS;
Related
As an example below, I am trying to figure out how I can use INTO (I have out parameters defined in a procedure I have made) when I am doing a SELECT statement that involves AS:
SELECT name, COUNT(addresses) AS TotalAddresses INTO outVar1, SUM(salary + tax) AS test INTO outVar2
FROM ...
Unfortunately, the compiler does not like this and well I have tried searching online, but no luck.
Use one into clause:
SELECT COUNT(addresses) AS TotalAddresses, SUM(salary + tax) AS test
INTO outVar1, outVar2
FROM ...
The results are going into variables. There is no need to select NAME if it is not going to a variable.
I want to search a string in multiple columns to check if it exists in any.
I found a solution for it here
The answer by Thorsten is short but that is a solution for mysql server not for SQL Server.
So I would like to apply similar query in SQL Server.
Here is the query suggested by Thorsten.
Select *
from tblClients
WHERE name || surname LIKE '%john%'
I tried it as
/* This returns nothing */
Select *
from Items
Where ISNULL(Code, '') + ISNULL(Code1, '') = '6922896068701';
Go
/* This generate error Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '|'.
I also used this one in mysql but it does not show the exact match.
*/
Select *
from Items
WHERE Code || Code1 = '6922896068701';
Go
/* This generate error Msg 4145, Level 15, State 1, Line 5
An expression of non-boolean type specified in a context where a condition is expected, near 'Or'. */
Select *
from Items
WHERE Code Or Code1 = '6922896068701';
Go
Is it really possible in SQL Server?
Note: The answer by J__ works accurately in the upper Question link but I want the comparison string to be entered once for all columns where I look for it like Thorsten.
Actually I think that separate logical checks in the WHERE clause for each column is the way to go here. If you can't do that for some reason, consider using a WHERE IN (...) clause:
SELECT *
FROM Items
WHERE '6922896068701' IN (Code, Code1);
If instead you want LIKE logic, then it gets tricky. If you knew that the matching codes would always consist of numbers/letters, then you could try:
SELECT *
FROM Items
WHERE ',' + Code + ',' + Code1 + ',' LIKE '%,6922896068701,%';
I would recommend doing the two comparisons separately:
WHERE name LIKE '%john%' OR
surname LIKE '%john%'
Unless you specifically want to find times when the names are combined, such as "Maryjoh" "Needlebaum" or whatever.
It is generally better to focus on one column at a time, because that helps the optimizer.
For MS SQL this may work;
Select *
from Items
WHERE Code = '6922896068701' Or Code1 = '6922896068701'
I'm having some trouble understanding WHY a select statement isn't working in a query I'm making.
I've got the SELECT and FROM lines functioning. With just those, ALL results from my selected table are displayed - 517 or so
What I want to do is display results based on a pattern using LIKE - What I have so far
SELECT *
FROM Tbl_ServiceRequestMatrix
WHERE Tbl_ServiceRequestMatrix.[Application/Form] LIKE 'P%';
This returns 0 results - despite the fact that the column selected DOES have entries that start with 'P'
I also tried utilising brackets, see if that was the issue - still displays 0 results:
SELECT *
FROM Tbl_ServiceRequestMatrix
WHERE ((Tbl_ServiceRequestMatrix.[Application/Form])='p%');
Can any one help me understand why my WHERE ** LIKE statement is causing 0 results to be displayed?
The wildcard character in MS Access is (by default) * instead of %:
WHERE Tbl_ServiceRequestMatrix.[Application/Form] LIKE "P*"
LIKE Statement has different parameters in different sql languages.
In MS Access you need * Instead of % in LIKE Statement.
I have a requirement for a query. It needs to select every number from a list that IS NOT present in a column. Currently, I have this working fine. This query returns every number between 1833 and 2000 that is not present in the ATTR table.
SELECT LEVEL + 1833
FROM DUAL
CONNECT BY LEVEL <= (2000 - 1833)
MINUS
SELECT ID_TX
FROM ATTR
WHERE ID_TX BETWEEN 1834 AND 2000;
What I want to do is make this as user-friendly as possible. To do that, I can enter two variables, a STARTING_ID and LIST_LENGTH. Now my query looks like this.
SELECT LEVEL + &STARTING_ID
FROM DUAL
CONNECT BY LEVEL <= &LIST_LENGTH
MINUS
SELECT ID_TX
FROM ATTR
WHERE ID_TX BETWEEN &STARTING_ID AND &STARTING_ID + &LIST_LENGTH;
At first, I was using &&, but then I could only use this query once. UNDEFINE couldn't be placed in the code block, and wasn't cleaning my variables anyway. Now my issue is that it considers each & variable to be different, so it's making the user enter 5 variables instead of 2.
How do I make it where I'm still using temporary variables (with or without the popup to enter the variable), but the person running the query only has to enter two values 1833 and 67?
A bit of a fudge but if you want the prompt for substitution variables then you can use bind variables but just populate them using substitution variables like this:
(Run it as a script using F5 and not as a statement using Ctrl+Enter)
VARIABLE list_length NUMBER;
VARIABLE start_value NUMBER;
BEGIN
:list_length := ≪
:start_value := &sv;
END;
/
SELECT LEVEL + :start_value
FROM DUAL
CONNECT BY LEVEL <= :list_length
MINUS
SELECT ID_TX
FROM ATTR
WHERE ID_TX BETWEEN :start_value + 1 AND :start_value + :list_length;
Otherwise, just use bind variables (i.e. the query at the bottom of the script).
How about using : as prompt ?
SELECT LEVEL + :STARTING_ID
FROM DUAL
CONNECT BY LEVEL <= :LIST_LENGTH
MINUS
SELECT ID_TX
FROM ATTR
WHERE ID_TX BETWEEN :STARTING_ID AND :STARTING_ID + :LIST_LENGTH;
This employs the concept of bind variables. Thus, user could enter the necessary values and proceed.
Try to use '&&' instead of one '&'.
If a single ampersand prefix is used with an undefined variable, the value you enter at the prompt is not stored.So once the alue is substituted,variable is discarded and remains undefined. If the variable is referenced twice, even in the same statement, then you are prompted twice.
If you use '&&', the value is stored and hence you will be prompted only one.
I have been working with a guy to finish off a rather ingenious means by which to extract information on packages installed by SCCM 2012 vs the built-in inventoried "Programs and Features". The last piece is extracting the PACKAGEID from registry strings that have been inventoried in the aforementioned process. Each string looks like this (the target "PACKAGEID" is identified in bold:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution\Execution History\System\ LAB00003 \ac80c725-7dc7-11e5-9bc8-000c292d4525
As stated, i am not the genius behind any of this but i wanted to understand why i am getting the following error:
Msg 537, Level 16, State 3, Line 1
Invalid length parameter passed to the LEFT or SUBSTRING function.
From the following query:
SELECT
DataType0,
KeyPath0,
Name0,
Value0,
(
SELECT SUBSTRING(KeyPath0, LEN(LEFT(KeyPath0, CHARINDEX ('\System\', KeyPath0))) + 1, LEN(KeyPath0) - LEN(LEFT(KeyPath0, CHARINDEX ('\System\', KeyPath0))) - LEN(RIGHT(KeyPath0, LEN(KeyPath0) - CHARINDEX ('\', KeyPath0))) - 1)
) as "Package ID"
FROM
dbo.v_GS_Registry_Values0
i verified that the the dbo.v_GS_Registry_Values0 view does indeed have the reg key string in it via select * from SCCM_Ext.vex_GS_Registry_Values0 but despite tons of searching, my simple sql mind cannot make sense of the query and its use of LEN & CHARINDEX.
Totally throwing myself at the mercy of this site in hopes i could get not only the resolution to this but also a better understanding of why this is happening and how the query works.
if there is ANY additional information i could provide please let me know.
If you're just trying to get the next string after \System\ you're SQL is quite complex, you can do it just with this:
SELECT left (Y.S, charindex ('\', Y.S) - 1)
from Table1
outer apply (
select CHARINDEX ('\System\', KeyPath0) as pos
) X
outer apply (
select substring (KeyPath0, X.pos + 8, 9999) as S
) Y
Example in SQL Fiddle
The first outer apply finds the \System\ the second gets the rest of the string (assuming max path is 9999 characters) and then just take the part before the next \ in the actual select.