Given
CREATE TABLE Person (
id bigint,
first_name nvarchar(60),
last_name nvarchar(60),
custom_employee_id nvarchar(60)
);
CREATE TABLE User (
id bigint,
username nvarchar(60),
department nvarchar(60)
);
INSERT INTO Person ( id,first_name,last_name,custom_employee_id)
VALUES (1,'steve','rogers','00009094');
INSERT INTO User ( id,username,department)
VALUES( 23,'9094','accounting');
I want to join on Person.custom_employee_id and User.username. Both are nvarchars so this should be cake but custom_employee_id has embedded leading zeros and also nulls to contend with.
SELECT A.id, A.first_name, A.last_name, A.custom_employee_id
, CAST( CAST( A.custom_employee_id) AS bigint) AS nvarchar) AS trythis
FROM Person A
WHERE ISDECIMAL( A.custom_employee_id) =1
seems to get me close with
1,'steve','rogers','00009094','9094'
so trying
SELECT A.id, A.first_name, A.last_name, A.custom_employee_id
, CAST( CAST( A.custom_employee_id) AS bigint) AS nvarchar) AS trythis
, B.department
FROM Person A
LEFT JOIN USER B ON B.username = 'trythis'
WHERE ISDECIMAL( A.custom_employee_id) =1
does execute but yields
1,'steve','rogers','00009094','9094',NULL
instead of the hoped-for
1,'steve','rogers','00009094','9094','accounting'
So how must I adjust my query?
TIA,
Still-learning Steve