Quantcast
Channel: SQL Server Integration Services forum
Viewing all articles
Browse latest Browse all 24688

Script task - date conversion issue

$
0
0

Hi .

I am using script task to fetch data from excel and pass it to a datatabale. From the dataTable, i am moving the data to sql table.

The data in each of the excel is of different format - one having: 19-Apr-2018 00:00:00  , another excel has data as 22/11/2019 00:00:00

I am trying to check if the data being passed to SQL is correct, before inserting the data. 

I have tried the below code:

 DateTime date;
 bool isValidBookedDate = true;
 string[] formats = { "yyyy-MM-dd hh:mm tt" };
 foreach (DataRow dtrow in excelDataSet.Tables[0].Rows)
{
  if (DateTime.TryParseExact(dtrow[5].ToString().Trim(), formats, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None,out date)) 
                    {                       
                        isValidBookedDate = true;
                    }
                    else
                    {                       
                        isValidBookedDate = false;
                        break;
                    }
}
In my SQL table, the smalldatetime column data is of the format: 2014-10-24 00:00:00

I am getting error in 2 places - One in parse, when the date looks correct, but still the loop goes to isValidbookeddate = false. sometimes if the data passes the parse code, it fails in the Insert  SQL statement with the error : The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value

The insert code is available like this after the datetime.parse - Insert into TableName ([Booked Date],[Eff Date], TestDate) Values ('22/11/2019 00:00:00','01/11/2019 00:00:00','04/04/2020 06:22:01') . In the SQL table columns, Booked Date,Eff Date are smalldatetime column, TestDate is varchar

How to work with the different formats of the date, and pass the correct excel dates to table?

Thanks


Viewing all articles
Browse latest Browse all 24688

Trending Articles



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