1

Our business would be providing us a .csv file. One of the columns in the file would be in date format. Now as we know there are many date formats in Excel. The problem is that we need to check whether the date provided is a correct date. It could be in any format like ddmmyyyy, yyyymmdd, dd-mon-yyyy etc basically any format that Excel supports.

We are planning to first load the data in a staging area and the date field would be defined as varchar so that it can accept any data.

Now either using SSIS or via T-SQL, I need to check whether the date provided is actually a date and if it is I need to load it into a different table in YYYYMMDD format.

How do I go about doing the above?

1 Answer 1

1

Considering you have your excel data already loaded into a SQL Server table as varchar (you can easily do this using SSIS), something like this would work:

SELECT 
    case when ISDATE(YOUR_DATE) = 1 then CONVERT(int,YOUR_DATE,112) else null end as MyDate
FROM
    YOUR_TABLE

I don't have access to a SQL Server instance at the moment and can't test the code above, so you may need to adapt to your needs, but this is the general idea. You can also do further research on ISDATE and CONVERT functions in SQL Server. You should be able to achieve what you need combining them together.

Sign up to request clarification or add additional context in comments.

1 Comment

I would try this approach and revert back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.