mercredi 25 novembre 2015

Create SQL function for dynamic input values

I have a query:

SELECT Substring(warehouse_id, 1, Len(warehouse_id) - 1)Warehouse_Id FROM   (SELECT (SELECT Cast(ur.warehouse_id AS VARCHAR) + ',' 
        FROM   wms_user_rights ur 
        WHERE  ur.delete_flag = 'N' 
               AND end_date >= Getdate() 
        FOR xml path('')) Warehouse_Id)mm 

It displays this result:

Warehouse_id
------------
11,12

If I don't use that substring, it give this result:

    Warehouse_id
    ---------------
    11

    12

So, my question is:

How do I create a function that will handle all this process that can be handled in one function?

Something like this would be simple:

SELECT FuncName(ur.warehouse_id) FROM  wms_user_rights ur 

I am using sql server 2005

Currently I tried with SubString but I want a Simple Function so that I can use it multiple times

Aucun commentaire:

Enregistrer un commentaire