r/SQLServer • u/squeezyflit • Mar 16 '26
Question SQL Server script error details
Our SQL Server error messages only show minimal detail -- the SQLSTATE, a tag (presumably from the job step), and a generalized error message (example: "Conversion failed when converting date and/or time from character string"). We get no indication of the record number, column name, or bad data itself. When importing thousands of lines from a flat file, it makes finding the bad data a challenge.

Can SQL Server error messages be configured to provide more detail? If so, can someone point me to relevant documentation? Thank you in advance!
4
u/Anlarb 1 Mar 16 '26
Oh my yes.
https://sommarskog.se/error_handling/Part1.html
1
u/Better-Credit6701 Mar 16 '26
Looks like you have some bad data, error converting date
3
u/Neghtasro Mar 16 '26
Nope, that error message is all you get. It's one of the datetime columns in whatever data set you're trying to work with. If you can change the format you receive the file in (or preprocess it), putting your datetimes in the format "YYYY-MM-DDTHH:mm:ss" should make things just work. But without trying to import each line one by one there's no real way to tell which part of the data is causing the error.
2
u/FreedToRoam Mar 16 '26
I import 100 million rows from a wide text file on a regular basis and thus the odds are stacked against me. That is why my workflow involves first sucking in the whole line in the table and then parse it out line by line and if a line fails I record the statement along with the line number. Because line by line is slow, I break it into batches and then I execute them all at once in a kind of poor man’s multithreaded processing. Afterwards I get a report how many lines succeeded and how many failed and I can then take the saved statement and run it manually to see where the problem is and why…
2
u/Simple_Brilliant_491 Mar 16 '26
This is part of the reason medallion architecture was developed, see What is Medallion Architecture? | Databricks https://share.google/RtLMtxtJPa6PuGXlQ
Bring the data in "raw", i.e. just as varchar or nvarchar. Once it's in a table you can validate it and decide what to do with bad data, for example skip the entire row, or use a default date if it's bad. Once the data is clean, then do further transformation.
This should give a more reliable and easier to troubleshoot approach.
1
u/mariahalt Mar 19 '26 edited Mar 19 '26
Are you staging the data as text first? You can then use TRY_CONVERT(Date, <fieldname>), any rows that return a NULL are bad dates.