

I tried PARSE('31012016' as date using 'pt-BR') but raised exception. SQL Server recognizes "YYYYMMDD", but it fails for "DDMMYYYY" with message:Ĭonversion failed when converting date and/or time from character string.Įxample: DECLARE date = PARSE('31012016' as date using 'pt-BR')
#Sql cast string to date code#
I want to grab the string from the flat file and place it directly into SQL code without any conversion. Net and printed in the SQL code, then parsed again by SQL Server. In case the function fails to convert, it issues an error, depending on. Use the function TODATE() to convert a text value containing a date to the date data type.

I don't want to parse it into C# DateTime to then format it to "YYYY-MM-DD" so that SQL Server parses it once more.Īs I do currently, the date string is parsed once by. The CAST() function returns a DATE value if it successfully converts the string to date. I'd like to grab this string and pass it directly to the SQL text where data will be inserted. Run the T-SQL script below to create the two tables. This should be on a SQL Server that also has SQL Agent since we will use a scheduled job to centrally collect the data. I'm developing ETL from a file where dates are expressed in the format "DDMMYYYY", example "31012016". Step 1 - Create Tables to Store Data Create two tables on any SQL Server instance where you want to run and collect the data for your servers and databases.
