Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I am trying to improve a search function to allow for a space when people search involving data from 2 columns in the same table. For example location and keyword ie they search for Townsville marketing.
I tried the following:
SELECT *
FROM `partners`
WHERE location_keywords LIKE '%$result%'
OR keywords LIKE '%$result%'
OR keywords[ ]+location_keywords LIKE '%$result%'
OR location_keywords[ ]+keywords LIKE '%$result%'
I want it to return all results that contain Townsville, Marketing, Townsville marketing, or marketing townsville.
This throws a syntax error.
Can anyone help me fix this please?
We can try adding keywords on both sides, because the user can write in any order.
Let's replace spaces with any number of characters - this will allow you to search even if there are a lot of words in the keywords, and the user wrote one of them.
Let's convert everything to uppercase so that it does not depend on the case.
SELECT *
FROM `partners`
WHERE
UPPER(keywords ||' '|| location_keywords ||' '|| keywords) like Replace(UPPER('%$result%'),' ','%')
PS:
Column connection may depend on the database
keywords ||' '|| location_keywords ||' '|| keywords
or
keywords +' '+ location_keywords +' '+ keywords
or
CONCAT_WS(' ',keywords,location_keywords,keywords)
PSS: better if $result is prepared more carefully in advance.
I used the following as suggested and it worked exactly as I wanted it to. Thank you so much!
SELECT * FROM partners WHERE
UPPER(CONCAT_WS(' ',keywords,location_keywords,keywords)) like Replace(UPPER('%$result%'),' ','%')
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
I want to display all the names in a table that contains the character '+' but it doesn't work on Oracle SQL developer, while the column contains names with '+'
Select *
from Table where NAME like '%+%';
==> I got an empty output
Example:
Could you help me please ?!
You can use this statement:
Select *
from Table where NAME like '%' || chr(43) || '%';
Thank you
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to make changes to a "FORMULA" column in a table with SQL using REPLACE(FORMULA, '[H2]', '(2H)'); but it doesn't change the [H2] text. Going the other direction, REPLACE(FORMULA, '(H2)', '[2H]'); works fine.
I have to assume that '[' and ']' have special meanings in Oracle SQL, though I've not been able to find clues yet.
Suggestions welcome!
Are you trying to replace only [H2] to (2H) or everything like [AB122] TO (122AB) ?
if only [H2] then
with datas as ( select '[AA2][AH33][H2][AH267]' AS FORMULA FROM DUAL )
select REGEXP_REPLACE(FORMULA,'\[H2\]','(2H)') from datas;
if Everything then
with datas as ( select '[AA2][AH33][AH267]' AS FORMULA FROM DUAL )
select REGEXP_REPLACE(FORMULA,'\[([A-Z]+)([0-9]+)\]','(\2\1)') from datas;
Now without some sample data, it's hard to make sure that it fits all your cases
PS : your original replace works on my database
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to search only in 2016 year.
select from products
WHERE year="2016"
and name like '%"& name &"%'
or size like '%"& Size &"%'
Put parens around your or-ed conditions, and change the double quotes to single quotes for the year (as suggested by jarlh):
select from products
WHERE year='2016'
and (
name like '%"& name &"%'
or size like '%"& Size &"%'
)
Oh and in case this is VB.NET and you got name and Size from a parameter or something, make sure you are escaping them to prevent SQL injection (if this not already done somewhere else).
No need for double quotes, or any quotes in the LIKE.
SELECT *
FROM products
WHERE year = '2016'
AND (name LIKE '%name%' OR size LIKE '%size%'
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have noticed that some people in database columns instead of for example id, user, addressare using something like h_id, e_user, f_address etc...
Is that some kind of security aspect? or maybe these are some shortcuts of words?
Its because there might be many id fields like user_id,category_id,that's why they use so that code is understandable.
And talking about columns name like f_address, they are just shortcut for say first address. It doesn't have anything to do with security but to increase the query readability use proper name to fields so that people can understand just by seeing column name what data it saves.
If there are fields like category_id and sub_category_id , it is understandable from the field name, but if i denote it using c_id and s_id, its hard to depict.
Well, 'User' is a security object in SQL Server, so using that is kind of scary. 'ID' and 'address' are way too generic to provide any semantics when used as attribute names.
If a purpose of design is to be maintainable and readable, then there some words that simply don't work.
Definitely not security related.
Some use it for readability or speed (you don't have to remember which table you gave a certain name->see following example) when writing queries.
i.e.
select a.name, b.name from table1 a join table2 b on a.id=b.id
Like this you have to remember that table1 is named a and table2 is named b etc.
But if you use tablename_field (which you can shorten by using only the first letter of the tablename). That way you never have duplicate fieldnames when creating join queries.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Using SQL only, is there a way to query all values that are emails from a column of misc data?
I tried like '%#%', but there could be texts like this: Fort Knox # Room 123.
Edit: Regex is unavailable.
I've also tried like '%#%.%' but did not cover cases with spaces.
Here is SQLFiddle for you.
SELECT *
FROM TABLENAME
WHERE email LIKE '%#%.%'
AND email NOT LIKE '% %';
If you don't mind using CLR functions, I'd suggest writing a .NET method which uses a proper regular expression (you can find various ones online) to validate the email address, then calling that function as part of your query.
select * from whatever
where dbo.IsThisAnEmailAddress(myColumn) = 1
I would use regular expressions in your query to get at the emails. There are tons of links on this site to valid email regexes.
I agree with others, RegEx is better, however, if not available, try the following
WHERE fieldName LIKE '%#%'
AND fieldName LIKE '%.%'
AND charindex(' ',fieldName)=0
It's not great, and slow, but should get you pretty close. I.E Contains both an # and an . and no spaces...
SQLFiddle: http://www.sqlfiddle.com/#!3/5f6d8/1