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 7066227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:03:30+00:00 2026-05-28T05:03:30+00:00

How would I return multiple values (say, a number and a string) from a

  • 0

How would I return multiple values (say, a number and a string) from a user-defined function in SQL Server inside a query?

e.g select col1, dbo.function1(col2) from table

and the result to be

Col1 |        Col2       |         Col3
-----+-------------------+---------------------
x    | Num from function | String from function

I don’t want to call the function 2 times because i use some complex code and i don’t want to execute it twice. Just to take the final 2 results in the Return

  • 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-28T05:03:30+00:00Added an answer on May 28, 2026 at 5:03 am

    Extract from – http://www.sqlteam.com/article/returning-complex-data-from-user-defined-functions-with-cross-apply

    Returning multiple values from a UDF can be done fairly easily in SQL
    Server, but we must return those values as a virtual table

    --Sample Table
    CREATE TABLE emails 
    (
        ID INT PRIMARY KEY, 
        EmailAddress VARCHAR(100)
    )
    GO
    
    --Dummy Data
    INSERT INTO emails
    SELECT 1,'jeff@jeff.com' UNION ALL
    SELECT 2,'yak@sqlteam.com' UNION ALL
    SELECT 3,'billg@microsoft.com'
    
    --UDF Creation
    CREATE FUNCTION EmailParse (@email VARCHAR(1000))
    RETURNS @t TABLE (UserName VARCHAR(20), Domain VARCHAR(20))
    AS
    BEGIN
        DECLARE @i INT
    
        SELECT @i = charindex('@', @email,1);
    
        IF (@i > 1)
            INSERT INTO @t VALUES (LEFT(@email,@i-1), SUBSTRING(@email,@i+1,20))
        ELSE
            INSERT INTO @t VALUES (NULL,NULL)
    
        RETURN 
    END
    
    
    --UDF Usage
    SELECT 
        emails.ID, s.Username, s.Domain
    FROM 
        emails
    CROSS APPLY 
    EmailParse(emails.EmailAddress) s
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I return multiple values (say, a number and a string) from a
Hi Please can someone show me how to return multiple values from a function?
I would like to return multiple values from stored procedure (attached as below) by
Let us say for a moment that C# allowed multiple return values in the
So, Python functions can return multiple values. It struck me that it would be
Given a table-valued function such as dbo.Split() from T-SQL: Opposite to string concatenation -
I can't return multiple values in javascript return statement. Would it be useful to
I would like to return multiple values in my case statement, such as :
I was wondering what the parameters from this method would return. - (void) observeValueForKeyPath:(NSString
How can I build a function slice(x, n) which would return a list of

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.