Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

How to validate the dates ie., compare the date values with one another

$
0
0

table structure

ID Col A   Col B

1  empno   [20141127-20151128]1234

1  empno   [20151201-99991231]232544

1  salutation  [20141127-99991231]Mrs

1  salutation  [20151127-99991231]Mr

2  empno   [20141127-20151128]1234

2  empno   [20151201-99991231]232544

2  salutation  [20141127-99991231]Mrs

2  salutation  [20151127-99991231]Mr

Issue Details:

so my requirement is to find the list of overlapping records based on the dates

user details may be varying from time to time in our system new data would be pushed. We have a tool in which we would push these data where we have applied a filter to accept on those users who are not having overlapping issues with the dates.

Overlapping Issue Example:

The users employee id for two years it is 1234 and later he moved to another department and his employee id got changed and it became 2345 remaining all details are same. So the main employee systems send the data for this user as empno - [20141127-20151128]1234 and empno - [20151201-99991231]232544

it means from 20141127 to 20151128 his employee no is 1234 and from 20151201 to 99991231 his employee would be 2345.

This is a correct case and the tool would accept this data.

the below cases are invald

Case 1: 1 salutation [20141127-99991231]Mrs

             1 salutation [20151127-99991231]Mr

Case 2: 2 salutation [20141127-99991231]Mrs

            2 salutation [20141127-99991231]Mr

So we wanted to find these overlapping records from tables.

Query:

I am able to successfully write query for the case 2 type but unable to write for case

 

select id,colA

count(left(ColB,CHARINDEX(']',ColB))) as 'Count'

from tblA with (nolock)

where id in (Select distinct id from tblb with (nolock))

group by id, cola,left(ColB,CHARINDEX(']',ColB))

having count(left(ColB,CHARINDEX(']',ColB)))>1


Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>