How to get leading zeros for zipcode in Teradata - sql

the zip is an integer datatype. so far i have
select substring('00000'||cast(zip as char(5)), character_length(cast(zip as char(5))),5)

It's not entirely clear what you are asking, but my guess from looking at your attempt is that you are trying to prefix a string with zeros to make it a common length.
For instance, if you have the following numbers: 123, 1564, 12413 and you would like them all to be 10 characters with repeated 0's prefixed like 0000000123, 0000001564, 0000012413, you will need something like:
SELECT substring('0000000000' FROM 1 FOR 10 - LENGTH(<yourField>)) || <yourField>

Apply a FORMAT to the zip:
TRIM(CAST(zip AS FORMAT '9(5)'))
or use LPAD in TD14:
LPAD(TRIM(zip), 5, '0')

Related

Format query to show floating decimal in Oracle SQL

I am using Oracle SQL Developer and I have a query in which one of the columns has a number value that I want to divide by 1,000,000 then display to show commas and up to 10 decimal places if the calculate value has any decimal places. I am currently using this query:
to_char(value/1000000, 'FM999,999,990.9999999999') as Millions_Value
The above format gives me commas and retains the number of decimal places I want, but it unfortunately adds a decimal point at the end of whole numbers as well.
Example:
value/1000000 = 33993
formatted value = 33,993.
what I want = 33,993
For a decimal value, I get what I want and need to keep the format
Example:
value/1000000 = 0.158739
formatted value = 0.158739
value/1000000 = 10.82
formatted value = 10.82
And ideas on how to get rid of the decimal for integers?
The easiest way is to use RTRIM, e.g.
RTRIM(to_char(value/1000000, 'FM999,999,990.9999999999'), '.') as Millions_Value
will remove any trailing periods.
EDIT
If you want to get fancy and internationalize it you need to query NLS_SESSION_PARAMETERS to get the decimal separator, use the G (group) and D (decimal) specifiers in the format string, and then trim off any trailing decimal separator characters. Or in other words:
WITH cteData AS (SELECT 123456789.123456789 AS N FROM DUAL UNION ALL
SELECT 123 FROM DUAL),
cteDecimalSeparator AS (SELECT SUBSTR(VALUE, 1, 1) AS DECIMAL_SEPARATOR
FROM NLS_SESSION_PARAMETERS
WHERE PARAMETER = 'NLS_NUMERIC_CHARACTERS')
SELECT TO_CHAR(N, 'FM999G999G990D9999999999') AS OLD_FORMAT,
RTRIM(TO_CHAR(N, 'FM999G999G990D9999999999'), s.DECIMAL_SEPARATOR) AS NEW_FORMAT
FROM cteData d
CROSS JOIN cteDecimalSeparator s
which returns
OLD_FORMAT NEW_FORMAT
123,456,789.123456789 123,456,789.123456789
123. 123
Remember: there's no kill like overkill :-)
Personally, I think it looks good with one trailing 0, so converting your format string to have a 0 after the decimal. But to answer you question, I think you just need to trim it after you convert the number like this:
SELECT TRIM(TRAILING '.' FROM TO_CHAR('345676', 'FM999G999G990D9999999999'))
FROM DUAL;

Formatting a string to 2 decimal places?

I have a number that looks like this:
'0000040000'
How can I turn it into a string that looks like this:
400.00
This should work on numbers like this as well :
1234540067 -> 12345400.67
I suspect it's fair to say you have a string that you would like to format as a number.
If you want to "learn" how to do this, I suggest looking up the convert, cast and format functions for SQL Server and gain some extra knowledge.
I've elected to first convert to a numeric type, divide by 100 and format the output.
This saves the need to trim leading zero's.
select format(convert(numeric(18,2), '0000040001') / 100, '0.00'))
Following SQL expression will first change string to decimal and then convert it back to a string in a required format:
SELECT FORMAT(CAST ('0000040000' AS DECIMAL(12,2))/100,'#.00')
This should work:
select convert(varchar(10), convert(decimal(10, 2), try_convert(int, '0000040000') / 100.0))
Why do you want the value as a string? Does a decimal work for your purposes?
Another method is:
select ltrim(str(try_convert(int, '0000040000') / 100.0, 10, 2))
Or using just string manipulations:
select replace(ltrim(replace(stuff('0000040000', 9, 0, '.'), '0', ' ')), ' ', '0')
This would be more elegant if ltrim() in SQL Server accepted the character to trim (as most other databases allow).
Here are a some possible solutions:
DECLARE #x CHAR(10)='0000040000'
SELECT CONVERT(VARCHAR(11),CONVERT(NUMERIC(10,2),CONVERT(NUMERIC(10),#x)/100))
SELECT CONVERT(VARCHAR(11),CONVERT(NUMERIC(10,2),STUFF(#x,9,0,'.')))
Simple as this:
SELECT ROUND('0000040004', 2, 1)/100;
Tested this with SQL Server 2017. It does not care about the fact that the number is a string, it does math on it just fine. Result of the above is 400.04
Your query is more of mathematical one.
To get the last 2 numbers after decimals, get the remainder of the number by dividing it by 100.
To get the digits, leaving the last 2 digits, divide the number by 100 again.
select convert(varchar,1234540067/100)+'.'+ convert(varchar,1234540067% 100);

REGEXP_SUBSTR to extract fixed length string starting from a digit

Table A
ID ID_Descr
1 'DUP 8002061286'
2 'DUP 8002082667 '
3 ' 8002082669 DUP'
I would like to extract the string from the ID_Descr field with the following conditions:
String always starts with 8
String length is always 10 digits
This means stripping everything to the right and left of the string (eg. '8002082669'). How can I achieve this? Using REGEXP_SUBSTR?
I am using Oracle 11g.
Thanks!
Although you could use regexp_substr() for this, I would take a different approach. I would just look for the '8' using instr() and then take the next 10 characters:
select substr(id_descr, instr(id_descr, '8'), 10)
This seems like the simplest solution.
You could use REGEXP_SUBSTR() but a regex is an expensive operation so you would be much better off using SUBSTR() and INSTR():
SELECT SUBSTR(ID_Descr, INSTR(ID_Descr, '8'), 10) FROM tableA;
If you really wanted to use REGEXP_SUBSTR() you could do it as follows:
SELECT REGEXP_SUBSTR(ID_Descr, '8.{9}') FROM tableA;
This would get 8 plus the following 9 characters (. being the wildcard).
Now if you wanted to match only digits, then REGEXP_SUBSTR() would probably be your best bet:
SELECT REGEXP_SUBSTR(ID_Descr, '8[0-9]{9}') FROM tableA;

How can I use RTRIM or REPLACE when I know the length I want to trim but not what it may contain?

I need to RTRIM the last 7 characters from a result set in an Oracle query. These 7 chars can be anything; spaces, alpha numeric etc... and I don't know the exact length of any value.
So for example I'd like to run something like this
SELECT RTRIM (COl_A, (SELECT LENGTH (COL_A)-7) FROM TABLE_ONE;
or a replace equivalent
SELECT REPLACE(COL_A, (SELECT LENGTH (COL_A)-7 FROM TABLE_ONE),'');
Do I need to do something with SUBSTRING maybe?
I know how to remove/replace specific chars but I'm having trouble when dealing with unknown chars. I've seen a few examples of similar problems but they seem unnecessarily complicated... or does this require a more in depth solution than I think it should?
As always thanks in advance for advice or hints.
You are in search of the substr function.
select substr(col_a, 1, length(col_a) - 7) from table_one
Actually, the correct solution is:
select substr(col_a, 1, (case when length(col_a) < 7 then 0 else length(col_a) - 7 end) from table_one
To be general, you would want to take into account what happens when the length is less than 7.

How to get rightmost 10 places of a string in oracle

I am trying to fetch an id from an oracle table. It's something like TN0001234567890345. What I want is to sort the values according to the right most 10 positions (e.g. 4567890345). I am using Oracle 11g. Is there any function to cut the rightmost 10 places in Oracle SQL?
You can use SUBSTR function as:
select substr('TN0001234567890345',-10) from dual;
Output:
4567890345
codaddict's solution works if your string is known to be at least as long as the length it is to be trimmed to. However, if you could have shorter strings (e.g. trimming to last 10 characters and one of the strings to trim is 'abc') this returns null which is likely not what you want.
Thus, here's the slightly modified version that will take rightmost 10 characters regardless of length as long as they are present:
select substr(colName, -least(length(colName), 10)) from tableName;
Another way of doing it though more tedious. Use the REVERSE and SUBSTR functions as indicated below:
SELECT REVERSE(SUBSTR(REVERSE('TN0001234567890345'), 1, 10)) FROM DUAL;
The first REVERSE function will return the string 5430987654321000NT.
The SUBSTR function will read our new string 5430987654321000NT from the first character to the tenth character which will return 5430987654.
The last REVERSE function will return our original string minus the first 8 characters i.e. 4567890345
SQL> SELECT SUBSTR('00000000123456789', -10) FROM DUAL;
Result: 0123456789
Yeah this is an old post, but it popped up in the list due to someone editing it for some reason and I was appalled that a regular expression solution was not included! So here's a solution using regex_substr in the order by clause just for an exercise in futility. The regex looks at the last 10 characters in the string:
with tbl(str) as (
select 'TN0001239567890345' from dual union
select 'TN0001234567890345' from dual
)
select str
from tbl
order by to_number(regexp_substr(str, '.{10}$'));
An assumption is made that the ID part of the string is at least 10 digits.