I feel my problem is very complicated.
I have a Table A with a column named AbsenceLinks.
The table would look like this:
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Description | AbsenceLinks |
|- - - - - - - - - - - - - + - - - - - - - - - - - - - -|
| Illness | 14/3 15/9
|
|- - - - - - - - - - - - - + - - - - - - - - - - - - - -|
| Education | 19/3 18/9
|
|- - - - - - - - - - - - - + - - - - - - - - - - - - - -|
|Leave of Absence| 20/3
|
|- - - - - - - - - - - - - + - - - - - - - - - - - - - -|
| Holiday | 8/3
|
l- - - - - - - - - - - - - - - - - - - - - - - - - - - -l
I have another table B where I have a column named AbsenceID that matches the number before the slash-symbol in the AbsenceLinks column. (Table A AbsenceLinks value '20/9' matches AbsenceID 20 in table B)
This table would look like this:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Absence | AbsenceID
|
| - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - -|
| Illness (Days) | 14
|
| - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - -|
| Illness | 15
|
| - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - -|
| Leave of Absence | 20
|
| - - - - - - - - - - - - - - - -+ - - - - - - - - - - - - - -|
|Holiday Without Salary| 8
|
l- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -l
I tried to see how I could retrieve some of the string from AbsenceLinks and made a case statement:
CASE
WHEN LEN(AbsenceLink) = 3 THEN SUBSTRING(AbsenceLink,1,1) --1/3
WHEN LEN(AbsenceLink) = 4 and SUBSTRING(AbsenceLink,1,4) LIKE '%/' THEN SUBSTRING(AbsenceLink,1,1)--1/10
WHEN LEN(AbsenceLink) = 4 AND SUBSTRING(AbsenceLink,1,4) LIKE '%/%' THEN SUBSTRING(AbsenceLink,1,2)--17/3
WHEN LEN(AbsenceLink) = 8 AND SUBSTRING(AbsenceLink,1,2) like '%/' AND SUBSTRING(AbsenceLink,5,2) like '%/' THEN SUBSTRING(AbsenceLink,1,1)+', '+SUBSTRING(AbsenceLink,5,1)--2/9 1/10
WHEN LEN(AbsenceLink) = 8 AND SUBSTRING(AbsenceLink,1,3) like '%/' AND SUBSTRING(AbsenceLink,5,3) like '%/' THEN SUBSTRING(AbsenceLink,1,2)+', '+SUBSTRING(AbsenceLink,5,2)--10/3 9/9
WHEN lenLENAbsenceLink) = 9 AND SUBSTRING(AbsenceLink,1,3) like '%/' AND SUBSTRING(AbsenceLink,5,4) like '%' THEN SUBSTRING(AbsenceLink,1,2)+', '+SUBSTRING(AbsenceLink,5,3)--14/3 15/9
End AS AbsLink
I have to compare theese values in a report for some statistic for a customer.
I need theese two tables to be linked and theese columns are the only columns which can be linked.
I want to do like this:
SELECT A.col1, A.col2, B.col1, B.col2
FROM TableA A, TableB B
WHERE A.AbsenceLink = B.Absence
The problem is:
- The value of AbsenceLink is a nvarchar value like '20 / 3 1/9 ', there may be one or many spaces before or after the AbsenceID
- Absence is a int value like 20 and 1.
- I want 20 and 1 from '20/3 1/9' to be compared and linked with the Absence value.
- It is a database at my work and I can't change the data or make another table.
So dearest wise and clever programmers - what can I do?