I am trying to format the date field which contains spaces in replace text processor from a csv file.
Got the error as it is unable to parse the date column which is spaces for first record. Please let me know how to handle this
Error message: Replace text failed to process session due to Cannot parse attribute value as a date; date format ddMMyyyy; attribute value:
Input csv:
1, , 123
2,02091997,234
Search value : (.{1}),(.{8}), (.{3})
Replacement value : $1, ${'$2':toDate("ddMMyyyy") :format("yyyy-MM-dd HH:mm:ss.SSS") }, $3
Replacement strategy : Regex Replace
Evaluation mode : Entire Text
You can use the isEmpty and ifElse function from the language expression.
ex: ${'$2':isEmpty():ifElse('null', '$2':toDate("ddMMyyyy"):format("yyyy-MM-dd HH:mm:ss.SSS")) }
Here I put 'null', when there is no date, but you can choose the value you want.
However, if you can, parse your CSV files with a CSVReacordReader which handle that out of the box.
Related
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 = '',.''')"
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)
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.
I have a bunch of csv files that need "cleaning".
Specifically, there is a column that contains timestamp values, however some lines have a value of '1' instead.
What I wish to do, is replace those 1's with the last valid (timestamp) value, i.e. replace the value of i-th line with that of that of line i-1.
I provide a sample of the file
URL192.168.2.2,420042,20/07/2015 09:40:00,168430081,168430109
URL192.168.2.2,420042,20/07/2015 09:40:00,3232236038,3232236034
URL192.168.2.2,420042, 1,168430081,168430109
URL192.168.2.2,420042,20/07/2015 09:40:01,3232236038,3232236034
So in this example, the 1 must be replaced with 20/07/2015 09:40:00. I tried it using awk but couldn't nail it.
Assuming no commas in the other fields, an awk program like this should work:
BEGIN { FS = OFS = "," }
$3!=1 { prev = $3 }
$3==1 { $3 = prev }
{ print }
Warning: this is untested code.
The first line sets the field separator to a comma, for both input and output. The second line saves the timestamp of every row that has a timestamp in the third field. The third line writes the most recently saved timestamp to every row that doesn't have a timestamp in the third field. And the fourth line writes every input line, whether modified or not, to the output.
Let me know how you get on.
I have data in the below format in a .txt file:
parameter1=12345 parameter2=23456 parameter3=23456 and so on.. the list is a long one.
I have found a way to match the parameter1 and so on and replace it with some other number.
modified_file=File.read("modified_file.txt",)
modified_file=modified_file.to_s.sub(/#{parameter1}=/, "some text of your choice")
The above regular expression would only replace the value with parameter1= but I intend to change following parameter1=.
I want to write a regular expression which can match the data up to = and replace the data following that.
For Eg: I want to replace 12345 to abcde and 23456 to xyzab so the final result would be:
parameter1=abcde parameter2=xyzab and so on..
/(?<=parameter1=)\S+/
What you want is called a "lookbehind".