SQL select specific (4th) part of column BLOB data, separated by specific pattern - sql

There is a BLOB column that contains data like:
{{Property1 {property1_string}} {Property2 {property2_string}} {Property3 {property3_string}} {Property4 {property4_string}} {Property5 {property5_string}}}
I select the above column to display the BLOB data, as follows:
utl_raw.cast_to_varchar2(dbms_lob.substr(blobColumn))
I need to display only the data of 4th Property of BLOB column, so the following:
{Property4 {property4_string}}
So, I need help to create the necessary select for this purpose.
Thank you.

this will work:
select substr(cast(blobfieldname as
varchar2(2000)),instr(cast(blobfieldname as
varchar2(2000)),'{',1,8)),instr(cast(blobfieldname as
varchar2(2000)),'}',1,8))-
instr(cast(blobfieldname as varchar2(2000)),'{',1,8))) from tablename;

You may use REGEXP_SUBSTR.
select REGEXP_SUBSTR(s,'[^{} ]+', 1, 2 * :n) FROM t;
Where n is the nth property string you want to extract from your data.
n = 1 gives property1_string
n = 2 gives property2_string
..
and so on
Note that s should be the output of utl_raw.cast_to_varchar2
Demo

Related

sql query or django filter for string

hello i need a query that search in database and return a row that has most similarity from starting character with a value.
imagine, given string is 'abcdefghijklmnop'
our database table has a column named x and for this column, rows are:
1- 'a'
2- 'abc'
3- 'absde'
4- 'abcdef'
5- 'abcdefg'
6- '1abcdefg'
and it should return the row number 5
In postgres exists a function for similarity, first create the extension that contains it
create extension pg_trgm;
after that select using a percentage of similarity like this (e.g. 80% or your desired value)
select x from "table" where similarity('abcdefghijklmnop', x)>0.35;
or the most similar string would be
select * from "table" order by similarity('abcdefghijklmnop', x) desc limit 1;
Fiddle http://sqlfiddle.com/#!17/c901e/2 and docs https://www.postgresql.org/docs/current/pgtrgm.html
best one i found is this:
SELECT id,
FROM table       
WHERE '3552' LIKE prefix || '%'       
ORDER BY prefix DESC       
LIMIT 1
thank you all

How to replace the first letter of a string using SQL and Postgres?

I have a database column of varchar(191) with strings in the database. We need to replace the first letter of every string with an "E". So for instance, we have:
Cuohvi-AQNqalPq8zdr1cOA
Needs to be changed to
Euohvi-AQNqalPq8zdr1cOA
Do you know how we can achieve this in Postgres with a SQL query? It needs to be updated for the whole table.
Per docs use overlay():
UPDATE the_table SET the_field = overlay(the_field placing 'E' from 1 for 1);
Use a combination of the CONCAT function and the RIGHT function with an argument of -1.
SELECT CONCAT('E', RIGHT('Cuohvi-AQNqalPq8zdr1cOA', - 1))
FROM yourtable
SELECT CONCAT('E', RIGHT(yourfield, - 1))
FROM yourtable
dbfiddle: https://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=e198b05f02283137afc39c24bb6c788d

Hive Delimiter using :

I want to extract a column A that has values such as W:X:Y:Z.
I am interested to extract Z from Column A.
I tried multiple commands such as SPLIT(Table.A, "[:]"[3] ) but get an error.
What is the best way to do this?
Split function returns array. Array index [3] should be applied to the split function result:
with yourtable as ( -- use your table instead of this
select 'W:X:Y:Z' as A
)
select split(A,'\\:')[3] from yourtable;
Result:
Z

how to query differently-structured files?

Is it possible to execute a query against files that have different schemas?
I have 2 sets of files in the same directory. The second type has an extra field.
Type 1
id, first, last
1, liza, smith
Type 2
id, first, last, state
4, alex, gordon, CT
Desired Result
1, liza
4, alex
How do we query files with different schemas, but where you want the same output fields?
Here's what I have:
SELECT id, first
FROM "/one 1300/{files}.csv"
USING Extractors.Csv();
#interestingRows = SELECT id, first FROM #interestingRows;
OUTPUT #interestingRows
TO #uriPrefix + "/one 1300/output/output.csv"
USING Outputters.Csv();
The CSV outputter not solve your problem.
You will need a custom extractor to solve this.
I recommend you to use Flexible Extractor
check this:
https://github.com/Azure/usql/tree/master/Examples/FlexibleSchemaExtractor
https://blogs.msdn.microsoft.com/mrys/2016/08/15/how-to-deal-with-files-containing-rows-with-different-column-counts-in-u-sql-introducing-a-flexible-schema-extractor/
The other solutions is to extract data with diferent schema separatly
How about importing the column as one using a delimiter you know does not exist in the data, then splitting it afterwards using the Split method of the string class? Something like this:
#working =
EXTRACT wholeRow string
FROM "/one 1300/{*}.csv"
USING Extractors.Text(delimiter:'|');
#working =
SELECT
wholeRow.Split(',')[0] AS id,
wholeRow.Split(',')[1] AS first,
wholeRow.Split(',')[2] AS last
FROM #working;
OUTPUT #working
TO "/output/output.csv"
USING Outputters.Csv(quoting:false);
Since you said that this two types are actualy in the same file, assuming that they are like this:
You just extract it with all the columns and set quoting to false:
//Extract the data
#extractedData =
EXTRACT id int,
first string,
last string,
state string
FROM "data.csv"
USING Extractors.Csv(skipFirstNRows : 1, quoting: false);
Then you just select the fields you need and output them:
//Select the fields
#finalData = SELECT id, first FROM #extractedData;
//Output the data
OUTPUT #finalData
TO "/Desired Result.csv"
USING Outputters.Csv(quoting: false);
The desired result:

SQL Server: How to select rows which contain value comprising of only one digit

I am trying to write a SQL query that only returns rows where a specific column (let's say 'amount' column) contains numbers comprising of only one digit, e.g. only '1's (1111111...) or only '2's (2222222...), etc.
In addition, 'amount' column contains numbers with decimal points as well and these kind of values should also be returned, e.g. 1111.11, 2222.22, etc
If you want to make the query generic that you don't have to specify each possible digit you could change the where to the following:
WHERE LEN(REPLACE(REPLACE(amount,LEFT(amount,1),''),'.','') = 0
This will always use the first digit as comparison for the rest of the string
If you are using SQL Server, then you can try this script:
SELECT *
FROM (
SELECT CAST(amount AS VARCHAR(30)) AS amount
FROM TableName
)t
WHERE LEN(REPLACE(REPLACE(amount,'1',''),'.','') = 0 OR
LEN(REPLACE(REPLACE(amount,'2',''),'.','') = 0
I tried like this in place of 1111111 replace with column name:
Select replace(Str(1111111, 12, 2),0,left(11111,1))