value of select column as an argument in a function

  • Hi all,

    Please bear with me for this quite beginner question. I am having problem to put a selected column as an argument of a function. Like

    Select col1,col2,cast(col3 as decimal(8,col1)) from table1 inner join....How do I put the value of col1 in a function? There must be a way to do it, right?

    thank your very much for your help in advanced,

  • You can do it only with scalar functions.

    select dbo.MyFunction(Col1) as Col1Mod, Col2,...

    FROM ...

    _____________
    Code for TallyGenerator

  • Thank you very much for replying. What is scalar function? If I need to use one column's value in the decimal(p,s), what should I do to accomplish that?

  • Type "CREATE FUNCTION" in BOL, there is a lot of explanatons over there.

    _____________
    Code for TallyGenerator

  • I need to create a function so that it will take the value of Col1 and return something replacing the decimal(p,s). Am I in the right direction?

    Thank you very much for the reply,

  • I did not get the question.

    _____________
    Code for TallyGenerator

  • What I meant is in order to successfully using something like cast(#temp1.col1 as decimal(8,col2)), I should create a user defined function that will take the col2 as parameter and return an integer to be inserted as s in deciaml(p,s). Then the statement would become cast(#temp1.col1 as decimal(8,MyFunction(col2))).

    Am I correct?

  • decimal(p,s) is not a function.

    It's a type definition. You cannot insert any value inside type definition.

    _____________
    Code for TallyGenerator

  • I am stuck..., I thought what I need to do is create a user defined function.....

    I need to take one column(columnA) from the select query and use it to format the other column(ColumnB). Column A is an integer which indicates how many decimal position ColumnB should be formatted with. ColumnB is a datatype decimal 5(8,2) at this time.

    What is the method I need to use?

    Thank you for the reply,

  • You really mean you want to have different data types in one column???

    You must be kidding...

    But if what you realy need is rounding, then functions ROUND is there for your service.

    _____________
    Code for TallyGenerator

  • LTRIM(STR(ColumnB,20,ColumnA))

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Got it, Thank you very much for your help, David.  I knew there must be a way to do it.  I am sorry I didn't explain it more clearly in the first place.

    I wish I can write the queries as fluent as you guys.

    Cheers,

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply