Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 convert numeric value to feet and inches

Author  Topic 

Brittney10
Posting Yak Master

154 Posts

Posted - 2014-06-03 : 17:05:27
I'm needing easily convert a numeric value to feet and inches. for example 12.4 would become 1 4 (space inbetween). Thanks for the hlp in advance.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-06-03 : 22:11:35
what does the numeric 12.4 represent ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

adsingh82
Starting Member

20 Posts

Posted - 2014-06-04 : 01:26:00
Please check my below code that gives the expected value

DECLARE @Value DECIMAL(18,2)

SET @Value = 12.4

;WITH Derived(Feet, Inches, RemFeet)
AS
(
SELECT CAST(CAST(@Value AS INT)/12 AS VARCHAR(18))
, REPLACE(CAST(@Value - CAST(@Value AS INT) AS FLOAT), '0.', '')
, CAST(@Value AS INT) % 12
)
SELECT CASE WHEN RemFeet > 0
THEN Feet + ' ' + CAST(RemFeet AS VARCHAR(18)) + Inches
ELSE Feet + ' ' + Inches
END AS [Feet And Inches]
FROM Derived;


Regards,
Alwyn.M
Go to Top of Page

ScottPletcher
Aged Yak Warrior

550 Posts

Posted - 2014-06-04 : 16:21:40
I agree; we must know the data type.

How do we distinguish between "12.1" and "12.10"?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-06-04 : 20:54:07
if 12.4 means 12 inch and 4 inch, that is an rather odd way of entering it


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -