Environment: SQL Server 2008 R2
Purpose: Data validation and proper handling. If the loaded data mismatch the following fields data range requirements, it will redirect those data into table A, while the right ones go to staging table for loading process
Problem: After setting allowable data values in T-SQL, where splits the valid ones into staging table while the invalid goes to DummyTable for the following items:
INSERT INTO DummyTable select * from StagingTable where form_cd NOT IN ('A' ,'B','C','D','E','F','G','H','I','J','K','G', 'Z') and not in grade_col NOT ('L','U','VLR') OR LENGTH(SSN)>9 OR SSN LIKE '%[^0-9]%'
In the dummy table contains fields as same as stagingTable but there is an additional field such as status. How would I flag invalid data and insert the type of invalidity, for example, stagingTable contains SSN field which one of the rows is storing 12345678910. This SSN considered invalid because the LEN is > 9 therefore, the T-SQL redirects the record/row, which is associated by 12345678910 to DummyTable. Then, flag it into status field as "length of SSN is > 9". Another example, 12345678Q is invalid SSN so the flagged it as SSN contains alphanumeric and so forth
I also another question about date
how I would verify date range in the field order_dt such as '20130431' while is suppose to be '20130430' out of range
How I could translate that into T-SQL