Converting a number to feet & inches

后端 未结 2 1155
不知归路
不知归路 2021-01-26 03:22

I want to take a number from a table ( using sql server 2008 ) such as 720 and convert it to feet and inches with this:

Format(Val(Length) \\ 12, \"00\' \") &a         


        
相关标签:
2条回答
  • 2021-01-26 03:54

    I think you are looking for this

    DECLARE @value INT = 720
    SELECT CONVERT(VARCHAR(20),@value /12) + '''' + CONVERT(VARCHAR(20),@value %12)+'"'
    
    0 讨论(0)
  • 2021-01-26 04:13

    Although you can use format() for this, it is not necessary.

    When creating strings with a particular format, I like to use replace() with wildcards in the string. This lets me easily control the format to see what is being produced:

    select replace(replace('<feet>'' <inches>"',
                           '<feet>', val(length) / 12),
                   '<inches>', val(length) % 12)
    

    This assumes that the expression val(length) returns an integer. If the column is just called length, then you would use length instead of val(length).

    0 讨论(0)
提交回复
热议问题