How to convert interger numbers into brazilain price format - gawk

i have integer numbers like below on left side in a file for products and need to convert them into brazilaian format like on right side.
100 =>100,00
84 => 84,00
1011 => 1.011,00

Use number_format() -
echo number_format(100, 2, ',', '.');
echo number_format(84, 2, ',', '.');
echo number_format(1011, 2, ',', '.');
Output -
100,00
84,00
1.011,00
Docs

Related

Convert sql query to nested json

I have the following SQL query:
SELECT att.prod_name, att.prod_group, att.prod_size, obj.physical_id, obj.variant, max(obj.last_updated_date)
FROM Table1 obj
join Table2 att
on obj.prod_name = att.prod_name
where
obj.access_state = 'cr'
AND obj.variant in ('Front')
AND obj.size_code in ('LARGE')
AND att.prod_name in ('prod_1','prod_2')
group by 1,2,3,4,5
The output currently looks like this:
prod_name prod_group prod_size physical_id variant max
prod_1 1 Large - 2 Oz jnjnj3lnzhmui Front 8/8/2020
prod_1 1 Large - 2 Oz pokoknujyguin Front 6/8/2020
prod_2 1 Large - 3 Oz oijwu8ygtoiim Front 4/2/2018
prod_2 1 Large - 3 Oz ytfbeuxxxx2u2 Front 7/2/2018
prod_2 1 Large - 3 Oz rtyferqdctyyx Front 4/4/2020
How can I convert this to a nested json in the query itself ?
Required output: (Variant and max date can be ignored)
{"prod_name":"prod_1" , "prod_group":"1", "prod_size":"Large - 2 Oz", "physical_id":{"physical_id_1":"jnjnj3lnzhmui", "physical_id2" : "pokoknujyguin"}}
{"prod_name":"prod_2" , "prod_group":"1", "prod_size":"Large - 3 Oz", "physical_id":{"physical_id_1":"oijwu8ygtoiim", "physical_id2" : "ytfbeuxxxx2u2", "physical_id3" : "rtyferqdctyyx"}}
As stated there aren't built in JSON statements for Redshift like there are in BigQuery TO_JSON() or SQL Server FOR JSON.
So, you are stuck with either writing a conversion yourself in a coding language like Java or Python or writing up a bunch of string manipulation code to "fake it" directly in Redshift.
Something akin to:
SELECT CHR(123) || '"prod_name"'|| ':' || '"' || nvl(prod_name,0) || '"' || ',' ||
'"prod_group"'|| ':' || '"' || nvl(prod_group,'') || '"' || ',' ||
'"prod_size"'|| ':' || '"' || nvl(prod_size,'') || '"' || Chr(125) FROM TABLE1
The nvl protects you from null values if present. The nesting aspects get a little harder, but with enough patience you should get there.
Good luck!

Store a combination of texts and variables in a single variable

I am doing a program in ABAP to display system date in differed formats like DDMMYYYY and YYYYDDMM.
The snippet is :
WRITE: / 'DATE IN DDYYYYMM FORMAT IS',SY-DATUM+6(2),'.',SY-DATUM+0(4),'.',SY-DATUM+4(2).
which produces this output :
DATE IN DDYYYYMM FORMAT IS 27 . 2007 . 07
Is it possible to store DATE IN DDYYYYMM FORMAT IS 27 . 2007 . 07 in a variable say c so that when I write the code WRITE : c. it outputs DATE IN DDYYYYMM FORMAT IS 27 . 2007 . 07?
You can use the character string operator && to join texts and variables :
DATA: date_text TYPE c LENGTH 10.
date_text = sy-datum+6(2) && '.' &&
sy-datum+0(4) && '.' &&
sy-datum+4(2).
WRITE: / 'DATE IN DDYYYYMM FORMAT IS',
date_text.
And the output will be:
DATE IN DDYYYYMM FORMAT IS 17.2019.07
PS: for WRITE, you can add NO-GAP to each element so that the extra space is removed:
WRITE: / 'DATE IN DDYYYYMM FORMAT IS',
sy-datum+6(2) NO-GAP,
'.' NO-GAP,
sy-datum+0(4) NO-GAP,
'.' NO-GAP,
sy-datum+4(2) NO-GAP.
Output will be like above.
A handy approach for stuff like this is using string templates:
DATA(lv_date) = |{ sy-datum+6(2) }.{ sy-datum+0(4) }.{ sy-datum+4(2) }|.
WRITE: / |DATE IN DDYYYYMM FORMAT IS { lv_date }|.

some guidance in modifying the query

I'm using the below query in order to split the value of a column into another column and is working fine, but it is not giving the output that I'm expecting. Below is the query along with the output.
Query:
SELECT
upper(regexp_substr(db_attributes, '[^,:]+$',1,1)) AS servername,
db_attributes FROM
table_name
Current Output:
Servername db_attributes
INPRD1HF hkp04lp0605s-rs6000.hk.hsbc, 50000, AP4INPU1:INPRD1HF
AE000BDS0096\LIVE0096MSSQL ae000bds0096.hbeu.adroot.hsbc, 60695, AE000BDS0096\LIVE0096MSSQL
NULL ora-abacogp.de.hsbc, 1626, ABACOGP
Desired output:
Servername db_attributes
AP4INPU1 hkp04lp0605s-rs6000.hk.hsbc, 50000, AP4INPU1:INPRD1HF
AE000BDS0096\LIVE0096MSSQL ae000bds0096.hbeu.adroot.hsbc, 60695, AE000BDS0096\LIVE0096MSSQL
ABACOGP ora-abacogp.de.hsbc, 1626, ABACOGP
The difference between the current and desired output is in the first line I want the value which is before colon in db_attributes column (i.e. AP4INPU1 not INPRD1HF)
Regards,
Vikas
You may use
regexp_substr(db_attributes, '([^,:[:space:]]+)(:\S*)?$', 1, 1, NULL, 1)
Details
([^,:[:space:]]+) - Group 1: one or more chars other than ,, : and whitespace chars
(:\S*)? - an optional group matching 1 or 0 sequences of : and then 0+ non-whitespace chars
$ - end of string.
See the online tests:
--select regexp_substr('hkp04lp0605s-rs6000.hk.hsbc, 50000, AP4INPU1:INPRD1HF', '([^,:[:space:]]+)(:\S*)?$', 1, 1, NULL, 1) AS servername from dual
-- => AP4INPU1
--select regexp_substr('ora-abacogp.de.hsbc, 1626, ABACOGP', '([^,:[:space:]]+)(:\S*)?$', 1, 1, NULL, 1) AS servername from dual
-- => ABACOGP
--
select regexp_substr('ae000bds0096.hbeu.adroot.hsbc, 60695, AE000BDS0096\LIVE0096MSSQL', '([^,:[:space:]]+)(:\S*)?$', 1, 1, NULL, 1) AS servername from dual
-- => AE000BDS0096\LIVE0096MSSQL

parsing oracle 11g

I have the following snippet of a file that needs parsing :
000000837002100302015-04-13-15.55.12.1922082015-04-1300032128CHECK CLEARED CHECK CLEARED 00000000000030000000000000000000000000000000016594703
The respective positions for the string are as follows:
RECORD1 - POS (1 :10 ) - DUR 10
RECORD2 - POS (11 :13 ) - DUR 3
RECORD3 - POS (14 :17 ) - DUR 4
RECORD4 - POS (18 :43 ) - DUR 26
RECORD5 - POS (44 :53 ) - DUR 10
RECORD6 - POS (54 :61 ) - DUR 8
RECORD7 - POS (62 :95 ) - DUR 34
RECORD9 - POS (96 :215) - DUR 120
RECORD10 - POS (216:233) - DUR 18
RECORD11 - POS (234:251) - DUR 18
RECORD12 - POS (252:266) - DUR 15
RECORD13 - POS (267:268) - DUR 2
I need to parse the string to extract these records from that string. Can anyone help with the substr/instr functionality to account for the string and blank spaces. The extracted data would then get inserted into a table. Thank you in advance!
Can you not simply use a sqlloader control file e.g.
echo "OPTIONS (DIRECT=TRUE)"
echo "LOAD DATA"
echo "INFILE '/path/to/file/$1'"
echo "BADFILE '/path/to/file/log/$1.bad'"
echo "DISCARDFILE '/path/to/file/log/$1.dsc'"
echo "APPEND INTO TABLE table.t"
echo "("
echo " RECORD1 POSITION (1:10),"
echo " RECORD2 POSITION (11:13),"
etc...
echo ")"
You don't need instr, because the position is already specified. Instead
use substr
insert into some_table
select substr(text, 1, 10) rec1, substr(text, 11, 3) rec2, ...
from text_table;

Splitting a Path Enumeration Model PathString in MySQL

I'm trying to implement a Path Enumeration model as per Joe Celko's book (page 38). The relevant attributes of my table (and the support table that just contains sequential integers) look like this:
Contribution
------------
ContributionID
PathString
_IntegerSeries
--------------
IntegerID
_IntegerSeries contains integers 1 to n where n is bigger than I'll ever need. Contribution contains three records:
1 1
2 12
3 123
... and I use a modified version of Joe's query:
SELECT SUBSTRING( c1.PathString
FROM (s1.IntegerID * CHAR_LENGTH(c1.ContributionID))
FOR CHAR_LENGTH(c1.ContributionID)) AS ContID
FROM
Contribution c1, _IntegerSeries s1
WHERE
c1.ContributionID = 3
AND s1.IntegerID <= CHAR_LENGTH(c1.PathString)/CHAR_LENGTH(c1.ContributionID);
... to successfully return a result set containing all of ContributionID 3's superiors in the hierarchy. Now, in this example, the PathString column holds plain integer values and obviously we run into trouble once we hit ContributionID 10. So we modify the PathString column to include separators:
1 1.
2 1.2.
3 1.2.3.
Now... the book doesn't give an example of getting superiors when the PathString uses delimiters... so I'll have to figure that out later. But it does give an example for how to split up a PathString (which I'm guessing is going to help me do superior searches). The MySQL version of the example code to do this is:
SELECT SUBSTRING( '.' || c1.PathString || '.'
FROM s1.IntegerID + 1
FOR LOCATE('.', '.' || c1.PathString || '.', s1.IntegerID + 1) - s1.IntegerID - 1) AS Node
FROM _IntegerSeries s1, Contribution c1
WHERE
SUBSTRING('.' || c1.PathString || '.' FROM s1.IntegerID FOR 1) = '.'
AND IntegerID < CHAR_LENGTH('.' || c1.PathString || '.');
... but this code returns an empty result set. I'm doing something wrong, but I'm not sure what. Figured I'd put this out to the stackoverflow community prior to bothering Joe with an email. Anyone have any thoughts?
UPDATE
Quassnoi's query... slightly modified a bit after testing, but exactly the same as his original functionally. Very nice. Much cleaner than what I was using. Big thanks.
SET #contributionID = 3;
SELECT ca.*
FROM
Contribution c INNER JOIN _IntegerSeries s
ON s.IntegerID < #contributionID AND SUBSTRING_INDEX(c.PathString, '.', s.IntegerID) <> SUBSTRING_INDEX(c.PathString, '.', s.IntegerID + 1)
INNER JOIN Contribution ca
ON ca.PathString = CONCAT(SUBSTRING_INDEX(c.PathString, '.', s.IntegerID), '.')
WHERE c.ContributionID = #contributionID;
This is because || in MySQL is boolean OR, not string concatenation.
To find all ancestors of a given Contribution, use:
SELECT ca.*
FROM Contribution с
JOIN IntegerSeries s
ON IntegerID < CHAR_LENGTH(c.path)
AND SUBSTRING_INDEX(c.path, '.', IntegerID) <> SUBSTRING_INDEX(c.path, '.', IntegerID + 1)
JOIN Contribution ca
ON ca.path = CONCAT(SUBSTRING_INDEX(c.path, '.', IntegerID), '.')
WHERE c.ContributionID = 3