I have been trying to figure out how to cleanly extract some_value from something that looks like this:
SELECT protobuff_text FROM table_name;
>>
resource_id {
high_part: 10310138280989442894
low_part: 12186277462739912197
}
...
}
known_field_name: some_value
mask {
points {
...
It should be extracted without assuming anything about what happens before or after it besides the newline. This is easy in MySQL... but I have been having a hard time figuring it out in postgres.
Any PostgreSQL experts out there who can help me with this?
P.S. The string that is inserted into the database is created via com.google.protobuf.TextFormat.printToString(Message m).
Try something like:
SELECT regexp_matches(protobuff_text,'resource_id\s*\{\s*high\_part\:\s*([0-9]*)\s*low\_part\:\s*([0-9]*)\s*\}')
FROM table_name;
This regex will get you the numeric values, you shown in the example.
For the known_field_name try something like:
SELECT regexp_matches(protobuff_text,'known_field_name\:\s*(\w*)')
FROM table_name;
Details here.
Related
I want to search and filter the sqlite database based on a input provided by user using SQL like command. %pattern% works fine, but %+pattern+% format didnt work, Anyother way of using the same?
I have tried the different forms of the query in DB browser for sqlite.. Didnt find luck..
Code:
return this.database.executeSql("SELECT * FROM table where name LIKE '%'+im+'%'",[] )
Expected to see the same output as %im% when using '%'+im+'%' and "im" will be replaced by user's input
You need to use || concatenate strings:
SELECT * FROM table WHERE name LIKE '%' || im || '%'
db<>fiddle demo
let say that the json object is > { "foo": "bar"}
after stringyfing i got > "{ \"foo\": \"bar\" }"
how can I get back the orginal json object using UPDATE sql query?
i'm aware of that it's a bad DB architecture it was designed by another engineer before me, that's why I would like get back the original json data and then alter the column to jsonb
Update:
please be aware that I'm looking for an answer to do that with only sql query and without any involving of programming languages like javascript.. etc
I was able to solve this same issue by doing some regexp replaces. You might not need the where clause, but in my case, I had a bug that started to stringify the JSONB column so only some of my data needed this change applied.
update your_table
set text =
jsonb(regexp_replace(regexp_replace(regexp_replace(text::text, '"$', ''), '^"+', ''), '\\"', '"', 'g'))
where text->>'foo' is null;
You could do to_json('{ \"foo\": \"bar\" }'::text)
so it would be something like
update yourtable set yourjsoncolumn = to_json(yourjsoncolum::text)
how do I test if a value is between two of my database columns?
I need to do something like this:
between(column1, column2, 'value')
or something like this:
or{
and {
ge("hora_inicio", evento.hora_inicio)
le("hora_fim", evento.hora_inicio)
}
and {
ge("hora_inicio", evento.hora_fim)
le("hora_fim", evento.hora_fim)
}
}
any ideias?
Thanks!
There is not an option to check that two columns are between a value. You are going to have to do two separate comparisons. You can compare one database column to another though, but it doesn't see like that's quite what you want. See documentation on this here: http://docs.grails.org/latest/ref/Domain%20Classes/createCriteria.html
From your second example, it looks like you need to find all the entries that have a start time between evento.hora_inicio and evento.hora_fim or that have a order time between evento.hora_inicio and evento.hora_fim.
It feels like this can be simplified to look for all the entries that have an hora_inicio before the evento.hora_fim and have a hora_fim after the evento.hora_inicio. That would look something like this:
Event evento = getEventFromSomewhere()
Appointment.createCriteria().list{
le 'hora_inicio', evento.hora_fim
ge 'hora_fim', evento.hora_inicio
}
I am trying to query a database to obtain rows that matches 4 conditions.
The code I'm using is the following:
$result = db_query("SELECT * FROM transportesgeneral WHERE CiudadOrigen LIKE '$origen%' AND DepartamentoOrigen LIKE '$origendep' AND DepartamentoDestino LIKE '$destinodep' AND CiudadDestino LIKE '$destino%'");
But it is not working; Nevertheless, when I try it using only 3 conditions; ie:
$result = db_query("SELECT * FROM transportesgeneral WHERE CiudadOrigen LIKE '$origen%' AND DepartamentoOrigen LIKE '$origendep' AND DepartamentoDestino LIKE '$destinodep'");
It does work. Any idea what I'm doing wrong? Or is it not possible at all?
Thank you so much for your clarification smozgur.
Apparently this was the problem:
I was trying to query the database by using the word that contained a tittle "Petén" so I changed the database info and replaced that word to the same one without the tittle "Peten" and it worked.
Now, im not sure why it does not accept the tittle but that was the problem.
If you have any ideas on how I can use the tittle, I would appreciate that very much.
This should be fairly simple, though I can't seem to find a single example.
I want to create a query looking like this:
SELECT column_name FROM table_name WHERE column_name IN (value1,value2,...)
As an option I could append OR-clauses to the end of the query.
The code I have written so far keeps blowing up with a Nullpointer:
#Select(sql = "select storename from broadcastrecipient where storecity in (?{1})")
public List<String> getStoresForCities(List<String> theCities) throws SQLException;
Thanks in advance.
//Abean
NOTE: I forgot to add some info about my env: PostGres 8.3, Java 1.6 and EOD SQL 0.9.
Thank you Jason.
For those who like to know, the query looks something like this:
#Select(sql = "select distinct(storename) from broadcastrecipient where storecity = any (?{1})", disconnected=true)
public List<String> getStoresForCities(String[] theCities) throws SQLException;
And I also needed to implement a TypeMapper class to map SQL-array to Java array.
I would suggest looking at EoD SQL 2.0: https://eodsql.dev.java.net/
Then take a look at the QueryTool.select() method which is designed exactly for this situation.
EoD SQL 0.9 has no functionality to cope with this sort of query built in.
A really ugly hack is to create a temp table, and populate it with your array data. Then run the query as:
#Select("SELECT storename FROM broadcastrecipient WHERE storecity IN (SELECT * FROM tmp_cities)")
public List<String> getStoresForCurrentCities();