Including numbers in text string

  • Hi All,

    How could I concatenate a numeric value such as 12.25 in a text string?

    Currently I am casting the value as a varchar. This truncates any figures after the decimal point. How can I go about preserving this please?

  • Generically:

    'text string characters here' + Cast(12345.25 as varchar(12)) + ' more characters here'

     

    In a Select statement from a table

    Select ColumnVarChar + Cast(ColumnMoney as varchar(12)) From YourTable

     

    In a stored procedure using variables

    Declare

         @descriptiontext     varchar(255),

         @numbervalues     money,

         @finalstring         varchar(512)

     

    Set @descriptiontext = 'test text characters: '

    Set @numbervalues = 12.25

    Set @finalstring = @descriptiontext + Cast(@numbervalues as varchar(12))

     

     

    [font="Arial"]Clifton G. Collins III[/font]

  • colin's solution should work. Definitely cast as a specific size.

  • The other thought, if you are setting the 12.25 to a int variable or column, change to the appropriate datatype that will handle decimals, (i.e. money or decimal).

     

    [font="Arial"]Clifton G. Collins III[/font]

  • The only case I can see when numbers after decimal point may be got truncated is when you use STR function without {length , decimal} specified.

    Something like this: STR(12.45)

    Check BOL for complete syntax for STR function.

    _____________
    Code for TallyGenerator

  • Thanks all - I originally had the fields only the values as datatype int. I have now changed this to decimal(4,2).

Viewing 6 posts - 1 through 5 (of 5 total)

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