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'
Related
When I run my query in management studio it works fine, but in a stream analytics job it throws an error: Query compilation error: Invalid column name: 'afkorting'. Column with such name does not exist..
I downloaded the input tables to check if something went wrong with uploading, but that file does have that column name (and I double checked for capital letters, miswriting etc), so how can I fix this?
This is my query:
; WITH Check AS
(
SELECT afkorting, *
FROM Reizen RE
LEFT JOIN Gegevens AP
ON RE.ID = AP.code
)
SELECT *
FROM Check CH
JOIN Model VM
ON CH.afkorting = VM.Station
WHERE VM.h_station = VM.v_station
AND DATEPART(hour, CH.MsgReportDate) = VM.start_uur
AND (DATEPART(minute, CH.MsgReportDate) BETWEEN VM.start_minuut AND VM.eind_minuut)
AND DATEPART(weekday, CH.MsgReportDate) = VM.weekdag
Hope someone can help me!
*PROBLEM SOLVED: you need to give in all columnnames, so not SELECT * but SELECT column1, column2 and use the given prefixes of the table, in my case: AP.column1, RE.column2 etc*
Just summarize all comments above for resolving the issue, I did some testing for Stream Query language elements WITH, SELECT & JOIN. Here is my result list for the issue.
Without JOIN, using column names with symbol * in the WITH scope is correct for executing on ASA.
With JOIN, it's necessary to list all column names you want without symbol * for executing. The reason seems to be to avoid ambiguity with column name conflict.
you need to give in all column names, so not
SELECT * but SELECT column1, column2
and use the given prefixes of the table,
for example
in my case:
AP.column1, RE.column2 etc
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.
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;
I want to compare two strings from two different tables which contain the full name of a person is this format "Blow, Joe" since in one table the user may have the full name like that and other table might have the same user but the full name as "Blow, Joseph) so I want to grab the first two character from both the first and last name and see if they match. Then if they do I wan to update the record. I am not sure what I am doing wrong but I was getting an out of range error and now I am getting incorrect syntax near 'SUBSTRING' which I am looking into now. Does anyone know of a good way to achieve what I am trying to accomplish?
This is what I currently have:
SELECT *
FROM EmployeeMaster e
JOIN EmployeeDivisions d ON SUBSTRING(REPLACE(RTRIM(LTRIM(LEFT(e.FullName,CHARINDEX(',',e.FullName) - 1))),' ',''),1,3) LIKE SUBSTRING(REPLACE(RTRIM(LTRIM(LEFT(d.Name,CHARINDEX(',',d.Name) - 1))),' ',''),1,3)
SUBSTRING(REPLACE(RTRIM(LTRIM(SUBSTRING(e.FullName,CHARINDEX(',',e.FullName) + 1, LEN(e.FullName)))),' ',''),1,3) LIKE SUBSTRING(REPLACE(RTRIM(LTRIM(SUBSTRING(d.Name,CHARINDEX(',',d.Name) + 1, LEN(d.Name)))),' ',''),1,3)
I guess I don't have to point out that this check might match names that are very different. In your example Blow, Josephwould match not onlyBlow, Joebut alsoBlack, Johnand so on...
Maybe you should at least extend the check to include the complete surname together with part of the given name.
But... if you still want to compare the first two letters in the word before the comma, and the first two letters in the word after the comma then use this:
SELECT *
FROM EmployeeMaster e
JOIN EmployeeDivisions d ON
(
SUBSTRING(REPLACE(RTRIM(LTRIM(LEFT(e.FullName,CHARINDEX(',',e.FullName) - 1))),' ',''),1,2)
=
SUBSTRING(REPLACE(RTRIM(LTRIM(LEFT(d.Name,CHARINDEX(',',d.Name) - 1))),' ',''),1,2)
)
AND
(
SUBSTRING(REPLACE(RTRIM(LTRIM(SUBSTRING(e.FullName,CHARINDEX(',',e.FullName) + 1, LEN(e.FullName)))),' ',''),1,2)
=
SUBSTRING(REPLACE(RTRIM(LTRIM(SUBSTRING(d.Name,CHARINDEX(',',d.Name) + 1, LEN(d.Name)))),' ',''),1,2)
)
You might be able to reduce the complexity of the join to this:
LEFT(LTRIM(e.FullName),CHARINDEX(',',e.FullName)-1)
=
LEFT(LTRIM(d.Name),CHARINDEX(',',d.Name)-1)
AND
SUBSTRING(e.FullName,CHARINDEX(',',e.FullName) + 1, 3)
=
SUBSTRING(d.Name,CHARINDEX(',',d.Name) + 1, 3)
I have very simple query that calls a UDF which splits a field by comma. The query is
select top 10 * FROM Emails e WHERE EXISTS(SELECT TOP 1 1 FROM dbo.fn_Split(e.committees,','))
When I run/parse it, I get:
Msg 170, Level 15, State 1, Line 4
Line 4: Incorrect syntax near '.'.
I think it must have something to do with SQL 2000. If you switch out e.committees for something hardcoded (i.e., 'A,B,C,D') it works fine.
SQL 2000 doesn't support passing column names to TVFs. That was brought in in SQL2005 along with CROSS APPLY
I'm not really sure what you are needing to do here. Is e.committees a non 1NF list of numeric committee ids? If so
select top 10 <column-list>
FROM Emails e
WHERE e.committees
like '%[0-9]%'
ORDER BY ...
Might work but a better solution would be to store these in a normalised form in a table with columns email_id,committee_id or whatever. This will likely make your queries easier!