Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Is there any exception handling mechanism in t-sql simillar to the C# or javascript one for example? (I mean smth. simillar to the try/catch constraction) if no... is there any other way to handle it?
One of all tasks is i want to handle date conversion (but not only -- its just one case). I have a table EVENTS with a field DATE of varchar type. In some records the value of this field is correct and amounts to smth like this: 05132004 i. e. mmddyyyy (without separation). But not all of them are correct... some of them can be of such sort: 051 . When i "parse" the string (with SUBSTRING function etc.) i form the string 05/13/2004 for example and its ok to convert it into datetime. But 05/1/ is not convertable and exception raises. You can tell me that I can handle it checking whether the string is correct or not before converting, but its not a good way (moreover, as i have told before, there are variety of such tasks, not only date conversion...). It would be very cute to use smth. like try { and to place conversion here...}.Using of @@error does not match, because exception raises anyway and my whole application will not work...
X002548
Not Just a Number
15586 Posts
Posted - 2004-06-01 : 14:46:59
Sounds like a good job for a User Defined Function...or
DECLARE @x varchar(8)SELECT @x = '20040531'SELECT CASE WHEN ISDATE(@x) = 1 THEN SUBSTRING(@x,5,2)+'/'+ SUBSTRING(@x,7,2)+'/'+ SUBSTRING(@x,1,4) ELSE '01/01/0001' END
But why not handle it in the fromt end?Also, why is the date varchar?You've already seen data integrity issues...What other types of conversion you talking about?Brett8-)