tags:

views:

670

answers:

4

A form field value like 45,234 is to be inserted into the DB as 45234. When I try to do insert for 45,234, all it gets into the DB is 0.Note:- I am able to insert 45.234as 45.234 in the SQL DB.

The form field is named costOfShares, which is to be inserted into the table ShareVales (SQL Server 2005). I am using CF8.

SQL

table:-ShareVales; field:-costOfShares; DataType:-float

ColdFusion (form page)

<li>
  <cfinput size="8" maxlength="20" name="costOfShares" 
    id="costOfShares" value="#share.GetcostOfShares()#">
</li>

Share.cfc:-

<cfcomponent> 
    <cfscript> Variables.CostOfShare </cfscript> 
<cffunction name="GetCostOfShare" returntype="numeric" access="public" output="false">         
   <cfscript> variables.CostOfShare; </cfscript>
</cffunction>

<cffunction name="SetCostOfShare" retuntype="void" access="public" output="false">    
   <cfargument name="costOfShare" type="string"  required="true">
      <cfscript> variables. costOfShare = arguments. costOfShare; </cfscript>
  </cffunction>
</cfcomponent> 

ColdFusion (query page)

  <cffunction>
    <cfargumnet>
    <cfquery …>
      INSERT INTO ShareVales(shareUnitId, costOfShares) 
      VALUES (
        <cfqueryparam cfsqltype="cf_sql_integer"
                      value="#arguments.share.GetshareUnitId()#">,
        <cfqueryparam cfsqltype="cf_sql_float"
                      value="#arguments.share.GetcostOfShares()#">);
    </cfquery>
</cffunction>

When I use the following directly in the Query Editor:

INSERT into share(setCostOfShare)
values(Cast(replace('5,322,444',',','') as float))

it succeeds. The error is while using cfquery/cfqueryparam in the ColdFusion template.

So, how do insert the 45234 when the form field costOfShares contains a value with commas?

+1  A: 
LSParseNumber("45,234", "French (standard)")

Or if "French (standard)" is already your system standard system default locale, just use:

LSParseNumber("45,234")

Or...

<cfset setLocale("French (standard)")>
<cfset value = LSParseNumber("45,234")>

They'll return 45.234 (if you want 45234 instead, use US locale, see doc!)

I'd suggest you to store costOfShares as a valid ColdFusion numeric value (aka, use '.' instead of ','), and only use LSNumberFormat() or LSLSCurrencyFormat() to display the value on the view. That way you don't even have to LSParse the number, unless the number is created by the user, and he uses ','.

Henry
Can I use......INSERT ....INTO....VALUES(...,<cfqueryparam cfsqltype="cf_sql_float" value="LSParseNumber(#arguments.share.GetcostOfShares()#)">);
vas
Ya, sure... as long as your default locale can decode the comma as symbol for decimal. If not, you'll have to either setLocale() first, or pass in the locale as the 2nd argument to LSParseNumber().
Henry
Henry I have added the form page code, and still I am getting the value of "0" instead of say "45,334".Further the locale US,Is providing "French" advisible
vas
The question is, where does 45,234 come from? I mean, if you're using Share.cfc object, it should always return 45.234 when u call getCostOfShares(), reason being you can then use that numeric value meaningfully, either do additional calculation in CF or persisting the value with <cfqueryparam>. So if you received "45,234" from a form, then you should LSParseNumber() that either before you setting it to the Share object, or inside Share.setCostOfShare("45,234"). If you want to display the comma, then use LSNumberFormat() on the view when needed.
Henry
for the INSERT statement it was value="#arguments.Share.setCostOfShare()#">) ;Can i use value="#arguments.Share.LSParseNumber(setCostOfShare())#">) ;
vas
or value="#arguments.Share.setCostOfShare(LSParseNumber())#">) ;
vas
or value=LSParseNumber("#arguments.Share.setCostOfShare()#")>) ;
vas
... it should be <cfqueryparam value="#LSParseNumber(arguments.Share.getCostOfShare())#">And I thought you wanted the comma to be the decimal originally??? :S
Henry
But a better practice is to use <cfqueryparam value="#arguments.Share.getCostOfShare()#"> , and make sure the getCostOfShare() method return valid CF numeric value (aka no freaking comma).
Henry
+1. I wish I could up-vote this more than once. :-)
Tomalak
Hi Henry I want to define the "getCostOfShare" in the Share.cfc/cfscript/Variables.
vas
Can I have it as .....<cfcomponent> <cfscript> Variables.CostOfShare</cfscript>
vas
in the same Share.cfc.......<cffunction name="GetCostOfShare" returntype="numeric" access="public" output="false"> <cfscript> variables.CostOfShare; </cfscript> </cffunction></cfcomponent>
vas
it was throwing an error...................Cannot assign a value to a function. Unable to assign a value to the function LSParseNumber on line 3....
vas
Vas... do you really expect me to be able to help you with messy code in comment?? Edit your original question or ask a new question, and post your code in a code block ok? :)
Henry
Sorry Henry!I have corrected it.
vas
+1  A: 

presumably the datatype is a float or something. comma's are not valid characters in a float (unless for European locales). You probably want instead to get the value out of the DB with a comma (but not necessarily store it with one). That would probably mean using the COVERT function during a SELECT:

http://technet.microsoft.com/en-us/library/ms174450.aspx
mlathe
Yes datatype is a float.I want to INSERT a number with comma in it.Example:- when i insert the value 45,234(with comma) in the form only '0'is populated int he SQL DB.Can I use CAST()/CONVERT in the INSERT query ?
vas
I think part of the confusion is are you trying to submit a European style number (like 3,5 or 1.003,14) or if you are trying to submit US type numbers with commas (ie 1,000.00 or 24,123.45). If you are doing the latter i don't think it's possible. The DB stores the numbers in binary format so you can't store the commas. Rather your question might really be "how do i get the numbers out so they look nice", if that's the case then you need to do that during a SELECT to convert the float into a nice looking string.
mlathe
BTW floats are horrible datatypes, never use them if you are doing calculations. Use decimal or numeric instead unlesss you like getting rounding errors.
HLGEM
+2  A: 

If they type a comma, strip it out first before trying to insert.

HLGEM
INSERT into share(setCostOfShare)values(Cast(replace('5,322,444',',','') as float)).........is an success in the DB-SQL-query editor....but the error is while using cfquery/<cfqueryparam > in the coldfusion template.
vas
In Germany, for example, the comma signifies the start of decimal places, so you can't just strip it out. Especially since the DB field is `float`, which implies that decimal places *are* significant. Parsing the input as a number in ColdFusion *before* the INSERT is the only clean way of doing it.
Tomalak
A: 

Not sure if this will help or not, but just put the replace inside of cfqueryparam. That way it hands SQL Server the "already cleaned up version".

CAST(<cfqueryparam cfsqltype="cf_sql_float"                                    
                   value="#REPLACE(arguments.share.GetcostOfShares()',',','')#"> AS FLOAT)

Or, have CF run a replace on the variable and clean it up before you even try the query.

Or, have CF run a replace on the variable and clean it up before you even pass it in as an argument.

Eric
use the correct function, which should be LSParseNumber()
Henry
ERROR:- Invalid data arguments.share.GetcostOfShares() for CFSQLTYPE CF_SQL_FLOAT.
vas
Eric
I wouldn't consider this a down vote (I assume by Henry?) LSParsenumber was designed for this type of operation, true but it also causes an exception when the value cannot be converted. Replace fails gracefully by not replacing anything. I wouldn't suggest using lsparsenumber without a try/catch wrapper.
Travis
using Replace was passing "0" values instead of say 4,123.Though a value of say 4.123 is going thro fine.
vas