I am trying to compare a table of users from one DB
and check whether that Email Address exists in our Dynamics CRM user base
I have the table User_V from that other interface,
and I created the following query, but it only gives me the results which are useremail = NULL
and I am only trying to find those who exist in user_v and not in [SystemUserBase]
select *
from [HSchool].[dbo].[user_v] as u
where not exists (select InternalEMailAddress
from [HrProd_MSCRM].[dbo].[SystemUserBase] as inn
where ltrim (rtrim (LOWER (u.useremail))) collate database_default <> ltrim (rtrim (LOWER(inn.InternalEMailAddress))))
I hope I am as clear as possible,
thank you in advance!
You are doing a double negative.
select *
from [HSchool].[dbo].[user_v] as u
where not exists (select InternalEMailAddress
from [HrProd_MSCRM].[dbo].[SystemUserBase] as inn
where ltrim (rtrim (LOWER (u.useremail))) collate database_default <> ltrim (rtrim (LOWER(inn.InternalEMailAddress)))
This query finds records that does not match.
Then there is a "Not Exist" above that.
Amending the "Where" clause in the sub-query to an equate should work
Related
I have written the following query:
SELECT TBSPACE FROM SYSCAT.TABLES WHERE TYPE='T' AND (TABNAME LIKE '%_ABS_%' OR TABNAME LIKE '%_ACCT_%')
This gives me a certain amount of results. Now the problem is that I have multiple TABNAME to select using the LIKE operator (~200). Is there an efficient way to write the query for the 200 values without repeating the TABNAME LIKE part (because there are 200 such values which would result in a really huge query) ?
(If it helps, I have stored all required TABNAME values in a table TS to retrieve from)
If you are just looking for substrings, you could use LOCATE. E.g.
WITH SS(S) AS (
VALUES
('_ABS_')
, ('_ACCT_')
)
SELECT DISTINCT
TABNAME
FROM
SYSCAT.TABLES, SS
WHERE
TYPE='T'
AND LOCATE(S,TABNAME) > 0
or if your substrings are in table CREATE TABLE TS(S VARCHAR(64))
SELECT DISTINCT
TABNAME
FROM
SYSCAT.TABLES, TS
WHERE
TYPE='T'
AND LOCATE(S,TABNAME) > 0
You could try REGEXP_LIKE. E.g.
SELECT DISTINCT
TABNAME
FROM
SYSCAT.TABLES
WHERE
TYPE='T'
AND REGEXP_LIKE(TABNAME,'.*_((ABS)|(ACCT))_.*')
Just in case.
Note, that the '_' character has special meaning in a pattern-expression of the LIKE predicate:
The underscore character (_) represents any single character.
The percent sign (%) represents a string of zero or more characters.
Any other character represents itself.
So, if you really need to find _ABS_ substring, you should use something like below.
You get both rows in the result, if you use the commented out pattern instead, which may not be desired.
with
pattern (str) as (values
'%\_ABS\_%'
--'%_ABS_%'
)
, tables (tabname) as (values
'A*ABS*A'
, 'A_ABS_A'
)
select tabname
from tables t
where exists (
select 1
from pattern p
where t.tabname like p.str escape '\'
);
I have query to find out the duplicates in my data but that query also encountering the data which are not duplicates but my query sees it as a duplicate because my query reading them same. For example 'AABWcFABmAAAyWJAAb' and 'AABWcFABmAAAyWJAAB' but in actual they are unique since this both id holds different data. I have used Collate function but it didn't help. Please let me know if there is any built in function I can use or any logic.
Thank you in advance for the help.
select distinct
npa
,npanxx_row_id
,count()
from kjm.audit_309
where npanxx_row_id
COLLATE Latin1_General_CS_AS in (npanxx_row_id) --and NPANXX_ROW_ID = 'AABWcFABmAAAyWJAAB'
group by npa,npanxx_row_id
having count() >1
order by npa
Another option in SQL Server would potentially be BINARY_CHECKSUM(). This will detect differences in case.
select
your_column
, BINARY_CHECKSUM(your_column)
, COUNT(*)
FROM your_table
GROUP BY
your_column
, BINARY_CHECKSUM(your_column)
HAVING count(*) >1
If you're looking for duplicates, then this should work:
CREATE TABLE #case_sensitivity_training (my_str VARCHAR(40) NOT NULL)
INSERT INTO #case_sensitivity_training (my_str)
VALUES ('AABWcFABmAAAyWJAAb'), ('AABWcFABmAAAyWJAAB')
SELECT
my_str COLLATE SQL_Latin1_General_CP1_CS_AS,
COUNT(*)
FROM
#case_sensitivity_training
GROUP BY
my_str COLLATE SQL_Latin1_General_CP1_CS_AS
HAVING
COUNT(*) > 1
You've put the collation in the wrong spot if you want to use DISTINCT.
The following should work on SQL Server:
SELECT DISTINCT
(ColumnName) COLLATE sql_latin1_general_cp1_cs_as
From TableName
But that collation may vary from RDBMS to RDBMS.
I have name fields in my data set. Using Oracle PL SQL, how can I search for the records that contain honorifics?
I have a list of honorifics that I want to search for in a separate table.
Any help would be really appreciated.
Thanks.
I'd use REGEXP_LIKE and do a cross join against the honorifics table.
This query will list all names that have an honorific, plus the honorific. If a name has more than one honorific it will be listed for each match:
SELECT
myTable.Name,
honorifics.Title
FROM myTable
CROSS JOIN honorifics
WHERE REGEXP_LIKE(myTable.Name, ''(\W|^)' || honorifics.Title || '(\W|$)')
The regex checks to see if the honorific title is at the beginning of the string or preceded by a "non-word" character, and if it's at the end of the string or followed by a non-word character.
Note that this search is case sensitive. To make it non-case sensitive, add a third argument of 'i' to the REGEXP_LIKE:
WHERE REGEXP_LIKE(myTable.Name, ''(\W|^)' || honorifics.Title || '(\W|$)', 'i')
^^^^^
SELECT names.lastName
FROM names
INNER JOIN honorophics ON names.lastName LIKE honorophics.listOfHonorophics + '%'
this will join the 2 tables, resulting in the rows, where lastName contains a pattern from the table with honorophics
How does wildcards works in sql. If I do select * from table it give all the fields. But if I do select a* from table it gives error. Shouldn't it give all fields which begins with a?
I am little confused.
SELECT * FROM tableName literally means "select all columns from tableName".
Philip Graham is right about his answer where he asked to use a.*
Wildcards help you search for strings about which you are not sure. These are almost always used with the LIKE keyword and put in WHERE clauses or searched CASE statements.
There are two wildcard characters - % and _.
% is used to find any string of 0 or more length.
E.g.,
SELECT firstName
FROM persons
WHERE UPPER(firstName) LIKE 'J%'
This will return all the firstName from the persons table where firstname starts with letter J. This would return "Jason", "James", "Josh", "Jessica" and much more.
Note that UPPER function was used to eliminate case sensitivity.
Next, you can have an _ character that looks for the presence of one single character.
SELECT firstName
FROM persons
WHERE UPPER(firstName) LIKE 'J_M__'
This would return "James", "Jimmy", "Jamos", "Jxmx" and filter away any "Jason", "Jaguar", etc.
For more info click here
You can use a.* where a is the name of the table. For instance in
select a.* from a left join b on a.id = b.id
You would return only the fields from a but not from b
If want to use a wild card in SQL, You need to key on the column that you want to filter using LIKE.
SELECT *
FROM table
WHERE column_name LIKE 'a%';
This will give you everything that begins with 'a' on that column.
If you don't want all the columns, you must explicitly give the name of each column that you want in the query.
SELECT LastName, FirstName, Address
FROM table
So if you want all the fields that begin with 'a' you must name all the fields that begin with 'a' in the SELECT statement.
Hope this helps.
I currently have a sql statement that outputs data into an excel document. However, every time there is a cell that doesn't have data, it outputs NULL into the cell. I was wondering if there was a way that I could replace NULL with just a blank space? Someone suggested using coalesce to do this, however I haven't ever had the chance to use it so I will have to do some reading on it. Anyone have any other suggestions?
In your select you can put an IsNull/IfNull round the column. Not particularly efficient but does what you want.
MS SQL
Select IsNull(ColName, '') As ColName From TableName
MySQL
Select IfNull(ColName, '') As ColName From TableName
IFNULL is an Oracle extension (adopted by many other platforms). The "standard" SQL function is coalesce():
SELECT COALESCE(columnName, ' ') AS ColumnName
FROM tableName;
Microsoft SQL
UPDATE TABLE_NAME
SET COLUMN_NAME = ''
WHERE COLUMN_NAME IS NULL
NOTE: this will SET, not select the result in your TABLE
MySQL
select IFNULL(columnName, '') from tableName
IFNULL(expr1,expr2)
If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2. IFNULL() returns a numeric or string value, depending on the context in which it is used.
SELECT teacher.name, IfNull(dept.name, '') as Dept
FROM teacher left outer JOIN dept
ON teacher.dept=dept.id