Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handling HEIGHT field, ie. X 'XX "

Status
Not open for further replies.

tlieu

IS-IT--Management
Jan 13, 2002
15
0
0
US
<cfset =&quot;newbie&quot;>

<cf_question>
If I wanted to input height fields into my db and then output/order it how would I do it?


 
Hey Tlieu

I'm not aware of any Height datatype, you could though convert the inches to/from the decimal like this:

where 'heightfeet' is the height in feet and 'heightinches' is the inches and 'Dheight' is the Decimal equivelant.


<cfset heightfeet = 5>
<cfset heightinches = 11>
<cfset cv = heightinches / 12 >
<cfset Dheight = heightfeet + cv>

Dheight would now be ready to enter into a DB and will sort correctly for the heighest or lowest.

you simply change your 12 inch scale into a 10 0.91666th of an inch scale making it store easily in a decimal world.

you would convert it back from the DB like this:

<cfset heightfeet = #int(DBheight)# >
<cfset cv = DBheight - heightfeet >
<cfset heightinches = cv * 12 >

Then you can display them anyway you want. These conversions could probably be written more compactly but I didn't want to think about it. :)

Hope it helps.
Have fun......

 
Thanks for the suggestion, but unfortunately I need it in XX'XX&quot; format =(

I was thinking perhaps I could use two fields HEIGHT1 and HEIGHT2 and somehow order it first on height1 then height 2?

Comments or suggestions?
 
Tlieu

The method I suggested lends itself well to that format, the database is going to see it as one number, but your app is going to see it as 2 numbers one for feet and one for inches. Then it is up to you to display it as X'XX&quot; . Just insert those characters. I think it would look like this with the above conversions:

<cfset outheight = &quot;#heightfeet#''#heightinches#&quot;&quot;&quot; >
#outheight#

something like that. just figure out how to escape the ' and the &quot; I haven't had coffee yet. If you search this Forum you will find it.

 
The previous answer pointed to processing a conversion
prior to storage.
You can process calculations in an order by clause
of your SQL.

<cfquery>
SELECT Height
FROM Table1
ORDER BY CAST(LEFT(Height, CHARINDEX(CHAR(39), Height) - 1) AS INT) * 12 + CAST(REPLACE(RIGHT(Height, LEN(Height) - LEN(LEFT(Height,
CHARINDEX(CHAR(39), Height)))), CHAR(34), '') AS INT)
</cfquery>

This was written in MSSQL2000

Must be a simpler way, but off the top of my head
 
The previous answer pointed to processing a conversion
prior to storage.
You can process calculations in an order by clause
of your SQL.

<cfquery>
SELECT Height
FROM Table1
ORDER BY CAST(LEFT(Height, CHARINDEX(CHAR(39), Height) - 1) AS INT) * 12 + CAST(REPLACE(RIGHT(Height, LEN(Height) - LEN(LEFT(Height,
CHARINDEX(CHAR(39), Height)))), CHAR(34), '') AS INT)
</cfquery>

This was written in MSSQL2000

Must be a simpler way, but off the top of my head
 
To escape characters
get the code
39 FOR '
AND
34 FOR &quot;
<cfset mystring = &quot;12#CHR(39)#6#CHR(34)#&quot;>
 
...Duh, stupid me, I didn't fully understand your function at the beginning; now it seems utterly clear.

Wanted to write to thank TLHAWKINS and CFK for providing me with the solution to my dilemma.

Deeply Appreciative!

 
PAGE1:
<form name=&quot;height&quot; method=&quot;post&quot; action=&quot;saveheight.cfm&quot;>
<input type=&quot;text&quot; name=&quot;heightfeet&quot;>Feet
<input type=&quot;text&quot; name=&quot;heightinches&quot;>Inches
<input type=&quot;submit&quot; value=&quot;Submit&quot;>
</form>

PAGE2:
<cfset heightfeet = #heightfeet#>
<cfset heightinches = #heightinches#>
<cfset cv = heightinches / 12 >
<cfset dheight = heightfeet + cv>
<cfquery datasource=&quot;measure&quot;>
INSERT INTO height(dheight) values(#dheight#)
</cfquery>


<!-- cfinsert datasource=&quot;test&quot;
tablename=&quot;height&quot;
formfields=&quot;dheight&quot;>

QUESTION: Why does the INSERT method work whereas the cfinsert datasource does not? In the cfinsert case, I end up inserting nothing into the database. I actually wanted to just use the line <cinfsert datasource=&quot;measure&quot; tablename=&quot;height&quot;> to make things simple but it kept telling me that I should have a heightfeet and heightinches in my DB, which I didn't; I wanted to only have dheight...so I thought by using the formfields=&quot;dheight&quot; I could keep things syntactically correct...

Comments and suggestions from the CFM gurus?

 

formfields should work, but as far as correct programming it is a good idea to use regular SQL. It will be easier to work on later and it will be more portable. If Insert works go with Insert. :) my 2 cents.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top