SQL SERVER error Importing from .txt file crashes upon invalid DATE or NUMBER

Hi,

I'm using SQL SERVER 2008R2.

I have a STORED PROCEDURE that imports a text file using a BULK INSERT into a real temporary table #table1.

Next, a SELECT statement  parses the strings from #table1 into variables then the the whole record gets INSERTED into tbl_destination.

PROBLEM: During parsing of the string, there are two scenarios that cause an error.
1. Invalid DATE conversions
2. Invalid NUMBER conversions

I would like to be able to catch these two invalid scenarios AND move them to another table tblExceptions
BEFORE the SELECT statement  parses the strings from #table1 into variables  and errors.

Please provide example tables and code of how to achieve this.

Thank you.
paultran00Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russell FoxDatabase DeveloperCommented:
You can use the IsDate() and IsNumeric() functions to remove them from the table prior to parsing:
INSERT INTO YourHoldingTable ([Field1], [Field2]...)
SELECT [Field1], [Field2]...
FROM YourTable
WHERE ISDATE([DateField]) = 0;
GO

DELETE YourTable
WHERE ISDATE([DateField]) = 0;
go

Open in new window

0
Dale FyeOwner, Developing Solutions LLCCommented:
If you are using the Bulk Insert command, I would recommend that you create your temp table with all the fields defined as nvarchar(255), or if longer than that, nvarchar(max).

Then, perform whatever error checking you need to in the temp table to flag records which will not transform properly when attempting to push the data from the temp table into your production tables.
1
Pawan KumarDatabase ExpertCommented:
Please do not use IsNumeric(). It fails in many cases.

Please try these solutions. Below will give you the rows which are not able to perform the conversion ( Bad/Problematic Data )

1. Invalid DATE conversions

SELECT * FROM yourtable
WHERE TRY_CAST(YourDATEColumn) IS NULL

OR

SELECT * FROM yourtable
WHERE ISDATE(YourDATEColumn) = 0

2. Invalid NUMBER conversions

SELECT * FROM yourtable
WHERE TRY_CAST(YourNumberColumn AS BIGINT) IS NULL
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

paultran00Author Commented:
@PAWAN:  TRY_CAST is not available in SQL SERVER 2008R2.
https://docs.microsoft.com/en-us/sql/t-sql/functions/try-cast-transact-sql
0
Pawan KumarDatabase ExpertCommented:
Yes you are correct... They are available in SQL 2012+
You can use this..

1. Invalid DATE conversions

SELECT * FROM yourtable
WHERE ISDATE(YourDATEColumn) = 0

2. Invalid NUMBER conversions

SELECT * FROM yourtable
WHERE  YourNumberColumn not like '%[^0-9]%'
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
paultran00Author Commented:
@Pawan Kumar:

How do I sequentially first test for invalid NUMBER CONVERSION then if that passes, then test for invalid DATE CONVERSION?  

(I use the SUBSTRING function to parse data from 1 column that got imported from a .txt file via BULK INSERT and when there's an error parsing, I write to a file the starting position of the substring and the length to help identify what broke)

I'm thinking I could write an inline query so that the results of the inner query (VALID NUMBER) are returned to the outer query to test for INVALID DATE.  Is there a more efficient way to write a SELECT query instead of an inline query which may be expensive?
0
Pawan KumarDatabase ExpertCommented:
Invalid.

SELECT * FROM yourtable
WHERE  YourNumberColumn not like '%[^0-9]%'

Valid

SELECT * FROM yourtable
WHERE  YourNumberColumn like '%[^0-9]%'
0
paultran00Author Commented:
@Pawan: Because you're already using ISDATE, for consistency of non numeric type validation, instead of using '%[^0-9]%'  , I used ISNUMERIC(SUBSTRING([a],224,8))=0:

WHERE
      (SUBSTRING([a],224,8) <> ''  -- NOT BLANK NULL pre-pod file
      AND ISNUMERIC(SUBSTRING([a],224,8))=0            -- NOT NUMERIC
      )  
      OR
      (SUBSTRING([a],224,8) <> ''  -- NOT BLANK NULL pre-pod file
      AND ISNUMERIC(SUBSTRING([a],224,8))=1  --  NUMERIC
      AND ISDATE(CASE WHEN SUBSTRING(a,224,8)>1900 THEN SUBSTRING([a],224,8)+19000000 ELSE SUBSTRING(a,224,8) END) = 0   -- INVALID DATE
      )
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.