SQL Function variable quote passing

Posted on 2011-10-04
Last Modified: 2012-05-12
I have a function that I need to pass a variable of 'SHM' but I cannot figure out how to get the single quotes to pass in variable. Thanks in advance. I am using SQL Server 2000.

Function Query

select * from dbo.sales_union_func  
'SHM' -- Needs to pass as 'SHM' not SHM

Open in new window

CREATE function sales_union_func (


) returns table AS return

select * from stock where assoc=@assoc

Open in new window

Question by:ubsmail
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    are you 200% sure that you need to pass quotes ?

    if yes:

    select * from dbo.sales_union_func  ( '''SHM'''  )

    Author Comment

    I am sure but that didnt work. Any other suggestions?
    --this returns results--
    select top 1000 * from stock where assoc='SHM'

    Open in new window

    CREATE function assoc_test (
        @assoc char
    ) returns table AS return
    select top 1000 * from stock where assoc=@assoc
    select * from assoc_test ('''SHM''')
    --Does not return results

    Open in new window

    LVL 59

    Accepted Solution

    In the question, you do NOT have any data type defined for @assoc and then in the above you have just char. You are likely passing just a single quote for comparison. As angeliii eluded to, you do NOT need to pass the quotes if the parameter is defined with the proper data type to match the data type of the column assoc; therefore, this should work for example:

    CREATE function dbo.assoc_test (
        @assoc char(3)
    ) returns table AS return
    select top 1000 * from stock where assoc=@assoc

    select * from dbo.assoc_test('SHM');

    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    I agree, if you want to pass a string, once you define the variable as string, it's ok to pass it like shown.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    I recently came across an interesting Question In EE ( and was puzzled about how to achieve that using SSIS out of the box tasks, which was i…
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now