Comparing 2 columns in the same table with the "Like" function - sql

I'm trying to come up with a way to query the values in two different columns in the same table where the result set will indicate instances where the value of columnB doesn't contain the value of columnA.
For example, my "Nodes" table contains columns "NodeName" and "DNS".
The values should look similar to the following:
NodeName DNS
Router1 Router1.mydomain.com
I want to run a query to show which rows have a DNS value that does not contain (or begin with) the value of the NodeName field. I think the query should function something similar to the following, but obviously I'm missing something with regard to the use of "Like" in this situation.
SELECT NodeName, DNS
WHERE DNS NOT LIKE 'NodeName%'
I'm using SQL Server 2005, and any suggestions would be greatly appreciated... :)

SELECT NodeName, DNS
WHERE DNS NOT LIKE NodeName + '%' AND DNS NOT LIKE '%' + NodeName + '%'
Your DNS LIKE 'NodeName%' is comparing the string literal "NodeName" with DNS
Edit:
Nissan Fan added NOT LIKE '%' + NodeName + '%' which would mean NOT LIKE NodeName + '%' is not needed.
It depends if want "contains" or "starts with": take your pick.
#Nissan Fan: thanks: you should post it as your own answer...

SELECT USR.USER_ID, USR.LDAP_TITLE, LRM.LDAP_ROLE, LRM.ROLE_ID, RLS.PRIMARY_ROLE
FROM USERS USR, NEW_LDAP_ROLE_MATCH LRM, NEWROLES RLS
WHERE (INSTR(USR.LDAP_TITLE,LRM.LDAP_ROLE) > 0)
AND LRM.ROLE_ID = RLS.ROLE_ID;
It took lot of time to get the scenario working.Check out below link for sql queries.
http://www.anaesthetist.com/mnm/sql/query.htm

Related

update table removing some data

Could anyone help me with a command on SQL to update a column as this example bellow.
IP : 192.168.010.001
I need to remove zero and update column to be like this:
IP : 192.168.10.1
If you need to remove the last four characters, then most databases support LEFT():
update table t
set col = left(col, length(col) - 4);
The name of the length() function also differs among databases (typically either len() or length()). And substr()/substring() could be used instead of left().
EDIT:
Ahhh, Jeffrey pointed out that the above is an incorrect interpretation. I misread the numbers. There is a method to do this, which is pretty database independent. Generally, there will not be more than two zeros after a period (if there are three, then you probably want the third). So:
update table t
set ip = replace(replace(ip, '.0', '.'), '.0', '.')
where ip like '%.0%';
which Database you are using , if you are using Sybase you can try str_replace and use to replace pattern like '.0' or '.00' to '' .
Query could be something like this
`update X set Y=str_replace(Y,'.0','.') where Y like '%.0%' `
and
`update X set Y=str_replace(Y,'.00','.') where Y like '%.00%' `
A simple replace twice will also work:
replace(replace('192.168.010.001','.0','.'),'.0','.')
or
IP_Field = replace(replace(IP_Field,'.0','.'),'.0','.')

Search half the refID in SQL

I have a SQL table which hold unique REFID (int) and many other columns. I wanted to search a row using the half REFID . So if someone just search 0001 then 50001, 00015... comes up.
I have tried:
SELECT TOP 10 REFID
FROM Tablename
where REFID LIKE '%' + cast(0001 as varchar(10)) +'%'
however the problem is, it also giving me 150100 however I wanted 0001 to be in order.
'0001' is passed in as a parameter passed in from my C# application. I know I can convert the '0001' to string/varchar before sending it to the SQL however I was looking for a way to do it within the SQL so I can pass in the int from C# application
Code:
SELECT TOP 10 REFID
FROM Tablename
where REFID LIKE '%0001%'
0001 is a number and when converted to varchar() it will become '1'.
This will work with any number but only if you know beforehand that you will use four characters in your expression.
SELECT TOP 10 REFID
FROM Tablename
where REFID LIKE '%' + RIGHT('0000' + CAST(0001 AS VARCHAR(4)), 4) +'%'
We don't know how are you building your SQL statement, so we may need more information in order to help. Where do you get your ' 0001' value from? Is it a variable? Is it a parameter in a stored procedure? Is it inside a function in a different programming language?
You need to compare the REFID to a string value (not an int: as the comments point out, CAST(0001 AS VARCHAR(10)) returns 1, not 0001.
SELECT TOP 10 REFID
FROM Tablename
where REFID LIKE '%0001%'
EDIT: you have bigger issues too, like how to search for an integer value stored without leading zeroes, but if you are passing in a parameter you need to either make it varchar, or convert it to varchar in your query body, like so (assuming, of course, that you are always searching for a four-digit string):
SET #SearchParam_char = RIGHT('000' + CAST(#searchParam_Int AS VARCHAR(10)), 4)
I have found:
cast('0001' as varchar(10)) as 0001 === 1 thanks to ALEX K.
SQL will strip leading zero and there is no way of keeping the zero if you don't know the length.
My solution: I will send a string from my application and let SQL search it using the string.

LIKE clause using wildcard with multi value parameter

I'm stuck in this problem for days. In "report builder" I made my report having parameters: #ProductGroup and #RBS.
#ProductGroup has some specific values (chosen from checkbox).
#RBS has two fields: 1. MemberValue 2. MemberFullValue.
#RBS gets values from a query using the selected value from #ProductGroup. I use LIKE clause with wildcards. The query looks like:
SELECT MemberValue, CAST(MemberFullValue AS Varchar(2000)) AS MFV
FROM MSPLT_RBS_UserView
WHERE (CAST(MemberFullValue AS Varchar(2000)) LIKE '%' + #ProductGroup + '%')
ORDER BY MFV
The problem is: it works well if i select single value from #ProductGroup, but when I select multi value it shows nothing.
I also tried IN clause. Doesn't work.

Need to check parameter against table rows in SQL Query

I would like to build one sql query in that one of my filed of form should not contain common names (maintained list of words in separate table) and i am passing value of that filed as parameter and want to check that it shouldn't contain any common name from that table.
How can i achieve that using sql query?
Note : if common name is 'abc' and i am passing parameter as '!abc123' since it contains that word query should return false.
Thanks in advance.
Try something like (Untested Query):
SELECT CommonName
FROM CommonNamesTable
WHERE CommonName like '%NameToTest%'
OR CONTAINS(NameToTest, CommonName);
Basically you need the string match options:
Take a look at options of CONTAINS and read about Queries with full text search
Is this what you're looking for?
SELECT (COUNT(*) == 0) FROM tablewithcommonwords
WHERE wordfromform LIKE CONCAT('%', wordcolumnnfromcommonwordstable, '%');
Try this:
IF NOT EXISTS(SELECT word FROM CommonWord WHERE #yourparam
LIKE '%' + word + '%')
BEGIN
RETURN 1
END
ELSE
BEGIN
Return 0
END
This works if the #yourParam is contained in any word or name, what you do not want to use. It only returns 1 if it is not contained by any row in the table.
I worte this sentence only on this way (you can use a simple Exists instead of NOT Exists), because may you want to extend the functionality in the true part.
if exists (select * from reservedwords where #parameter like '%'+word + '%')
select 0
else
select 1
I would like to suggest that You have to use keypress Event in Your TextBox and then Handle your Code after Each character enter in your TextBox.

SQL Query for finding a column name where matching text is in column

This is my first stakoverflow question, although I've lurked for quite a while. I'm writing a webapp in PHP/SQLite, and I'm trying to find a column name along with the following SQL query:
SELECT lexemeid FROM lexeme, sense WHERE lexeme.lexemeid =
sense.senselexemeid AND (lexeme.lexeme LIKE '%apani%' OR lexeme.variant
LIKE '%apani%' OR lexeme.affixedform LIKE '%apani%' OR sense.example
LIKE '%apani%');
Basically, I'm offering a full text lookup for a few different fields. The query above works, but I'd like to get the name of the column where my wildcard matches for each result as well. Basically I want something like the above query with the SELECT looking more like:
SELECT lexemeid, COLUMN NAME FROM...
I'd also welcome any ideas for making my SQL Query look/perform better (maybe using LIKE and IN??). I'm basically trying to join lexeme.lexemeid and sense.senselexemeid and do a wildcard lookup on a text string (in this case, "apani").
Thanks!
Assuming you only have a match in one of the columns, you could use a CASE statement.
SELECT lexemeid,
CASE WHEN lexeme.lexeme LIKE '%apani%' THEN 'lexeme'
WHEN lexeme.variant LIKE '%apani%' THEN 'variant'
...
WHEN sense.example LIKE '%apani%' THEN 'example'
END AS ColumnName
FROM ...