sql- Like Query find - sql

Would anyone know what to do with this I am doing a like query to select info. example:
SELECT *
FROM customers
WHERE customer_name LIKE '26%'
which will return
26_xx
26_xx
265_xx
but i only want to display 26_xx
I have tried which was suggested from a site :
SELECT *
FROM customers
WHERE customer_name LIKE 'H%!%' escape '!';
but that also returned
26_xx
26_xx
265_xx

In T-SQL you can use [] to escape the wildcard character _;
SELECT * FROM customers
WHERE customer_name LIKE '26[_]%'
The ESCAPE version would be;
SELECT * FROM customers
WHERE customer_name LIKE '26!_%' ESCAPE '!'

SELECT *
FROM customers
WHERE customer_name LIKE '26_%'
Is this what you mean? Only where the names start "26_" or do I read your question wrong?

This should work:
SELECT *
FROM customers
WHERE customer_name LIKE '26[_]%'
and len(customer_name) = 5
Proof:
select
sample_data.*
from
(
select '26_55' as customer_name
union
select '26_abc'
union
select '265_22'
union
select '26-22'
union
select 'abc'
union
select '26_18'
) sample_data
where sample_data.customer_name like '26[_]%'
and len(sample_data.customer_name) = 5

Related

BigQuery Union Distinct Where Value not in Preceding DataSet

I am trying to reconcile some student database with GSuite emails, where usernames have been created inconsistently for years.
The gist of the query I am trying to make on BigQuery is:
Match Emails to Students from email Pattern 1 and union with
Match Emails to Students from email Pattern 2 and union with
Emails not in 1 an 2.
Or in SQL:
with mymatches as (
with emaildataset as (
select 'testA' as col
union all
select 'testB'
union all
select 'testC'
union all
select 'testD'
)
select * from emaildataset where col like '%A'
union distinct
select * from emaildataset where col like '%B'
),
emaildataset2 as (
select 'testA' as col
union all
select 'testB'
union all
select 'testC'
union all
select 'testD'
)
select * from mymatches
union distinct
select * from emaildataset2 where emaildataset2.col not in (select col from mymatches)
This runs happily, but when I run the real code, then I'm getting duplicates.
The real code is now:
with matchedEmails as (
with g as (
select * from gsuite.StudentUsers
union all
select * from gsuite.AlumniUsers
)
select
std.STDCODE,
g.*
from g
inner join quick.all_students_alumni as std
on split(lower(g.Email), '#')[offset(0)] = split(quick.studentEmail(std.FNAME, std.MNAME, std.LNAME, std.STATUSTYPE), '#')[offset(0)]
where g.OU like '/Student%' or OU like '/Alumni%'
union distinct select
std.STDCODE,
g.*
from g
inner join quick.all_students_alumni as std
on split(lower(g.Email), '#')[offset(0)] = split(quick.studentEmail(std.FNAME, '', std.LNAME, std.STATUSTYPE), '#')[offset(0)]
where g.OU like '/Student%' or OU like '/Alumni%'
)
select * from matchedEmails
union distinct select
'NOT MATCHED' as STDCODE,
g.*
from (
select * from gsuite.StudentUsers
union all
select * from gsuite.AlumniUsers
) as g
where g.Email not in (select Email from matchedEmails)
and g.OU like '/Student%' or OU like '/Alumni%'
As a result though, I am getting duplicates in the Email column, which--based on my knowledge and test above--should not be, due to the where g.Email not in (select Email from matchedEmails) clause.
Am I doing something wrong?
I think, very last WHERE clause should be fixed to look like below
where g.Email not in (select Email from matchedEmails)
and (g.OU like '/Student%' or OU like '/Alumni%')
As you can see - the brackets around g.OU like '/Student%' or OU like '/Alumni%' were missing
it might be something else too that still need to be fixed - but this answers you below questions
As a result though, I am getting duplicates in the Email column, which--based on my knowledge and test above--should not be, due to the where g.Email not in (select Email from matchedEmails) clause.

sql repeat regex pattern unlimited times

I need to select where column contains numbers only and ends with a hyphen
I'm running SQL Server Management Studio v17.9.1
I have tried:
select * from [table] where [column] like '[0-9]*-'
select * from [table] where [column] like '[0-9]{1,}-'
select * from [table] where [column] like '[0-9]{1,2}-'
none of these work. The expression ([0-9]*-) works in any regex tester I've run it against, SQL just doesn't like it, nor the other variations I've found searching.
You can filter where any but the last character are not numbers and the last is a dash. DATALENGTH/2 assumes NVARCHAR type. If you're using VARCHAR, just use DATALENGTH
SELECT
*
FROM
[table]
WHERE
[column] like '%-'
AND
LEFT([column], (datalength([column])/2)-1) NOT LIKE '%[^0-9]%'
SQL Server does not support regular expressions -- just very limited extensions to like functionality.
One method is:
where column like '%-' and
column not like '%[^0-9]%-'
You can use left() and right() functions as below :
with [table]([column]) as
(
select '1234-' union all
select '{123]' union all
select '1234' union all
select '/1234-' union all
select 'test-' union all
select '1test-' union all
select '700-'
)
select *
from [table]
where left([column],len([column])-1) not like '%[^0-9]%'
and right([column],1)='-';
column
------
1234-
700-
Demo

Like Operator for checking multiple words

I'm struggling for a like operator which works for below example
Words could be
MS004 -- GTER
MS006 -- ATLT
MS009 -- STRR
MS014 -- GTEE
MS015 -- ATLT
What would be the like operator in Sql Server for pulling data which will contain words like ms004 and ATLT or any other combination like above.
I tried using multiple like for example
where column like '%ms004 | atl%'
but it didn't work.
EDIT
Result should be combination of both words only.
Seems you are looking for this.
`where column like '%ms004%' or column like '%atl%'`
or this
`where column like '%ms004%atl%'
;WITH LikeCond1 as (
SELECT 'MS004' as L1 UNION
SELECT 'MS006' UNION
SELECT 'MS009' UNION
SELECT 'MS014' UNION
SELECT 'MS015')
, LikeCond2 as (
SELECT 'GTER' as L2 UNION
SELECT 'ATLT' UNION
SELECT 'STRR' UNION
SELECT 'GTEE' UNION
SELECT 'ATLT'
)
SELECT TableName.*
FROM LikeCond1
CROSS JOIN LikeCond2
INNER JOIN TableName ON TableName.Column like '%' + LikeCond1.L1 + '%'
AND TableName.Column like '%' + LikeCond2.L2 + '%'
Try like this
select .....from table where columnname like '%ms004%' or columnname like '%atl%'

How select `Only String` and `Only Int`

I have some nvachar data in a my database.
aaaa , bb1b, c+cc , 1234 , dser , 5896
I need to two select for Only String and Only Int. This is mean :
Result of first select be : aaaa , dser
Result of second select be : 1234 , 5896
How?
You can use LIKE to perform this comparison. By using a double-negative, you can perform the tests with a single expression each:
declare #t table (val varchar(10) not null)
insert into #t(val)
select 'aaaa' union all
select 'bb1b' union all
select 'c+cc' union all
select '1234' union all
select 'dser' union all
select '5896'
select * from #t where val not like '%[^A-Z]%'
select * from #t where val not like '%[^0-9]%'
The first select says "give me all strings that don't contain a non-alphabetic character". Similarly, the second says "give me all strings that don't contain a non-digit"
You need to check for a regular expression.
In your example the query would look like this:
SELECT * FROM example WHERE foo LIKE '%[0-9]%' AND foo NOT LIKE '%[A-Z]%'
and
SELECT * FROM example WHERE foo NOT LIKE '%[0-9]%' AND foo LIKE '%[A-Z]%'
Maybe you have to do UPPERCASE(foo) for case insensitive checks. And add other characters like +, - and so on to the NOT LIKE expression.
See: http://msdn.microsoft.com/en-us/library/ms187489(SQL.90).aspx

How to select a field in addition to everything else?

In SQL Server I had the very convenient ability to make a query like this:
SELECT phone_number, last_known_location, *
FROM missing_female_pilots
WHERE last_name = 'Earhart'
How can I do something similar in Oracle?
You can use table alias :
SELECT t.phone_number, t.last_known_location, t.*
FROM missing_female_pilots t WHERE t.last_name = 'Earhart'
Or just prepend table name before * :
SELECT phone_number, last_known_location, missing_female_pilots.*
FROM missing_female_pilots WHERE last_name = 'Earhart'