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?
 
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
 
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 FyeCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.