SQLite Select Last Occurance of Character - sqlite

I'm using SQLite, and I'm unable to find a way to locate the index of the last occurrence of a character. For example, the records that I need to parse are:
test123.contoso.txt
testABC.contoso.atlanta.docx
another.test.vb
I would appreciate if anybody can point me in the direction how I can parse the file extensions (txt, docx, vb) from these records through a SQLite query. I've tried using the REVERSE function, but unfortunately SQLite doesn't include this in it's toolbox.

You can adapt the solution in How to get the last index of a substring in SQLite? to extract the extension.
select distinct replace(file, rtrim(file, replace(file, '.', '')), '') from files;

If you want to check whether a file name has a specific extension, you can use LIKE:
... WHERE FileName LIKE '%.txt'
However, it is not possible with the built-in functions to extract the file extension.
If you need to handle the file extension separately, you should store it separately in the database, too.

Related

db2 create index but hit SQLSTATE=42703

I have a table created successfully.
1 of the column name is code and another 1 is "deleted".
Thus, I plan to use this 2 field to create its index. I am doing something like follow:
CREATE INDEX SADM.IDX_SC_IDX1 on SADM.SC ("code" ASC, "DELETED") ALLOW REVERSE SCANS;
This is working fine in my local. However, I hit this error in UAT:
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0205N Column, attribute, or period "code" is not defined in
"SADM.SC". SQLSTATE=42703
I double check the table and confirm the "code" column or "deleted" is exist and same with my local.
I believe something wrong is inside but I cant find the root cause.
Kindly advise.
As per my comment. You are using double-quotes around the column names the column case (uppercase, lowercase) must match between the table-definition and the index definition.
Make sure to name the columns as they were created and are listed in the system catalog. Look for the column names in SYSCAT.COLUMNS (for most Db2 versions). If you don't use quotes, Db2 converts identifiers to uppercase by default. However, if you use quotes they always need to be referenced exactly as written.
"code" is different from "Code" or "COde" or CODE. Thus, check how the column is really named.

Reading individual statements in an Sqlite dump

I have an SQLite database and I want to dump and read individual statements from the dump. The problem I am encountering is that the individual statements can span multiple lines. So, I can't just read the dump file line-by-line. Is there a way to change the default line separator for .dump/.output command?
The .dump command always use a new-line as separator.
Any combination of characters (except a single ') is valid inside a string, and semicolons are valid inside a trigger body. To be able to read individual statements, you'd have to implement an SQL parser.

Get index of first character apperence without extension functions?

Is it possible to find index of an character in SQLite without using extension functions?
I need to substring texts like below from the beginning until ( character in a SELECT statement.
TT 15 (Something...)
TT 5 (blabla...)
I cannot use instr in our version of SQLite (i think it is 3.6) and it's not possible to update SQLite either.
If it were possible to do something like instr() without actually calling instr(), the authors of SQLite would not have felt the need to add instr().
If you cannot update SQLite or create an extension function, you have to read the entire string from the database and search it in your own code.

Sqlite: How to cast(data as TEXT) for BLOB

I have a sqlite database from which I want to extract a column of information with the datatype BLOB. I am trying this:
SELECT cast(data as TEXT) FROM content
This is obviously not working. The output is garbled text like this:
x��Uak�0�>�8�0Ff;I�.��.i׮%�A��s�M
The data in the content column is mostly text, but may also have images (which I recognized could cause a problem if I cast as TEXT). I simply want to extract that data into a usable format. Any ideas?
You can use
SELECT hex(data) FROM content
or
SELECT quote(data) FROM content
The first will return a hex string (ABCD), the second quoted as an SQL literal (X'ABCD').
Note that there's (currently) no way of converting hexadecimal column information back to a BLOB in SQLite. You will have to use C/Perl/Python/… bindings to convert and import those.
You can write some simple script which will save all blobs from your database into files. Later, you can take a look at these files and decide what to do with them.
For example, this Perl script will create lots of files in current directory which will contain your data blob fields. Simply adjust SELECT statement to limit fetched rows as you need:
use DBI;
my $dbh = DBI->connect("dbi:SQLite:mysqlite.db")
or die DBI::errstr();
my $sth = $dbh->prepare(qq{
SELECT id, data FROM content
});
$sth->execute();
while (my $row = $sth->fetchrow_hashref()) {
# Create file with name of $row->{id}:
open FILE, ">", "$row->{id}";
# Save blob data into this file:
print FILE $row->{data};
close FILE;
}
$dbh->disconnect();

Using Full text search on varbinary data type for searching file contents

I create a full-text search index on varbinary(max) column and insert file content into this column.(files like docx,xlsx,txt,...) I create an extension column for specify content type.but when I query this table with contains,
SELECT *
FROM tblFiles
WHERE CONTAINS(tblFiles.FileContent, 'web OR webserver');
no result were showing.why?
You may need to wait a bit after defining a full text index, because the index itself is created in the background; it's an async process, not synchronous. For a small table it may not be noticeable, but for a large one, it will be.
Also, make sure the types in your file type column start with a dot: .docx instead of just docx.
I found why my query dont work. because files that inserted into db was docx and in my database does not exist any filter for this format.I inserted a doc document and query it.It works. :)
Thank you.

Resources