Generate native oracle database alphanumeric sequence - database

Can anyone give me some idea to generate native oracle database alphanumeric sequence with column length 4, like 000A,000B and so on. I need 200k or more records of this sequence.
Any ideas/solutions are highly appreciated.
Thanks in advance

If you want a SELECT statement to generate hexadecimal values then:
SELECT TO_CHAR(LEVEL, 'FM0000X')
FROM DUAL
CONNECT BY LEVEL <= 200000;
If you want a SEQUENCE then they only generate numeric values; however, you can wrap its output in the same TO_CHAR function:
CREATE SEQUENCE table_name__id__seq;
CREATE TABLE table_name (
id VARCHAR2(5)
);
INSERT INTO table_name (id)
VALUES (TO_CHAR(table_name__id__seq.NEXTVAL, 'FM0000X'));
SELECT * FROM table_name;
Outputs:
ID
00001
db<>fiddle here

Related

Oracle 11g how to store multi values to one column but get ok performance

Hi,
My database is Oracle11g. For some reason, I need to store one or several values in column1. In this case I use '||' as a delimiter. So the sql query is trying to answer, e.g. if value 310-G01-000-000-000 in column1?
I want a solution, for Oracle 11g, to enhance the query performance? I know that PostgreSQL column can store an array, and there is index for array. But for Oracle, I don't know.
On of my thought is using REGEXP_SUBSTR to generate a list, and use function index on REGEXP_SUBSTR may work?
Please let me know the best practice to store multi values to one column and the way to query it quickly?
Thanks a lot
J
If you want performance then don't store delimited values in a column.
If you cannot avoid it then just use simple string functions (which are an order of magnitude faster than regular expressions) to check if there is a sub-string match:
SELECT *
FROM test_table
WHERE '||' || column1 || '||' LIKE '%||' || :search_value || '||%';
or:
SELECT *
FROM test_table
WHERE INSTR('||' || column1 || '||', '||' || :search_value || '||') > 0;
However
You could use a nested table:
CREATE TYPE string_list IS TABLE OF VARCHAR2(19);
CREATE TABLE test_table (
id VARCHAR2(10),
column1 string_list
) NESTED TABLE column1 STORE AS test_table__column1;
Then:
INSERT INTO test_table (
id,
column1
) VALUES (
'abc',
string_list('310-G01-000-000-000', '310-G04-000-000-000','310-G04-000-000-001')
);
and to find the value use the MEMBER OF collection operator:
SELECT id
FROM test_table
WHERE '310-G01-000-000-000' MEMBER OF column1;
db<>fiddle here

Why two string do not match although they are exactly the same?

I have a lookup table with a list of values. Lookup Table I need to filter on a value from the LUT table in a simple where condition. It works with all table values except one and I don't know why. I have tried using trim function and lower function to change the string but nothing helped. Does anyone have the same experience? Why it does work for all table values except one? My code:
SELECT * FROM "PossibleNewGMCIssues" WHERE "gmcIssue" = 'Suspended account for policy violation'
Thank you in advance.
It's too hard for anybody to say without seeing the strings themselves. They probably look similar but have different unicode values. You can convert to the hex values to see where they are different though by using hex_encode:
Below I create a table that uses two strings that look the same but aren't. One contains an m-dash and the other an en-dash.
-- Create a table with two columns with strings in them that look the same but arent
create or replace transient table test_table as (
select 'a-string'::string col1, 'a—string'::string col2
);
-- This returns 0 results
select * from test_table where col1=col2;
-- You can tell that the strings are different by checking the hex representation of them
select hex_encode(col1), hex_encode(col2)
from test_table
;
-- The above returns:
-- +----------------+--------------------+
-- |HEX_ENCODE(COL1)|HEX_ENCODE(COL2) |
-- +----------------+--------------------+
-- |612D737472696E67|61E28094737472696E67|
-- +----------------+--------------------+

counting all the words matching the pattern in sql server records

I'm trying to count all the words in sql records having the following pattern
example:
[stackoverflow]
[stackexchange]
[control]
How to perform this in sql server.
I can able to count the known words, but how to count all the occurrences of words with pattern [ ]
Helps much appreciated
USE the pattern ('[[]%]') to get count for words start and end with '[]'
SELECT COUNT(*)
FROM YourTable WHERE SearchColumn LIKE ('[[]%]')
mysql> SELECT SUM(column_name REGEXP '^\\[.*\\]') from table_name;
should do the thing for you
The SQL 'delimits' the special character with square brackets, telling the engine to treat it as a normal literal character instead of a character with special meaning. Check here for more info.
CREATE TABLE #test
(
a VARCHAR(50)
)
INSERT INTO #test
VALUES ('[stackoverflow]'),
('[stackoverflow]'),
('[stackexchange]'),
('[control]'),
('control')
SELECT a,
Count(a)
FROM #test
WHERE a LIKE '%[[]%]%'
GROUP BY a

SEQUENCE in SQL Server 2008 R2

I need to know if there is any way to have a SEQUENCE or something like that, as we have in Oracle. The idea is to get one number and then use it as a key to save some records in a table. Each time we need to save data in that table, first we get the next number from the sequence and then we use the same to save some records. Is not an IDENTITY column.
For example:
[ID] [SEQUENCE ID] [Code] [Value]
1 1 A 232
2 1 B 454
3 1 C 565
Next time someone needs to add records, the next SEQUENCE ID should be 2, is there any way to do it? the sequence could be a guid for me as well.
As Guillelon points out, the best way to do this in SQL Server is with an identity column.
You can simply define a column as being identity. When a new row is inserted, the identity is automatically incremented.
The difference is that the identity is updated on every row, not just some rows. To be honest, think this is a much better approach. Your example suggests that you are storing both an entity and detail in the same table.
The SequenceId should be the primary identity key in another table. This value can then be used for insertion into this table.
This can be done using multiple ways, Following is what I can think of
Creating a trigger and there by computing the possible value
Adding a computed column along with a function that retrieves the next value of the sequence
Here is an article that presents various solutions
One possible way is to do something like this:
-- Example 1
DECLARE #Var INT
SET #Var = Select Max(ID) + 1 From tbl;
INSERT INTO tbl VALUES (#var,'Record 1')
INSERT INTO tbl VALUES (#var,'Record 2')
INSERT INTO tbl VALUES (#var,'Record 3')
-- Example 2
INSERT INTO #temp VALUES (1,2)
INSERT INTO #temp VALUES (1,2)
INSERT INTO ActualTable (col1, col2, sequence)
SELECT temp.*, (SELECT MAX(ID) + 1 FROM ActualTable)
FROM #temp temp
-- Example 3
DECLARE #var int
INSERT INTO ActualTable (col1, col2, sequence) OUTPUT #var = inserted.sequence VALUES (1, 2, (SELECT MAX(ID) + 1 FROM ActualTable))
The first two examples rely on batch updating. But based on your comment, I have added example 3 which is a single input initially. You can then use the sequence that was inserted to insert the rest of the records. If you have never used an output, please reply in comments and I will expand further.
I would isolate all of the above inside of a transactions.
If you were using SQL Server 2012, you could use the SEQUENCE operator as shown here.
Forgive me if syntax errors, don't have SSMS installed

Ms Sql Convert and insert between 2 databases

I have two databases.I insert data from DATABASE_2 TO DATABASE_1 however i need to convert some column.
I must convert Customer_Telephone_Number from varchar to bigint after that insert it.
So,
My Question is in below.
SET IDENTITY_INSERT DATABASE_1.dbo.CUSTOMER_TABLE ON
INSERT INTO DATABASE_1.dbo.CUSTOMER_TABLE
(
Customer_Id,
Customer_Telephone_Number
)
Select
Customer_Id,
Customer_Telephone_Number // This is varchar so i need to convert it to Big int.
from
DATABASE_2.DBO.CUSTOMER_TABLE
Any help will be appreciated.
Thanks.
If the data stored is without spaces or other non numeric symbols:
Select
Customer_Id,
CONVERT(BIGINT,Customer_Telephone_Number)
from
DATABASE_2.DBO.CUSTOMER_TABLE
For instance, if there is value with (222)-3333-333 it would fail. If the value was 2223333333 it would succeed

Resources