So this is the data I am pulling in from powershell (there's actually more, but it all follows the same pattern):
Name : SULESRKMA 1
Location : Leisure Services - Technology Services 2
DriverName : KONICA MINOLTA mc4695MF PS PPD 3
Shared : False 4
ShareName : 5
JobCountSinceLastReset : 0 6
I am trying to remove 'Name : ' and 'Location : ', and so on, but using the REPLACE command here is part of my sql query:
SELECT * FROM #ReadCmd
WHERE Result LIKE 'Name %:%'
INSERT INTO #output(Name)
SELECT REPLACE(Result, '% %: %', '')
From #ReadCmd
WHERE Result LIKE 'Name %:%'
SELECT * FROM #output
Or for example, here:
IF OBJECT_ID('tempdb..#fields') != 0
DROP TABLE #fields
CREATE TABLE #fields (Fields varchar(256))
INSERT INTO #fields (Fields)
SELECT REPLACE(Result, ' %: %', '')
FROM #ReadCmd
Where Result Like '% %: %'
The point is, I'd like to replace the '_________ : ' with nothing, but the REPLACE command reads the sql wild card '%' as an actual percent sign. Is there another way to accomplish this?
Using Select RIGHT(Result,CHARINDEX(': ',Result) -1) outputs in seperate cells:
: SULESRKMA
: PrimoPDF
oft XPS Document Writer
: Fax
: CutePDF Writer
you can try
SELECT * FROM #ReadCmd
WHERE Result LIKE '%:%' and Result like 'Name %';
if you want select only the info after the : then you should use
SUBSTRING(Result, CHARINDEX(':',Result) +2, 255)
from #ReadCmd
WHERE Result LIKE '%:%' and Result like 'Name %';
Select RIGHT(ColumnName,CHARINDEX(':',ColumnName) -1)
See SQL string manipulation [Get all text left of '('] for reference.
That is unless I am misunderstanding your question and you want to keep Name and the : in there and just remove the spaces. If that is so your second and fourth non code lines are contradictory.
Related
Hello I'm using Oracle 11g and i have a data that looks like this
no|name|flag
------------
1|kumar|1
2|rajesh singh|1
3|adi sneedar|1
4|danielle castro|1
5|cef danish|1
if i did
select count(*) from tablename where name like '% %'
it will return 2 records.
if i did "multiple spaces", like 2 or more spaces
select count(*) from tablename where name like '% %'
it returns 0(this means good)
it will return 5 records.
What i want is if the user only input '% %' it will also return 0. But i also wanted that
select count(*) from tablename where name like '%adi sneedar%'
it will return 1
How should i do that in the where condition?
Something like this might suffice, assuming that the '%' are being passed as part of the binding input
select *
from mytable
where name like :bindvar
and replace(replace(:bindvar,'%'),' ') is not null
which basically says they need to enter something that is not solely spaces and percentage signs.
I have a table called grades that has 4 columns: ID, math, science and history.
I want to create an sql query that selects from the table "grades" the specific entry where "ID" is equal to a variable. This variable changes every time the program is run. What I have tried so far but is not working is this:
"SELECT * FROM grades Where ID LIKE %" + IDString + "%"
"SELECT * FROM grades Where ID LIKE %IDString%"
"SELECT * FROM grades Where ID LIKE 'IDString'"
Note: IDString is the String variable.
Note: I am using Java and Sqlite.
String IDString = "12345";
ResultSet rs = connection.createStatement().executeQuery("SELECT * FROM grades Where ID LIKE '%' || IDString || '%' ");
this code is to make my problem a little more clear.
Use concat function to concat the variable and strings.
SELECT * FROM mytable Where col2 LIKE CONCAT('%', #start, '%')
or in your case:
SELECT * FROM grades Where ID LIKE CONCAT('%', #IDString, '%')
Here is a small demo
In SQLite this would look like this:
SELECT * FROM grades Where ID LIKE '%' || #IDString || '%'
But in SQLite you can not use variables. This needs to be done in your Java part of the code.
When sending the query from JAVA you can do it like this:
"SELECT * FROM grades Where ID LIKE '%' ||" + IDString + "|| '%' "
Your own examples are wrong because the pattern literal should be enclosed into single quotes. The pattern characters like % or _ will be inside the literal:
SELECT * FROM grades Where ID LIKE '%IDString%'
If you'd like to substitute a value in the "pure MySQL", put the pattern characters into the variable, but not into query body:
SET #pattern = CONCAT('%', #var, '%');
SELECT * FROM grades WHERE ID LIKE #pattern;
https://www.db-fiddle.com/f/vr13gVV19CTcR1kbgohS34/0
The same for PHP or other "external" languages. You can use prepared statement with a value which already contains pattern characters. Something like that:
$sth = $dbh->prepare("SELECT * FROM grades WHERE ID LIKE ?");
$sth->execute('%' . $var . '%'));
$result = $sth->fetchAll();
I have a table with a column and value JobSkill = ".net sap lead". Now user enter the value "abap sap hana". I want to include a where condition which match exactly 3 or more continuous characters including space. In above scenario both have common "sap" substring so the condition should result in true. Below is my query. Please help. Previously I am using charindex but it does not resolve the purpose. I am using sql server 2008
SELECT Email_Id, JobSkill FROM Jobs
WHERE CHARINDEX(JobSkill, "abap sap hana") > 0
You need to create a function which loops through all positions of characters of String1 except the last 2, and check if String2 is like '%' + [(x,x+1,x+2)] + '%' string, where x is current position.
So for stings ('abcd acd g', 'ert acd'),
it should check
'ert acd' like '%abc%'
'ert acd' like '%bcd%'
'ert acd' like '%cd %'
'ert acd' like '%d a%'
and so on...
If like returns TRUE, break the loop.
Try like this,
SELECT j.Email_Id
,j.JobSkill
FROM Jobs j
INNER JOIN (
SELECT LTRIM(RTRIM(m.n.value('.[1]', 'varchar(8000)'))) SearchString
FROM (
SELECT CAST('<XMLRoot><RowData>' + REPLACE(#Input, ' ', '</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
) t
CROSS APPLY x.nodes('/XMLRoot/RowData') m(n)
) T ON j.JobSkill LIKE '%' + T.SearchString + '%'
I have a Text,
'Me and you against the world' // false
'Can i have an email address' // true
'This is an' // true
'an' //true
I want to check whether the word an is inside my String.
How do I check if a text contains a specific word in SQL? I can't add a full-text catalog. Otherwies i could
SELECT * FROM TABLE WHERE CONTAINS(Text, 'an')
Here's one approach.
DECLARE #table_name table (
column_name varchar(50)
);
INSERT INTO #table_name (column_name)
VALUES ('Me and you against the world')
, ('Can i have an email address')
, ('This is an')
;
SELECT column_name
FROM #table_name
WHERE ' ' + column_name + ' ' LIKE '% an %'
;
There are some way to do this, seem you want find a word and not a part of a word, so you can do in easy way with like operator
You can have 3 cases to found a word
'space'WORD
WORD'space'
'space'WORD'space'
SELECT * FROM TABLE WHERE Field like ' an' OR Field like 'an ' OR
Field like ' an '
Hope it helps
It is perfectly done in MS SQL Server by the CHARINDEX function (it is internal to MS SQL):
if CHARINDEX('an ',#mainString) > 0
begin
--do something
end
The solution was showed before in another post.
The three cases you'll encounter as Luka mentions:
Space before word
Space after word
Space before and after word
To accomplish this, you'll write a query like the following which searches for the whole word, and pads the expression to search with a leading and trailing space to capture words at the start/end of the expression:
Note: I've used a contrived example to make this portable and demonstrable.
select
t.txt
from (
select
'this is an awesome test of awesomeness man' as txt
) t
where
charindex(' an ', ' ' + t.txt + ' ') > 0;
I have this query:
SELECT *
FROM
(SELECT ' ' + REPLACE(Title,' ',' ') + ' ' AS Title
FROM MyTable) t
WHERE Title LIKE '% Samsung %'
AND Title LIKE '% Galaxy %'
AND Title LIKE '% Axiom %'
AND REPLACE(REPLACE(REPLACE(Title,' Samsung ',''),' Galaxy ',''), ' Axiom ','') = ''
This query should search in MyTable field Title and dispay all rows which contain the words specified in LIKE.
I don't get any error, but the Field Title contains a row with the following string 'Samsung Galaxy Axiom R830' and my query does not return it (and it should).
This was my original question, it worked for some records, but not for all SQL SELECT LIKE containing only specific words
Could it be because you are looking for "Samsung" with a space before & after and the string does not have a space before "Samsung" ?
You have spaces in the like. Perhaps you want something like:
WHERE (Title LIKE '% Samsung %' or title like '%Samsung' or title like 'Samsung%')
AND (Title LIKE '% Galaxy %' or title like '%Galazy' or title like 'Galaxy%')
AND (Title LIKE '% Axiom %' or title like '%Axiom' or title like 'Axiom%')
AND replace(REPLACE(REPLACE(REPLACE(Title,'Samsung',''),'Galaxy',''), 'Axiom',''), ' ') = ''
Actually, as I think about it, I think the final replace is sufficient:
where replace(REPLACE(REPLACE(REPLACE(Title,'Samsung',''),'Galaxy',''), 'Axiom',''), ' ') = ''
If the title is "Samsung Galaxy Axiom R830", then the following condition will not be true.
REPLACE(REPLACE(REPLACE(Title,' Samsung ',''),' Galaxy ',''),' Axiom ','') = ''
The replaces as written will output
SamsungAxiom R830
This will not match a blank string.
If you removed the spaces from your replaces, you'll be left with R830 (and possibly some whitespace). As Hellion says in his comment, this is a query that requires the words 'Samsung', 'Galaxy' and 'Axiom' to be the only words in your title.