Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 809907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:48:08+00:00 2026-05-15T00:48:08+00:00

This question could easily take multiple paths, so I will hit the more specific

  • 0

This question could easily take multiple paths, so I will hit the more specific path first. While working with SQL Server 2005, I’m trying to create a scalar function that acts as a ‘TryCast’ from varchar to int. Where I encounter a problem is when I add a TRY block in the function;

CREATE FUNCTION u_TryCastInt
(
   @Value as VARCHAR(MAX)
)
RETURNS Int
AS
BEGIN
   DECLARE @Output AS Int

   BEGIN TRY
      SET @Output = CONVERT(Int, @Value)
   END TRY
   BEGIN CATCH
      SET @Output = 0
   END CATCH

   RETURN @Output
END

Turns out theres all sorts of things wrong with this statement including “Invalid use of side-effecting or time-dependent operator in ‘BEGIN TRY’ within a function” and “Invalid use of side-effecting or time-dependent operator in ‘END TRY’ within a function”. I can’t seem to find any examples of using try statements within a scalar function, which got me thinking, is error handling in a function is possible?

The goal here is to make a robust version of the Convert or Cast functions to allow a SELECT statement carry through depsite conversion errors. For example, take the following;

    CREATE TABLE tblTest
    (
        f1 VARCHAR(50)
    )
    GO

    INSERT INTO tblTest(f1) VALUES('1')
    INSERT INTO tblTest(f1) VALUES('2')
    INSERT INTO tblTest(f1) VALUES('3')
    INSERT INTO tblTest(f1) VALUES('f')
    INSERT INTO tblTest(f1) VALUES('5')
    INSERT INTO tblTest(f1) VALUES('1.1')

    SELECT CONVERT(int,f1) AS f1_num FROM tblTest

    DROP TABLE tblTest

It never reaches point of dropping the table because the execution gets hung on trying to convert ‘f’ to an integer. I want to be able to do something like this;

SELECT u_TryCastInt(f1) AS f1_num FROM tblTest

fi_num
__________
1
2
3
0
5
0

Any thoughts on this? Is there anything that exists that handles this? Also, I would like to try and expand the conversation to support SQL Server 2000 since Try blocks are not an option in that scenario.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:48:09+00:00Added an answer on May 15, 2026 at 12:48 am

    Check if you can convert to int first, check out the IsInteger function here: IsNumeric, IsInt, IsNumber It will work on 2000 and up

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 514k
  • Answers 514k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It sounds like you need another table in your database… May 16, 2026 at 6:15 pm
  • Editorial Team
    Editorial Team added an answer I'd make that server listen on 127.0.0.1 and then use… May 16, 2026 at 6:15 pm
  • Editorial Team
    Editorial Team added an answer In cURL, there's the curl_getinfo function, that returns some info… May 16, 2026 at 6:15 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I thought this would be something I could easily google, but maybe I'm not
I am currently working on a project with specific requirements. A brief overview of
This is a general question about how limiting are web development frameworks such as
I know I could easily write a function and put it in the application
I've been working for years with VS's debugger, but every now and then I
Can/Should my POCO's encompass multiple Databases if applicable? The reason I ask is that
This is a little contrived, but say I have a class interface like this:
I'd like to create a random boolean in JavaScript, but I want to take
I'm just curious as to whether such as tool exists (free, open source, or
I am just setting up a simple library application, basically a collection of Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.