Hivesql question: im trying to format 'datatype: double' to chars of 9 strings but add 0s to leading string if not upto 9 string - hive

I am trying to remove space or special characters and format 'datatype: double' to chars of 9 strings but add 0s to leading string if not upto 9 string. Each row output have diff amount of strings
Row Record
922,421
1,294,043
22,513,01
24,655
My expectation:
000922421
001294043
002251301
000024655
What i tried: cast(cast(column_name as format '9(9)') as chars(9)). It throws error

Use lpad.
But first remove comma with blank using replace() and then lpad() 9 places with0s.
select lpad(replace( '922,421',',',''),9,'0') as col

Related

Regex - trying to get the 5 digit words extracted from the string (presto)

I am trying to retrieved each sequence of 5 numbers / letters that are in brackets just like this example:
accuracy of action - [1232d, 74294, qw23t, 23d45, 76wer, 12874] march
and from that I want to extract 1232d 74294 qw23t 23d45 76wer 12874
I know that to extract only a single 5 digit sequence in square brackets I can do \[[a-z0-9 ]{5,7}\] But I don't know how to do retrieve various 5 digit sequences.
Right now, since all the words inside [...] consist of 5 alphanumeric chars, you can use
(?:\G(?!^),\s*|\[)(\w+)(?=[^\]\[]*])
See the regex demo.
Details:
(?:\G(?!^),\s*|\[) - either the end of the preceding successful match and a comma and zero or more whitesapces, or a [ char
(\w+) - Group 1: one or more word chars
(?=[^\]\[]*]) - followed with zero or more chars other than [ and ] and then a ].

database padding space if the value inserted has smaller length than column size- DB2

I was checking if DB pads spaces in a column if the inserted string has fewer characters than the designated length of the column. Example:
lets say size of <column1> is 10 but the value entered is abc - then is it abc_______ which the DB stores where _ represents spaces?
I am asking because I used LTRIM-RTRIM while INSERTing the values and on again fetching the value in the very next minute I got the result as abc_______.
You are using the CHAR or CHARACTER datatype for the column. The CHAR or CHARACTER datatype is a fixed length datatype and is padded with space at the end of the value to fill the column size.
You can use VARCHAR to avoid the padding with spaces at the end of the values.
Note: Make sure you are using CHARACTER_LENGTH on CHARACTER columns to get the correct character length (without padding spaces). The result of LENGTH also includes the padding spaces.
demo on dbfiddle.uk

How to extract just numeric value with REGEXP_EXTRACT in BigQuery?

I am trying to extract just the numbers from a particular column in BigQuery.
The fields concerned have this format: value = "Livraison_21J|Relais_19J" or "RELAIS_15 DAY"
I am trying to extract the number of days for each value preceeded by the keyword "Relais".
The days range from 1 to 100.
I used this to do so:
SELECT CAST(REGEXP_EXTRACT(delivery, r"RELAIS_([0-9]+J)") as string) as relayDay
FROM TABLE
I want to be able to extract just the number of days regardless of the the string that comes after the numbers, be it "J" or "DAY".
Sample data :
RETRAIT_2H|LIVRAISON_5J|RELAIS_5J | 5J
LIVRAISON_21J|RELAIS_19J | 19J
LIVRAISON_21J|RELAIS_19J | 19J
RETRAIT_2H|LIVRAISON_3J|RELAIS_3J | 3J
You may use
REGEXP_EXTRACT(delivery, r"(?:.*\D)?(\d+)\s*(?:J|DAY)")
See the regex demo
Details
(?:.*\D)? - an optional non-capturing group that matches 0+ chars other than line break chsrs as many as possible and then a non-digit char (this pattern is required to advance the index to the location right before the last sequence of digits, not the last digit)
(\d+) - Group 1 (just what the REGEXP_EXTRACT returns): one or more digits
\s* - 0+ whitespaces
(?:J|DAY) - J or DAY substrings.

Why split('TEST','') returns two extra elements:- ["","T","E","S","T",""]

Why is the Hive split function returning two extra elements? My input string only has 4 characters but split returns an array with 6 elements.
hive> select split('TEST','');
OK
["","T","E","S","T",""]
My goal was to convert the input string to character array.

How to format integer as string with 2 digits?

I would like to format an integer 9 to "09" and 25 to "25".
How can this be done?
You can use either of these options:
The "0" Custom Specifier
value.ToString("00")
String.Format("{0:00}", value)
The Decimal ("D") Standard Format Specifier
value.ToString("D2")
String.Format("{0:D2}", value)
For more information:
Custom Numeric Format Strings
Standard Numeric Format Strings
If its just leading zero's that you want, you can use this:
value.tostring.padleft("0",2)
value.ToString().PadLeft(2, '0'); // C#
If you have 2 digits, say 25 for example, you will get "25" back....if you have just one digit, say 9 for example, you will get "09"....It is worth noting that this gives you a string back, and not an integer, so you may need to cast this later on in your code.
String formate is the best way to do that. It's will only add leading zero for a single length. 9 to "09" and 25 to "25".
String.format("%02d", value)
Bonus:
If you want to add multiple leading zero 9 to "0009" and 1000 to "1000". That's means you want a string for 4 indexes so the condition will be %04d.
String.format("%04d", value)
I don't know the exact syntax. But in any language, it would look like this.
a = 9
aString =""
if a < 10 then
aString="0" + a
else
aString = "" + a
end if