I'm trying to write a query that only includes customer numbers either six or seven digits long. The numbers are stored in a VARCHAR(30) field in Teradata. I've tried the following:
...
AND LENGTH(STAFF_NO) > 5
AND LENGTH(STAFF_NO) < 8
...
...
AND CHARACTER_LENGTH(STAFF_NO) > 5
AND CHARACTER_LENGTH(STAFF_NO) < 8
...
...
AND CHAR_LENGTH(STAFF_NO) > 5
AND CHAR_LENGTH(STAFF_NO) < 8
...
but all of these have returned no rows; the query, in each case, has only looked at the maximum length of the field (30) rather than the actual number of characters in it.
How can I filter so it only checks the number of actual characters in the field?
It depends on how you want to handle the trailing / leading whitespace.
Include: CHAR_LENGTH(STAFF_NO) BETWEEN 6 AND 7
Exclude: CHAR_LENGTH(TRIM(STAFF_NO)) BETWEEN 6 AND 7
It shouldn't make a difference whether it's VARCHAR or CHAR.
Use:
LENGTH(TRIM(BOTH FROM STAFF_NO)) AS STAFF_NO_LENGTH
This removes empty spaces from either side of the string.
You need to use CHARACTER_LENGTH function to get length of character in VARCHAR column.
Cheers!!
Each functions that you used should return the length of column value not the max length of the column itself. Probably, there are extra spaces at the end of STAFF_NO? Try adding a TRIM as follows:
AND CHARS(TRIM(STAFF_NO)) BETWEEN 6 AND 7
I have a requirement to convert the very lengthy amount to a comma separated value in oracle. I was searching in google. but I got some solutions which works only for small numbers. But not for lengthy number. Below is the solution I have. But not working properly. I was getting ############... if I run the below.
SELECT TO_CHAR(6965854565787645667634565432234565432345643265432345643242087,
'99G999G999G9999', 'NLS_NUMERIC_CHARACTERS=",."') as test
FROM dual;
Desired output:
6,965,854,565,787,645,667,634,565,432,234,565,432,345,643,265,432,345,643,242,087
Please help me. thanks in advance.
Please check if below query can help.
SELECT ltrim(regexp_replace('00'
|| '6965854565787645667634565432234565432345643265432345643242087', '(...)', ',\1' ),',|0') AS t
FROM dual;
Numbers in Oracle can't have more than 38 significant digits. You have many more than that.
If I may, what kind of "amount" is that? My understanding is that Oracle was designed to handle real-life values. What possible meaning is there to the sample number you posted?
Added: Original poster in a comment (below) stated that he is getting the same error with a shorter number, only 34 digits.
Two issues. First, the format model must have at least the needed number of digits (of 9's). to_char(100000, '9G999') will produce the output #### because the format model allows only 4 digits, but the input is 6 digits.
Then, after that is corrected, the output may still look incorrect in the front-end application, like SQL*Plus. In SQL*Plus the default width of a number column is 10 (I believe). That can be changed to 38, for example with the command set numwidth 38. In other front-ends, like Toad and SQL Developer, the default numeric width is a setting that can be changed through the graphical user interface.
More added - actually the result of to_char is a string, and by default strings of any length should be displayed OK in any front-end, so the numeric width is probably irrelevant. (And, in any case, it does not affect the displaying of strings, including the result of to_char().)
SELECT TO_CHAR(
6676345654322345654323456432654323456,
'999G999G999G999G999G999G999G999G999G999G999G999G999',
'NLS_NUMERIC_CHARACTERS=",."') as test FROM dual
TEST
------------------------------------------------------------
6,676,345,654,322,345,654,323,456,432,654,323,456
#AlexPoole pointed out that perhaps your input is a string.
I didn't get that vibe; but if in fact your input IS a string, and if you know the length is no more than 99 digits, you could do something like below. If your strings can be longer than 99, replace 99 below with a sufficiently large multiple of 3. (Or, you can replace it with a calculated value, 3 * ceil(length(str)/3)).
with
inputs ( str ) as (
select '12345678912345' from dual
)
-- WITH clause is only for testing/illustration, not part of the solution
select ltrim(regexp_replace(lpad(str, 99, ','), '(.{3})', ',\1'), ',') as test
from inputs;
TEST
------------------
12,345,678,912,345
How to specify length constraint of min and max digits on dijit.form.ValidationTextBox programmatically ?
Thanks
I recommend you to use the 'pattern' option which used regex to manage min and max:
eb = new ValidationTextBox({
value: 2,
pattern: '[\\w]{2,4}'
});
I will explain the pattern: [\\w]{2,4}.
\w: accept letters only.
{2,4}: define min and max letters that are allowed. min is 2 and max is 4.
If you want to unlimit the max, leave it empty like this: {2,}.
You can add \\d AS numbers only or combine then with letters like this: \\w\\d
I have a column of numbers between 0 - 6 digits long. For those less than 6 I need to pad out with zeros to ensure they are all 6 digits i.e 12563 = 012563 or 23 000023 etc etc. Can someone recommend a solution?
Probably the easiest way to pad numbers with leading zeros would be to use the Format() function, as in
Format(fieldName, "000000")
If you're searching on this (like for PIN numbers, where '12' would be represented as '000012' here's an example using Gord's correct answer;
SELECT CStr(Format(fieldName,"000000")) FROM table WHERE CStr(Format(fieldName,"000000"))="000012";
I had a similar issue. I couldn't change the field on the actual file because it was a split database and it had to be changed on the data source (Database_be). I went to the data source and made the change from Number to Short Text to all tables and that was it... Like magic!!
Try:
Update TABLE set DIGITS = string(6- len(DIGITS),"0")
DIGITS TABLE is the table where your numbers are stored.
DIGITS is the field that contains your numbers.
The above does NOT work.
Corrected version:
Update TABLE set DIGITS = string(6- len(DIGITS),"0")&DIGITS
The number '6' can be altered for whatever the total length of your field.
I want to be able to differentiate between a string that is alphnumeric and a string that is in hex format.
My current query is:
<columnName> LIKE '?_____=' + REPLICATE('[0-9A-Fa-f]',16)
I found this method of searching for hex ID's online and I thought it was working. However after getting a significantly larger sample size I can see a high false positive rate in my results. The problem is that this gives me all the results I do want but it also gives me a bunch of results I dont care about. For example:
I want to see:
<url>.php?mains=d7ad916d1c0396ff
but i dont want to see:
<url>.php?mblID=2007012422060265
The difference between the 2 strings is that the 16 characters at the end that i want to collect are all numeric and not a hex ID. What are some ways you guys use to limit the results to hex ID only? Thanks in advnace.
UPDATE:
Juergen brought up a good point, the second number could be a hex value to. Not all hex numbers contain [a-F]. I would like to rephrase the question to state that I am looking for an ID with both letters and numbers in it, not just numbers.
The simplest way is just to add a separate clause for that restriction:
<columnName> LIKE '?_____=' + REPLICATE('[0-9A-Fa-f]',16)
AND <columnName> NOT LIKE '?_____=' + REPLICATE('[0-9]',16)
It should be fairly simple to determine if a string contains only numbers...
Setting up a test table:
CREATE TABLE #Temp (Data char(32) not null)
INSERT #Temp
values ('<url>.php?mains=d7ad916d1c0396ff')
,('<url>.php?mblID=2007012422060265 ')
Write a query:
SELECT
right(Data, 16) StringToCheck
,isnumeric(right(Data, 16)) IsNumeric
from #Temp
Get results:
StringToCheck IsNumeric
d7ad916d1c0396ff 0
2007012422060265 1
So, if the IsNumeric function returns 0, it could be a hex string.
This makes several assumptions:
The rightmost 16 characters are what you want to check
You only ever hit 16 characters. I don't know when the string would get too long to check.
A non-numeric character means hex. Any chance of "Q" or "~" being embedded in the string?