I have a Table with a Column like this:
Location
19.15
19.14
19.13
18.01
18.02
I searched for function to retrieve just 2 digit from beginning to be like this:
Location
19
19
19
18
18
Unfortunately I can't find solution yet .
select cast(location as int) as location
from your_table
It depends entirely on the data type.
We can return the first two characters of a string column with the SUBSTR() function:
select substr(location, 1, 2) as location
from your_table;
If you wish to handle a varying number of digits before the point then perhaps a regular expression function would be better:
select regexp_substr(location, '([0-9]+)\.(.*)', 1, 1, 'i', 1)) as location
from your_table;
If location is a numeric and you want to remove the trailing decimals you can use TRUNC() like this:
select trunc(location) as location
from your_table;
Related
I have a query as below
|| LPAD (TRIM (TO_CHAR (RWTEXPT_STD_AMOUNT, 'FM9999999999999D00')), 15, '0')
its giving the result : 000011545467.00
what i need is : 000000115454.67
i have tried 'FM9999999999999D00' and '999999999999D99' but it gives the same results 000011545467.00
what i need is 000000115454.67
Convert your string value to a number, divide it by 100 and then format it:
SELECT TO_CHAR(
TO_NUMBER(RWTEXPT_STD_AMOUNT)/100,
'FM000000000000D00'
) AS result
FROM table_name
Which, for the sample data:
CREATE TABLE table_name (RWTEXPT_STD_AMOUNT) AS
SELECT '000000011545467' FROM DUAL;
Outputs:
RESULT
000000115454.67
fiddle
Just do it this way
Select To_Char(14.5, 'FM000000000000D00') "NMBR" From Dual
--
-- NMBR
-- ----------------
-- 000000000014.50
Put any number of leading zeros within the format. Zero means zero and 9 is a placeholder if there is a number present. Letter D is for decimal point character. You can use G for grouping character too.
Regarding decimals - if your column has integer value like 1467 and you know that last two numbers are decimal numbers then just put 1467/100
Select To_Char(1467/100, 'FM000000000000D00') "NMBR" From Dual
--
-- NMBR
-- ----------------
-- 000000000014.67
Regards...
I need to combine two fields but force the characters of the second string to be 2 characters.
I'm combining a year field and month field and want the result to be YYYY_MM. Forcing any single months (e.g. 1,2,3,4) into a two digit format e.g. (01).
Below is my formula for combining the fields, but I need help making the month two digits.
Thanks, L
WITH so_header(soh_build_year,soh_build_week) AS (
SELECT 2020, 3
UNION ALL SELECT 2020,13
)
SELECT
CAST(SO_HEADER.SOH_Build_Year AS VARCHAR)
+'_'
+CAST(SO_HEADER.SOH_Build_Week AS VARCHAR) as [Build YYYY_WW]
FROM so_header;
Try this out (Syntax: SQL Server)
SELECT
CAST(2019 AS VARCHAR)
+'_'
+CAST(format (1, '0#') AS VARCHAR) as [Build YYYY_WW]
Replace your values with your variables
Try this:
WITH so_header(soh_build_year,soh_build_week) AS (
SELECT 2020, 3
UNION ALL SELECT 2020,13
)
SELECT
CAST(SO_HEADER.SOH_Build_Year AS VARCHAR)
+ '_'
+ SUBSTR(
CAST(100+SO_HEADER.SOH_Build_Week AS VARCHAR)
, 2
, 2
) as Build_YYYY_WW
FROM so_header;
-- out Build_YYYY_WW
-- out ---------------
-- out 2020_03
-- out 2020_13
If you are using SQL Server never use varchar (or related types) with no length. The default varies by context and may not be large enough for what you want.
If you are trying to convert a date to YYYY_MM format, you can use format():
select format(getdate(), 'yyyy_MM')
I recommend using dates, if they are available. If you are not using SQL Server, most other databases have similar functionality.
If not, you an simply use:
select concat(so_header.SOH_Build_Year, '_'
right(concat('00', so_header.soh_build_week), 2)
)
concat() does not require explicitly converting the values to strings.
https://dbfiddle.uk/?rdbms=oracle_18&fiddle=94771b6589b01526ad0cf6e5c4d01945
I need help in extracting the number substring from a file name
currently for file format - 'monkey_eats_mango_everyday_202002.txt'
we are doing like this
select regexp_substr('monkey_eats_mango_everyday_202002.txt', '\d+') as parameter12a
from dual;
result-
202002
which in turn used in larger query to get the last date of this date like this
select to_char(last_day(to_date(regexp_substr('monkey_eats_mango_everyday_202002.txt', '\d+'), 'yyyymm')), 'yyyymmdd') as parameter
from dual ;
result-
20200229
Now the file format has changed, so we have - 'donkey_eats_pines_cones_20192301_7771234_everyday_202002.txt'
In this file format there are numbers at other places like 201943_7771234 which can be dates or any random number, so I need regex expression which can extract 202002 from file format
select regexp_substr('donkey_eats_pines_cones_201943_7771234_everyday_202002.txt', '\d+') as parameter12a
from dual;
You can use a \. to anchor your digits match to next to the period in the file name, and then use a capture group around the digits to get just the digits in the output, using the 6th parameter to REGEXP_SUBSTR to indicate that you only want group 1 in the output:
SELECT REGEXP_SUBSTR('donkey_eats_pines_cones_201943_7771234_everyday_202002.txt', '(\d+)\.', 1, 1, NULL, 1) AS parameter12a
FROM dual;
Output:
202002
Demo on dbfiddle
One option is to use nested expressions: inner returns file extension and the date (that precedes that extension), and outer fetches date itself.
SQL> with test (col) as
2 (select 'donkey_eats_pines_cones_201943_7771234_everyday_202002.txt' from dual)
3 select regexp_substr(regexp_substr(col, '\d+.\w+$'), '\d+') result From test
4 /
RESULT
------
202002
SQL>
check this
select reverse(split_part(reverse(r.r ), '.', 2)) from
(
SELECT reverse(split_part(reverse('donkey_eats_pines_cones_20192301_7771234_everyday_202002.txt'), '_', 1)) as r
)as r
ANS :
202002
Consider the following column row:
col
-------------------------
'{"day":"8","every":"2"}'
I am trying to get 8 from this string using regular expression to figure out the day.
so far I have:
SELECT
regexp_replace(col, '{"day":[^0-9]', '') as "day"
FROM
mytable;
This gives me:
day
---------------
8","every":"2"}
I am having trouble figuring out how to filter out the rest of the string from the first number forward. In my example I just want the number 8 for this row.
When you are lucky enough to use Oracle 12c Release 1 (12.1.0.2) or later, do take a look at JSON_VALUE
WITH t (s)
AS (
SELECT '{"day":"8","every":"2"}'
FROM DUAL
)
SELECT JSON_VALUE(s, '$.day' ) AS day
, JSON_VALUE(s, '$.every') AS every
FROM t;
DAY EVERY
--- -----
8 2
How about this?
SELECT
regexp_replace(col, '{"day":"([0-9]+).*', '\1') as "day"
FROM
mytable;
If you don't have access to JSON_VALUE() then I would recommend the following regex unless you always know the position of the day key in the JSON string:
SELECT REGEXP_REPLACE(col, '^.*"day":"(\d+)".*$', '\1') AS day
FROM mytable;
This will replace the entire string (assuming it matches!) with the contents of the first capturing group (enclosed in parentheses: (\d+)). \d indicates a digit 0-9. If you want to return NULL values as well, you can replace \d+ with \d*. If negative or non-numeric values are possible, then I would recommend the following:
SELECT REGEXP_REPLACE(col, '^.*"day":"([^"]*)".*$', '\1') AS day
FROM mytable;
This will return whatever characters that might be contained in the day key.
FYI, once you have the value, numeric or non-, you can convert it to a number safely by using TO_NUMBER() along with REGEXP_SUBSTR():
SELECT COALESCE( TO_NUMBER( REGEXP_SUBSTR( REGEXP_REPLACE( col, '^.*"day":"[^"]*".*$', '\1' ), '\d+' ) ), 0 ) AS day
FROM mytable;
Hope it helps.
How can I convert my DECIMAL(11) field from 12345678 to a character value of 00012345678?
Only use the DIGITS function, because this verifies the length of the field numeric or decimal, etc and completes with zeros to the left when is necessary.
SELECT DIGITS(FIELD) FROM ...
The length of the resulting string is always:
5 if the argument is a small integer
10 if the argument is a large integer
19 if the argument is a big integer
Based on your comment in #Mr Fuzzy Botton's answer, I'm guessing you're on DB2 for i, which does not have the LPAD function. You could instead use a combination of the REPEAT and RIGHTfunctions:
SELECT RIGHT(REPEAT('0', 11) || LTRIM(CHAR(your_field)), 11)
FROM your_table
Using http://pic.dhe.ibm.com/infocenter/dzichelp/v2r2/topic/com.ibm.db2.doc.sqlref/castsp.htm for details on CAST
and http://pic.dhe.ibm.com/infocenter/dzichelp/v2r2/topic/com.ibm.db2z10.doc.sqlref/src/tpc/db2z_scalarfunctionsintro.htm for string functions,
I assume this should do the trick -
SELECT LPAD( CAST(FIELD AS CHAR(11)) ,11,'0') AS PADDEDFIELD
Don't know if you've worked it out, however try this:
SELECT LPAD( DIGITS( fieldName ), 11, '0') ) as paddedFieldName FROM yourTable
The LPAD is the left padding, but the DIGITS function was the only way I got DB2 to treat the numeric value like a string.
My LeftPad function without LeftPad function
REPEAT('0', 4-length(MY_COLUMN_VALUE))||CHAR(MY_COLUMN_VALUE) as NEW_COLUMN
MY_COLUMN_VALUE NEW_COLUMN
1 0004
23 0023
testing ...
SELECT '32' MY_VALUE, REPEAT('0', 4-length('23'))||CHAR('23') as LEFTPAB_MY_VALUE FROM sysibm.sysdummy1
If this is DB2 for i, and myColumn data type is DECIMAL with precision (11) and scale (0), then:
SELECT digits( myColumn ) FROM sysibm.sysdummy1
will return:
....+....1.
DIGITS
00001234567
Changing the number of leading zeros could be done in many ways. CASTing to a different precision before using DIGITS() is one way.
SELECT SUBSTRING(
CHAR(100000000000+fieldName),
2,11
) as paddedFieldName
FROM yourTable
I only wanted to define my field once in the select statement so the above worked for me and is tidy
I just went the other direction: cast(field as int)
Try this for your field x:
substr(digits(x), 33 - length(x), length(x) )
From Numeric 8,0 (datenumfld=20170101) to 01/01/2017 This works for me:
DATE(TO_DATE(CHAR(datenumfld), 'YYYYMMDD')) as YourDate