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

  • SEARCH
  • Home
  • 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 908735
In Process

The Archive Base Latest Questions

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

I’m trying to write a UDF to translate a string that is either a

  • 0

I’m trying to write a UDF to translate a string that is either a guid or a project code associated with that guid into the guid:

CREATE FUNCTION fn_user_GetProjectID 
(
    @Project nvarchar(50)
)
RETURNS uniqueidentifier
AS
BEGIN

    declare @ProjectID uniqueidentifier

    BEGIN TRY
        set @ProjectID = cast(@Project as uniqueidentifier)
    END TRY
    BEGIN CATCH
        set @ProjectID = null
    END CATCH

    if(@ProjectID is null)
    BEGIN
        select  @ProjectID = ProjectID from Project where projectcode = @Project
    END

    return @ProjectID

END

This works fine if the above code is embedded in my Stored Procedures, but I’d like to make a function out of it so that I follow DRY.

When I try to create the Function, I get errors like this:

Msg 443, Level 16, State 14, Procedure fn_user_GetProjectID, Line 16
Invalid use of side-effecting or time-dependent operator in 'BEGIN TRY' within a function.

Does anyone have an idea how I can get around this error?

Edit: I know I can’t use Try-Catch in a Function, I guess a simplified questions would be, is there a way to do a cast that will just return NULL if the cast fails, instead of an error?

  • 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-15T16:45:49+00:00Added an answer on May 15, 2026 at 4:45 pm

    From MSDN:

    A column or local variable of
    uniqueidentifier data type can be
    initialized to a value in the
    following ways:

    By using the NEWID function.

    By converting from a string constant
    in the form
    xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,
    in which each x is a hexadecimal digit
    in the range 0-9 or a-f.

    For example,
    6F9619FF-8B86-D011-B42D-00C04FC964FF
    is a valid uniqueidentifier value.

    You can use pattern matching to verify the string. Note that this won’t work for specific encoding that reduces the size of the GUID:

    declare @Project nvarchar(50) 
    
    declare @ProjectID uniqueidentifier 
    declare @HexPattern nvarchar(268) 
    
    set @HexPattern =  
        '[A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9]' +  
        '[A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9]' +  
        '[A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9]' +  
        '[A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9][A-F0-9]' 
    
    /* Take into account GUID can have curly-brackets or be missing dashes */
    /* Note: this will not work for GUIDs that have been specially encoded */
    set @Project = '{' + CAST(NEWID() AS VARCHAR(36)) + '}'
    
    select @Project
    
    set @Project = REPLACE(REPLACE(REPLACE(@Project,'{',''),'}',''),'-','')
    
    /* Cast as uniqueid if pattern matches, otherwise return null */ 
    if @Project LIKE @HexPattern 
      select @ProjectID = CAST(
             SUBSTRING(@Project,1,8) + '-' + 
             SUBSTRING(@Project,9,4) + '-' + 
             SUBSTRING(@Project,13,4) + '-' + 
             SUBSTRING(@Project,17,4) + '-' + 
             SUBSTRING(@Project,21,LEN(@Project)-20)
             AS uniqueidentifier) 
    
    select @ProjectID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.