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
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 28 days ago.
Improve this question
When I use:
SELECT *
FROM Customers1
WHERE email LIKE '%[^#]%'
There are no customers on the output at all, when there should be two on the output. Any ideas what might be causing this? I need to find customers email addresses which don't contain the '#'symbol.
I've tried running code:
SELECT *
FROM Customers1
WHERE email LIKE '%[#]%';
This shows 13 on the output.
I've also tried the '%[!#]%'; but this makes no difference.
In my understanding, you are trying to get all the email in a table that is not a valid mail address (by checking that email must have # symbol in it).
The way you are trying to achieve this will not work as this will fetch all data in the column except the null and blank (empty string) data.
[^] wildcard will check LIKE and PATINDEX in some special cases like.
A column has only single characters.
You are fetching a string where a specific position has not that value, etc.
For this scenario I will rather suggest, use a simple TSQL like.
SELECT *
FROM Customers1
WHERE email NOT LIKE '%#%';
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%'),' ','%')
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 2 years ago.
Improve this question
I have a table with string field like a|2$5.0| and I need to convert it to the following one a|5.0|5.0|.
I'm using SQL Server 2016.
The logic is as follows:
a - is the first character od each string
| - is a list elements separator
$ - if there is a $ sign in string, the element (value between $ and | signs) should be repeated as many times as the value of number preceeding $ sign (in my example 2 times).
String can be also more complex, but the same logic applies, eg.:
a|2$5.0|3.1|-4.2|3$-9.6|
which should be converted to:
a|5.0|5.0|3.1|-4.2|-9.6|-9.6|-9.6|
Any help will be appreciated.
Thanks in advance.
Honestly, like the comment say, I recommend fixing the design. Storing delimited data in your database is a bad idea, if I am honest. T-SQL is also a poor choice here, as it's far from good at string manipulation.
That being said, you can do this. I use DelimitedSplit8K_LEAD here, as it is ordinal position aware. if you're not on a recent version of SQL Server, you'll need to replace STRING_AGG with the old FOR XML PATH method:
SELECT STRING_AGG(NS.NewString,'|') WITHIN GROUP (ORDER BY DS.ItemNumber,T.I) + '|' AS NewString
FROM (VALUES('a|2$5.0|3.1|-4.2|3$-9.6|'))V(YourString)
CROSS APPLY dbo.DelimitedSplit8K_LEAD(V.YourString,'|') DS
CROSS APPLY (VALUES(CHARINDEX('$',DS.Item)))CI(I)
CROSS APPLY (VALUES(ISNULL(LEFT(DS.Item,NULLIF(CI.I,0)-1),1)))R(I)
CROSS APPLY (VALUES(STUFF(DS.Item,1,CI.I,'')))NS(NewString)
JOIN (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10))T(I) ON R.I >= T.I;
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 1 year ago.
Improve this question
I have a problem writing regular expression. I want to write a regular expression that replaces all double consonants with a single consonant.
Please help me to write such a rule in only one line.
Thanks in advance.
Here's a .NET regex that'll find any group of exactly two non-vowels:
[^aeiou]{2}
The following will work for groups longer than 2:
[^aeiou]{2,}
For example, this will match "llst" in "allstar."
Slightly uglier, but will match groups of 2 consonants, case-insensitive:
[QqWwRrTtYyPpSsDdFfGgHhJjKkLlZzXxCcVvBbNnMM]{2}
The following will match two identical non-vowels:
([^aeiou])\1
For example, this would match the "ll" in "all."
Once you have your regex, just use your chosen language's Regex.Replace function.
Since you did not specify the language, I'm going to go ahead and assume Javascript.
This should get you started:
console.log('babble bubble http htttp www'.replace(/([^aeiou\.,\/=?:\d&\s!##$%^*();\\|<>"'_+-])\1{1}/gi, "$1"));
See more here:
http://regexr.com/3ee47
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.