sql query to retrieve numeric and alphanumeric entries - sql

Hi can anyone help with a quire to retrieve both numeric and alpha numeric entries from a column.
Example: Column is 'ID' and entries could be = abc0000 or could be all 9999999999.
The search is a list of numeric and alphanumeric items.
Example: 8765398
ha34789
ss0487667
7629

You could do:
SELECT * FROM table WHERE ID REGEXP '^[A-Za-z0-9]+$'
You could also use a named character:
SELECT * FROM table WHERE ID REGEXP '^[[:alnum:]]+$'

Related

How I can use where Condition in SQL I have comma separated value in A columns in multiple rows

Column1 EventTypes_pKey
Are 5,3
Test 1,4,5
test 1,3,5
If I am using
Select * from Table name where EventTypes_pKey in('5,1,4)
then I want that record where these value belongs the column.
How I can use where condition on the basis of EventTypes_pKey this is my Varchar column.
I want If I am selecting 5,3,4 the there should be all three row data.
Please help me.
If you are using Postgres, you can do this by by converting the value into an array and then using the overlaps operator &&
select *
from badly_designed_table
where string_to_array(eventtypes_pkey, ',')::int[] && array[5,3,4];
Online example

How to query array which is present as string in postgres?

I have a table called table with column called column with datatype text with values like '["1","2"]'.
I need to get all records which has "1" as one of the element.
select *
from table
where column.....?
How should the where clause be?
Simply use LIKE. Keep the double quotes to pass 1, but avoid other numbers containing that digit.
select *
from table
where column like '%"1"%'
I think you can use ? operator on jsonb type:
select *
from (
select '["1","2"]' union all
select '["0"]'
) as a(data)
where
a.data::jsonb ? '1'
In general, I'd consider storing your data as jsonb instead of string.
db<>fiddle example

select query return null when where cause has value with special character and comma

a column in a table has value of format lastName,firstname. some lastNames have special characters. for instance,
Full_Name
Gössel,Robert
Ocaña,Jack.
a select query
select *
from tblx
where fullname = 'Gössel,Robert';
is returning null.
How to handle special characters in database? I am using oracle 11g.
Thank you!

How to update a part of the column data using replace function in SQL?

I am trying to update a table coulmn, the table has thousands of records.
Currently I am updating the table by running the following query manually for some of the records.
UPDATE MyTable
SET column = REPLACE(column, 'ABC', 'ABC9')
WHERE where column like ‘ABC%’
Now I am trying to generate a generic query to update the table by adding a letter '9' after the alphabets. Thanks for your help
Use PATINDEX and STUFF
Patindex - Helps you to identify the first occurrence of numeric character in the string
Stuff - Helps you to insert 9 before the first occurrence of numeric character in the string
UPDATE MyTable
SET column = stuff(column,patindex('%[0-9]%',column),0,'9')

Extract alphanumeric value from varchar column

I have a table which contains a column having alphanumeric values which is stored as a string. I have multiple values in that column having values such as F4737, 00Y778, PP0098, XXYYYZ etc.
I want to extract values starting with a series of F and must have numeric values in that row.
Alphanumeric column is the unique column having unique values but the rest of the columns contain duplicate values in my table.
Futhermore, once these values are extracted I would like to pick up the max value from the duplicate row,for eg:
Suppose I have F4737 and F4700 as a unique Alphanumeric row, then F4737 must be extracted from it.
I have written a query like this but the numeric values are not getting extracted from this query:
select max(Alplanumeric)
from Customers
where Alplanumeric '%[F0-9]%
or
select max(Alplanumeric)
from Customers
where Alplanumeric like '%[0-9]%'
and Alplanumeric like 'F%'**
I run the above query but I am only getting the F series if I remove the numeric part from the above query. How do I extract both, the F starting series as well as the numeric values included in that row?
Going out on a limb, you might be looking for a query like this:
SELECT *, substring(alphanumeric, '^F(\d+)')::int AS nr
FROM customers
WHERE alphanumeric ~ '^F\d+'
ORDER BY nr DESC NULLS LAST
, alphanumeric
LIMIT 1;
The WHERE conditions is a regular expression match, the expression is anchored to the start, so it can use an index. Ideally:
CREATE INDEX customers_alphanumeric_pattern_ops_idx ON customers
(alphanumeric text_pattern_ops);
This returns the one row with the highest (extracted) numeric value in alphanumeric among rows starting with 'F' followed by one ore more digits.
About the index:
PostgreSQL LIKE query performance variations
About pattern matching:
Pattern matching with LIKE, SIMILAR TO or regular expressions in PostgreSQL
Ideally, you should store the leading text and the following numeric value in separate columns to make this more efficient. You don't necessarily need more tables like has been suggested.