NULL when casting string values to decimal in Hive - hadoop

I'm using Hive 0.13 and I have in a STRING column of my table values like 1.250,99
I want to cast these values into decimal, so I must replace "." by "" and "," by "." The result would be 1250.99
This is my hql sentence:
cast(regexp_replace(regexp_replace(price, '\\.',''), ',','.') as decimal(18,6))
But it returns NULL, I suppose because the conversion does not succeed. What is the problem?
If I don't do the conversion, it returns the expected string.
UPDATE
My problem was that there were white spaces in the column, so it could not convert it into decimal value. I have used trim function before doing the conversion.

Try this:
select cast(regexp_replace(regexp_replace('1.234,56','\\.',''),'\,','\.') as decimal(10,2));

Related

How to pass contant string value for a column in SUMMARIZECOLUMN

I want to pass a constant string value i.e. Not Applicable for one of the column values that will be used in SUMMARIZECOLUMNS function. But I am not find any format in how to do the same.
SUMMARIZECOLUMNS( table_name[column_name], "Custom_Column_Name", "Not Applicable")

ADO Recordset filter comparing two fields

How can I filter a recordset by comparing two fields?
For a given ADO Recordset with n fields (Field1, Field2,...,Fieldn)
I used to filter a field against a value:
rs.Filter = "Field1 = 'something'"
But what I need to do is something like this:
rs.Filter = "Field1 = Field2"
Is that possible?
The criteria string is made up of clauses in the form FieldName-Operator-Value
Value is the value with which you will compare the field values (for example, 'Smith', #8/24/95#, 12.345, or $50.00). Use single quotes with strings and pound signs (#) with dates. For numbers, you can use decimal points, dollar signs, and scientific notation. If Operator is LIKE, Value can use wildcards. Only the asterisk (*) and percent sign (%) wild cards are allowed, and they must be the last character in the string. Value cannot be null.
This suggests that comparing fields to each other is not supported. Value must be a literal.

SQL*loader infile replace function

I am trying to change SQL*loader files to replace a comma in decimal numbers with a dot.
How the loader data file looks
Pamatstudijas,Lauksaimniecibas,4,16.01.2006,70,"5,38"
My control file
load data
infile 'C:\Users\Students\Documents\ricardshercbergs_itia\12\izmaksas1.dat'
into table kartoteka
fields terminated by "," Optionally Enclosed By '"'
TRAILING NULLCOLS
(stud_veids char,
fakultate char,
kurss Integer External,
izmaksas_datums date 'dd.mm.yyyy',
stipendija Integer External,
kompensacija "REPLACE (:kompensacija,',','.')" Decimal External ,
nauda_kopa ":stipendija+:kompensacija")
This: kompensacija "REPLACE (:kompensacija,',','.')" Decimal External , doesnt work as I expected. I get the error:
SQL*Loader-350: Syntax error at line 11.
Expecting "," or ")", found keyword decimal.
kompensacija "REPLACE (:kompensacija,',','.')" Decimal External ,
According to the example control file in the documentation the definition of the datatype comes before the transformation of the column, i.e. your spec should be:
kompensacija Decimal External "REPLACE (:kompensacija,',','.')"
It looks possible you're using REPLACE() to switch around what character is a decimal point and which is the thousands separator. If this is the case you can use TO_NUMBER, which makes it more obvious what you're doing and will fail with the correct error if the conversion is not possible:
kompensacija decimal external "to_number(:kompensacija, '99D99', 'NLS_NUMERIC_CHARACTERS = '',.''')"

FoxPro convert currency to numeric

I'm using Visual FoxPro and I need to convert currency amount into numeric. The 2 columns in the table are tranamt(numeric,12,2) and tranamt2(character)
Here's my example:
tranamt2=-$710,000.99
I've tried
replace all tranamt with val(tranamt2)
and
replace all tranamt with val(strtran(tranamt2, ",",""))
both results give me zero. I know it has something to do with the negative sign but I can't figure it out. Any help is appreciated.
Try this:
replace all tranamt with VAL(STRTRAN(STRTRAN(tranamt2, "$", ""), ",", ""))
This removes the dollar sign and comma in one shot.
need to convert currency amount into numeric
tranamt(numeric,12,2) and tranamt2(character)
First of all a neither a Character Field Type nor a Numeric Field type (tranamt2) are Not a VFP Currency Field type
You may be using the value of a Character field to represent currency, but that does not make it a currency value - just a String value.
And typically when that is done, you do NOT store the Dollar Sign '$' and Comma ',' as part of the data.
Instead, you store the 'raw' value (in this case: "-710000.99") and merely format how that 'raw' value is displayed when needed.
So in your Character field you have a value of: -$710,000.99
Do you have the Dollar Sign '$' and the Comma ',' as part of the field data?
If so, to convert it to a Numeric, you will first have to eliminate those extraneous characters prior to the the conversion.
If they are not stored as part of your field value, then you can use the VAL() 'as is'.
Example:
cStr = "-710000.99" && The '$' and ',' are NOT stored as part of Character value
nStr = VAL(cStr)
?nStr
However if you have the Dollar Sign and the Comma as part of the field data itself, then you can use STRTRAN() to eliminate them during the conversion.
Example:
cStr = "-$710,000.99" && Note both '$' and ',' are part of data value
* --- Remove both '$' and ',' and convert with VAL() ---
nStr = VAL(STRTRAN(STRTRAN(cStr,",",""),"$",""))
?nStr
Maybe something like:
REPLACE tranamt WITH VAL(STRTRAN(STRTRAN(tranamt2,",",""),"$",""))
EDIT: Another alternative would be to use CHRTRAN() to remove the '$' and ','
Something like:
cRemoveChar = "$," && Characters to be removed from String
REPLACE tranamt WITH VAL(CHRTRAN(tranamt2,cRemoveChar,""))
Good Luck
A little late but I use this function call
function MoneyToDecimal
LPARAMETER tnAmount
LOCAL lnAmount
IF VARTYPE(tnAmount) = "Y"
lnAmount = VAL(STRTRAN(TRANSFORM(tnAmount), "$", ""))
ELSE
lnAmount = tnAmount
ENDIF
return lnAmount
endfunc
And can be tested with these calls:
wait wind MoneyToDecimal($112.50)
wait wind MoneyToDecimal($-112.50)
Use the built-in MTON() function to convert a currency value into a numeric value:
replace all tranamt with mton(tranamt2)

grid filter in foxpro

I have a grid on a form that displays some columns from a dbf table and a textbox.
I want to search the value displayed in the textbox over all columns from a dbf table. Some fields are numeric and other are character
If I want to find a number, should search all record that contain that number in all columns, no matter the column type.
If I want to search a substring should give me all record that contain that substring.
SET FILTER TO ALLTRIM(ThisForm.Text1.Value) $Content or ALLTRIM(val(ThisForm.Text1.Value)) $registrationNumber or ALLTRIM(ThisForm.Text1.Value) $holderNo
Your approach with the "$" wildcard "contains" approach appears to be ok. However, your attempt via allt( val( )) would fail as you cant trim a numeric value, it would have to be pre-converted to a string.
Now, that said, you could shorten your query by just doing a $ against a concatenation of ALL columns something like (assuming your registration number is a numeric field)...
set filter to ALLTRIM(ThisForm.Text1.Value) ;
$ ( Content +"," +str(registrationNumber) +," + holderNo )
if you have dates or date/time fields you could do DTOC( dateField ) or TTOC( dateTimeField). So, by building a single string of all values, you dont have to explicitly repeat the OR condition repeatedly.
You could do something like:
select curGrid
scan
lcRow = transform(field1) + transform(field2) ... + transform(lastfield)
if lcSearchValue $ lcRow
DoWhatever()
endif
endscan
This leverages the fact that transform() will give a string representation of any data type.

Resources