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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:11:17+00:00 2026-05-11T22:11:17+00:00

Is possible to pass type as param in SQL function? thank in advance.

  • 0

Is possible to pass type as param in SQL function?

thank in advance.

  • 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-11T22:11:17+00:00Added an answer on May 11, 2026 at 10:11 pm

    the parameter type is defined within the function header, as a result it is always the same within the function. When you pass in a parameter that is a different types than the defined type parameter type, SQL Server does its best to that to the defined function parameter type.

    I have a few functions that I want to work just like what you are after. The way I work around this issue is to define the function parameter as a varchar(x), and then issue any necessary CVONVERT() or formatting on the incoming parameter in the function call the parameter when you call it.

    CREATE FUNCTION dbo.QuoteValue
    (
         @InputStr      varchar(8000)  --value to format
    )
    RETURNS
    varchar(8000)
    AS
    BEGIN
        RETURN COALESCE(''''+@InputStr+'''','null')
    END
    GO
    
    DECLARE @DateValue  datetime
    SET @DateValue=GETDATE()
    PRINT '@DateValue='+dbo.QuoteValue(@DateValue)
    PRINT '@DateValue='+dbo.QuoteValue(CONVERT(varchar,@DateValue,121))
    

    Depending on what you are after, if you want to work with dates or numbers, make the function parameter varchar(x) and within the function use:

    • ISDATE ( expression ) – ISDATE returns 1 if the input expression is a valid date; otherwise, it returns 0. The following table shows the return values for a selection of examples.
    • ISNUMERIC (Transact-SQL) – ISNUMERIC returns 1 when the input expression evaluates to a valid integer, floating point number, money or decimal type; otherwise it returns 0. A return value of 1 indicates that expression can be converted to at least one of the numeric types.

    here is an example of how to handle dates…

    CREATE FUNCTION dbo.DateXYZ
    (
         @InputStr      varchar(50)  --date value manipulate
    )
    RETURNS
    datetime
    AS
    BEGIN
        DECLARE @DateValue  datetime
    
        IF ISDATE(@InputStr)=1
        BEGIN
            SET @DateValue=@InputStr
            --do some datetime manipulation here
        END  
        RETURN @DateValue --will return null if not given a valid date
    END
    GO
    

    You could use it as follows:

    DECLARE @X varchar(50)
    DECLARE @Y varchar(50)
    DECLARE @BadDate datetime
    DECLARE @GoodDate datetime
    SET @X='bad date!!'
    SET @Y='1/1/2009'
    SET @BadDate=dbo.DateXYZ(@X)
    SET @GoodDate=dbo.DateXYZ(@Y)
    SELECT @BadDate,@GoodDate
    

    you could also pass in a parameter to signify what you are passing in:

    CREATE FUNCTION dbo.AnyType
    (
         @InputStr      varchar(8000)  --general purpose value
        ,@Type          char(1)         --"I"nt, "F"loat, "D"atetime, "S"tring, etc...
    )
    RETURNS
    varchar(8000)
    AS
    BEGIN
        DECLARE @ReturnValue  varchar(8000)
    
        IF @Type='I' --int
        BEGIN
            DECLARE @IntValue int
            SET @IntValue=@InputStr
            --do some int manipulation here
            SET @ReturnValue=@IntValue
        END  
        ELSE IF @Type='F' --float
        BEGIN
            DECLARE @FloatValue float
            SET @FloatValue=@InputStr
            --do some float manipulation here
            SET @ReturnValue=@FloatValue
        END  
           ELSE IF @Type='D' --datetime
        BEGIN
            DECLARE @DatetimeValue  datetime
            SET @DatetimeValue=@InputStr
            --do some datetime manipulation here
            SET @ReturnValue=CONVERT(varchar(23),@DatetimeValue,121)
        END   
        --etc...
        --etc...
    
        RETURN @ReturnValue
    END
    GO
    

    Functions can only return a single fixed type, make it varchar(x) and have the caller assign it to a variable of the the proper data type, or CAST it in a result set as necessary.

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

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • 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 var fOldIndex: integer = -1; procedure TForm1.ListBox1MouseMove (Sender: TObject; Shift:… May 11, 2026 at 11:28 pm
  • Editorial Team
    Editorial Team added an answer Never use * syntax in production code. Rewrite your query… May 11, 2026 at 11:28 pm
  • Editorial Team
    Editorial Team added an answer Ok. You can use this class here (posted on pastebin… May 11, 2026 at 11:28 pm

Related Questions

If I want to match x.gif and y.gif , is it possible to pass
Looking for a way to pass an associative array to a method. I'm looking
Suppose you have a python method that gets a type as parameter; is it
The goal: To create a .NET dll i can reference from inside SQL Server

Trending Tags

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

Top 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.