Insert a number in table without special character - plsql

I need a procedure or function for validation-
if i insert data in a column then only that numbers will enter which have only integers not special character not alphanumeric values.

If I understand it correctly you need a function that in a string with numbers, letters and special characters will keep only the numbers.
In that case try with REGEXP_REPLACE
E.g.:
SELECT REGEXP_REPLACE('1/A2!46', '([^0-9])+', '') FROM dual;
Output:
REGEXP_REPLACE('1/A2!46','([^0-9])+','')
-----------------------------------------
1246

Related

Teradata: expected something like ',' between an integer and the word

I'm trying to create a table and I get the error. Could someone please let me know how to add a column which has an integer starting in its name. Find below the statement and error
Create table mutablecode
(
4th_Procedure_Code varchar(20)
);
Syntax error, expected something like ','
between an integer and the word 'th_Procedure_Code'
A valid object name in Teradata consists of a-z,A-Z,0-9,#,_,$ but must not start with a digit.
If you really need this column name you must double quote it (then almost any character is allowed):
"4th_Procedure_Code" varchar(20)
Remark: According to Standard SQL a double quoted name is case-sensitive, but in Teradata it's still case-insensitive.

How to convert a number with decimal values to a float in PL/SQL?

The issue is that I need to insert this number into json, and because the number contains a comma, json becomes invalid. A float would work because it contains a period not a comma.
I have tried using replace(v_decimalNumber,',','.') and it kind of works, except that the json property is converted to a string. I need it to remain some type of a numerical value.
How can this be achieved?
I am using Oracle 11g.
You just need to_number() function.
select to_number(replace('1,2', ',', '.')) float_nr from dual;
Result:
FLOAT_NR
1.2
Note that if your number has .0 like 1.0, the function will remove it and leave it only 1
The data type of v_decimalNumber is some type of character format as it can contain commas (,). Your contention is that it contains a number once the commas are removed. However there is NO SUCH THING until that contention has been validated since being character I can put any character(s) I want into it subject to any length restriction. As an example a spreadsheet column that should contain numeric data. However, it that doesn't apply users will often put N/A into telling themselves that it doesn't apply. Oracle will happily load this into your v_decimalNumber. (And that's 1 of many many ways non-numeric data can get into your column.) So before attempting to process as a numeric value you must validate it is in fact valid numeric data. The following demonstrates one such way.
with some_numbers (n) as
( select '123,4456,789.00' from dual union all
select '987654321.00' from dual union all
select '1928374655' from dual union all
select '1.2' from dual union all
select '.1' from dual union all
select '1..1' from dual union all
select 'N/A' from dual
)
, rx as (select '^[0-9]*\.?[0-9]*$' regexp from dual)
select n
, case when regexp_like(replace(n,',',null), regexp)
then to_number(replace(n,',',null))
else null
end Num_value
, case when regexp_like(replace(n,',',null), regexp)
then null
else 'Not valid number'
end msg
from some_numbers,rx ;
Take away: Never trust a character type column to contain specific data requirements except random characters. Always validate then put the data into the appropriately defined columns.

How to get a count of number of occurrence of a substring in a string in teradata?

I have a column in a teradata table with string values like "page1-->page2-->page1-->page3-->page1--page2-->..."
I want to search for a specific page and get the number of occurrence of the page in the string. I couldn't find any function that gives this result.
There's no builtin function, but there's a common solution:
Remove all occurences of the substring from the string and compare the length before/after:
(Char_Length(string) - Char_Length(OReplace(string, searchstr))) / Char_Length(searchstr)
Edit:
For a wildcard search you can utilize REGEXP_REPLACE:
Char_Length(RegExp_Replace(RegExp_Replace(s, 'page1(.+?)page3', '#',1,0), '[^#]','',1,0))
For `#' use a character which is known not to be in your input string.

Query a manual list of data items

I would like to run a query involving joining a table to a manually generated list but am stuck trying to generate the manual list. There is an example of what I am attempting to do below:
SELECT
*
FROM
('29/12/2014', '30/12/2014', '30/12/2014') dates
;
Ideally I would want my output to look like:
29/12/2014
30/12/2014
31/12/2014
What's your Teradata release?
In TD14 there's STRTOK_SPLIT_TO_TABLE:
SELECT *
FROM TABLE (STRTOK_SPLIT_TO_TABLE(1 -- any dummy value
,'29/12/2014,30/12/2014,30/12/2014' -- any delimited string
,',' -- delimiter
)
RETURNS (outkey INTEGER
,tokennum INTEGER
,token VARCHAR(20) CHARACTER SET UNICODE) -- modify to match the actual size
) AS d
You can easily put this in a Derived Table and then join to it.
inkey (here the dummy value 1) is a numeric or string column, usually a key. Can be used for joining back to the original row.
outkey is the same as inkey.
tokennum is the ordinal position of the token in the input string.
token is the extracted substring.
Try this:
select '29/12/2014'
union
select '30/12/2014'
union
...
It should work in Teradata as well as in MySql.

String Aggregation in sqlite

Anyone knows if String Aggregation in sqlite is possible?
If i have an animal column with 5 rows/datas, how can i combine them so that the output would be in one field
'dog','cat','rat','mice','mouse' as animals
Thanks
You're looking for something like the following:
select group_concat(animal) from animals;
This will return something like the following:
dog,cat,rat,mice,mouse
If you don't want to use a comma as the separator, you can add your own separator as a second parameter:
select group_concat(animal, '_') from animals;
which will return:
dog_cat_rat_mice_mouse
I think this will be useful:
group_concat(X)
group_concat(X,Y)
The group_concat() function returns a string which is the concatenation of all non-NULL values of X. If parameter Y is present then it is used as the separator between instances of X. A comma (",") is used as the separator if Y is omitted. The order of the concatenated elements is arbitrary.

Resources