Wildcard for string before # character - sql

How would I use a wildcard to filter out any permutation of the following. There can be any number of zeros before the "#" character.
Example
0#test.com
000000#test.com
00000000000#test.com
Basically, I'm looking to wildcard any email address with only zeros before the # character.
Any help would be greatly appreciated
Thank you !
Smiddy

T-SQL:
To remove all values with only zeros before the #
SELECT
*
FROM (SELECT '000#test.com' AS[values] ) [table]
WHERE
LEN(REPLACE((LEFT([values],CHARINDEX('#',[Values])-1)),'0','')) <> 0
-- LEFT & CHARINDEX : get the result LEFT of the #
-- LEN & REPLACE : After all 0 are replaced by '' any email that contained only zeros will result in len = 0.

I would solve it with a regular expression, but the concrete answer depends on your database vendor and the available syntax.
PostgreSQL:
SELECT * FROM table WHERE email ~ '^0+#'
MySQL:
SELECT * FROM table WHERE email REGEXP '^0+#'
Synonyms are email RLIKE '^0+#' and REGEXP_LIKE(email, '^0+#').
Oracle:
SELECT * FROM table WHERE REGEXP_LIKE(email, '^0+#')

Related

How to get the domain name without '.com'?

SELECT substr(Emails, instr(Emails, '#')+1)
FROM EmployeeEmails;
Returns
gmail.com
Do I need to concat to get:
gmail
Check your relevant database query :
Query(for MySQL)
select (SUBSTRING_INDEX(SUBSTR(Emails, INSTR(Emails, '#') + 1),'.',1)) from EmployeeEmails;
Query(For Sql Server):
select SUBSTRING(Emails,(CHARINDEX('#',Emails)+1),1) from EmployeeEmails;
Query(For Oracle)
select substr(Emails,instr(Emails,'#',1)+1) as domain
from EmployeeEmails;
You can use REGEXP_REPLACE to extract the domain name:
select regexp_replace(emails, '^[^#]+#([^.]+)\..+$', '\1') from employeeemails;
This works for any email of the pattern abcd#efgh.ijkl .
The pattern:
^ start of the sting
[^#]+ 1 to n characters other than #
# the at sign #
( remember the following string
[^.]+ 1 to n characters other than the dot .
) end of the string to remember
\. a dot .
.+ 1 to n characters
$ end of the string
\1 the remembered string
And here is the old-fashioned way without REGEXP_REPLACE:
select substr(emails,
instr(emails, '#') + 1,
instr(emails, '.', instr(emails, '#') + 1) - instr(emails, '#') - 1
)
from employeeemails;
try this : -
declare #email sysname = 'testCode#gmail.com'
SELECT substring(#email, charindex('#',#email,0)+1 , (charindex('.',#email,0)-1 - charindex('#',#email,0)))
You can use LIKE with wild cards see here
underscore character ( _ ) for any single character.
percent sign character (%) for a string of zero or more characters.
SELECT email FROM emails
WHERE email NOT LIKE '%_#__%.__%'
This will ignore the following cases (simple version for valid emails):
emails that have at least one character before the #
emails that have at least two characters between # and .
emails that have at least two characters between . and the end.

SQL special group by on list of strings ending with *

I would like to perform a "special group by" on strings with SQL language, some ending with "*". I use postgresql.
I can not clearly formulate this problem, even if I have partially solved it, with select, union and nested queries which are not elegant.
For exemple :
1) INPUT : I have a list of strings :
thestrings
varchar(9)
--------------
1000
1000-0001
1000-0002
2000*
2000-0001
2000-0002
3000*
3000-00*
3000-0001
3000-0002
2) OUTPUT : That I would like my "special group by" return :
1000
1000-0001
1000-0002
2000*
3000*
Because 2000-0001 and 2000-0002 are include in 2000*,
and because 3000-00*, 3000-0001 and 3000-0002 are includes in 3000*
3) SQL query I do :
SELECT every strings ending with *
UNION
SELECT every string where the begining NOT IN (SELECT every string ending with *) <-- with multiple inelegant left functions and NOT IN subqueries
4) That what I'm doing return :
1000
1000-0001
1000-0002
2000*
3000*
3000-00* <-- the problem
The problem is : 3000-00* staying in my result.
So my question is :
How can I generalize my problem? to remove all string who have a same begining string in the list (ending with *) ?
I think of regular expressions, but how to pass a list from a select in a regex ?
Thanks for help.
Select only strings for which no master string exists in the table:
select str
from mytable
where not exists
(
select *
from mytable master
where master.str like '%*'
and master.str <> mytable.str
and rtrim(mytable.str, '*') like rtrim(master.str, '*') || '%'
);
Assuming that only one general pattern can match any given string, the following should do what you want:
select coalesce(tpat.thestring, t.thestring) as thestring
from t left join
t tpat
on t.thestring like replace(tpat.thestring, '*', '%') and
t.thestring <> tpat.thestring
group by coalesce(tpat.thestring, t.thestring);
However, that is not your case. However, you can adjust this with distinct on:
select distinct on (t.thestring) coalesce(tpat.thestring, t.thestring)
from t left join
t tpat
on t.thestring like replace(tpat.thestring, '*', '%') and
t.thestring <> tpat.thestring
order by t.thestring, length(tpat.thestring)

Sql Where Clause not working if add space at the end of string

I have below sql Query
Select * from usertable where username = 'xyz (space)';
It is giving me the result which is wrong the result has to come only when i not add space at end because in database there is no space.
Is there any function or how can i do that.
I hope you want to filter the records those have the space in the last position.
DECLARE #TestTable TABLE (Data VARCHAR(20));
INSERT INTO #TestTable (Data) VALUES ('xyz'), ('Sharma ');
SELECT * FROM #TestTable WHERE Data = 'Sharma ';
SELECT * FROM #TestTable WHERE Data = 'Sharma';
you will get the same result for WHERE Data = 'Sharma ' and 'Sharma'
Using SUBSTRING(Data, DATALENGTH(Data), 1) you can get the last character of the column and add the condition in WHERE clause will solve your problem:
DECLARE #TestingValue AS VARCHAR(20) = 'Sharma'; -- 'xyz' -- 'Sharma '
SELECT * FROM #TestTable WHERE Data = #TestingValue AND SUBSTRING(#TestingValue, DATALENGTH(#TestingValue), 1) = ' ' ;
The above block return result if you set 'Sharma ' as parameter value.
This is because SQL Server follows ANSI 92 rules which stipulates that:
SQL Server follows the ANSI/ISO SQL-92 specification (Section 8.2, <Comparison Predicate>, General rules #3) on how to compare strings with spaces. The ANSI standard requires padding for the character strings used in comparisons so that their lengths match before comparing them. The padding directly affects the semantics of WHERE and HAVING clause predicates and other Transact-SQL string comparisons. For example, Transact-SQL considers the strings 'abc' and 'abc ' to be equivalent for most comparison operations.
(my emphasis)
If the data is CHAR data with trailing spaces you could use the RTRIM function like this;
select * from usertable where RTRIM(username) = 'xyz'
EDIT: In order to match including trailing spaces you can use the DATALENGTH function, like this;
select * from usertable where username = 'xyz ' AND
DATALENGTH(username) = DATALENGTH('xyz ')
If you don't want the result if there is space at end you can use below query
select * from usertable where username not like '% '
Or this one:
select * from usertable where right(username,1) <> ' '
Hi this code works for me........
select * from usertable where username = 'xyz (Space) ' AND
DATALENGTH(username) = DATALENGTH('xyz (Space) ')

Select statement with column contains '%'

I want to select names from a table where the 'name' column contains '%' anywhere in the value. For example, I want to retrieve the name 'Approval for 20 % discount for parts'.
SELECT NAME FROM TABLE WHERE NAME ... ?
You can use like with escape. The default is a backslash in some databases (but not in Oracle), so:
select name
from table
where name like '%\%%' ESCAPE '\'
This is standard, and works in most databases. The Oracle documentation is here.
Of course, you could also use instr():
where instr(name, '%') > 0
One way to do it is using replace with an empty string and checking to see if the difference in length of the original string and modified string is > 0.
select name
from table
where length(name) - length(replace(name,'%','')) > 0
Make life easy on yourselves and just use REGEXP_LIKE( )!
SQL> with tbl(name) as (
select 'ABC' from dual
union
select 'E%FS' from dual
)
select name
from tbl
where regexp_like(name, '%');
NAME
----
E%FS
SQL>
I read the documentation mentioned by Gordon. The relevent sentence is:
An underscore (_) in the pattern matches exactly one character (as opposed to one byte in a multibyte character set) in the value
Here was my test:
select c
from (
select 'a%be' c
from dual) d
where c like '_%'
The value a%be was returned.
While the suggestions of using instr() or length in the other two answers will lead to the correct answer, they will do so slowly. Filtering on function results simply take longer than filtering on fields.

SQL LIKE only one letter

When i use LIKE statement in my SQL,
for example,
SELECT * FROM table WHERE name = "%k"
It will return all rows, where name ends on k.
It can return : Ok, OOk, OOOk,
How i can do same statement but with one letter, so it returns only Ok.
Or 2 letters, so it returns only OOk?
_ is a single character wildcard.
SELECT * FROM table WHERE name = `_k`
See this IBM Reference
Use the equality operator (=) or the IN operator instead of the LIKE operator:
SELECT * FROM table WHERE name IN ('Ok', 'OOk')
Use the _ wildcard. It matches only a single character.
_k for Ok.
__k for Ook
depending on the length you provide
For 1 character 'X' followed by k
Select *
from table
where length(name) = 2
AND name = "%k"
For 2 character 'X' followed by k
Select *
from table
where length(name) = 3
AND name = "%k"
You should use an underscore (_) character. See the documentation about operator LIKE.
So, the query you need (1 or 2 chars before k) is:
SELECT * FROM table WHERE name LIKE '_k' OR name LIKE '__k'