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
I think you are looking for this
DECLARE @value INT = 720
SELECT CONVERT(VARCHAR(20),@value /12) + '''' + CONVERT(VARCHAR(20),@value %12)+'"'
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)
.