To preface, I'm using PostGIS and pgAdmin4 to do all of this work.
So I've imported a set of rasters into a database with the "rid", "raster" and "filename" columns.
I used this query to add the column, where I just as easily could have used the GUI in pgAdmin to achieve the same.
ALTER TABLE IF EXISTS ch12.prec
ADD COLUMN "Month" integer;
I need to extract the month from the filename and insert it into this new column but am at a loss on how to do so. The file name structure is "prec1_16.bil", where the 1 is the month and 16 the year. I tried using an INSERT statement with a CASE expression like:
INSERT INTO ch12.prec (Month)
SELECT
CASE
WHEN filename = 'prec1_16.bil' OR filename = 'prec1_17.bil' THEN '1'
WHEN filename = 'prec2_16.bil' OR filename = 'prec2_17.bil' THEN '2'
WHEN filename = 'prec3_16.bil' OR filename = 'prec3_17.bil' THEN '3'
WHEN filename = 'prec4_16.bil' OR filename = 'prec4_17.bil' THEN '4'
WHEN filename = 'prec5_16.bil' OR filename = 'prec5_17.bil' THEN '5'
WHEN filename = 'prec6_16.bil' OR filename = 'prec6_17.bil' THEN '6'
WHEN filename = 'prec7_16.bil' OR filename = 'prec7_17.bil' THEN '7'
WHEN filename = 'prec8_16.bil' OR filename = 'prec8_17.bil' THEN '8'
WHEN filename = 'prec9_16.bil' OR filename = 'prec9_17.bil' THEN '9'
WHEN filename = 'prec10_16.bil' OR filename = 'prec10_17.bil' THEN '10'
WHEN filename = 'prec11_16.bil' OR filename = 'prec11_17.bil' THEN '11'
WHEN filename = 'prec12_16.bil' OR filename = 'prec12_17.bil' THEN '12'
END filename
FROM ch12.prec;
All I get is a column doesn't exist error, despite being able to see it and query it. Any thoughts? Would something like UPDATE be more appropriate?
Here you double-quoted the column name "Month", thereby preserving mixed-case spelling:
ALTER TABLE IF EXISTS ch12.prec
ADD COLUMN "Month" integer;
Here you didn't, thereby lower-casing the identifier:
INSERT INTO ch12.prec (Month) ...
See:
Are PostgreSQL column names case-sensitive?
And yes, you are looking for UPDATE, not INSERT:
UPDATE ch12.prec
SET "Month" = substring(filename, '^prec(1?\d)_1[67].bil$')::int;
Replacing your lengthy CASE expression with a shorter regular expression (100 % equivalent). And making it an actual integer.
Doesn't mean I would do that. I don't use mixed-case identifiers if I can avoid it. And I don't add columns with redundant information (except for special cases).
Consider not adding a column and operating with the expression I gave you instead - or possibly an even simpler one if assumptions can be made.
Related
The table meat_poultry_egg_inspect is being updated where column zip is being set to something wherever column st matches with PR, VR and length of zip is 3. I think its making the zip column into a five digit value: '00 + zip'.
UPDATE meat_poultry_egg_inspect
SET zip = '00' || zip
WHERE st IN('PR','VI') AND length(zip) = 3;
The || is the operator for string concatenation.
This is the SQL standard operator, although not all databases support it.
I have a table with filed 'myData' of type nvarchar containing sometimes a data ending with the string '|||' that I want to remove.
Obviously the data is not fixed, so I can't just use
UPDATE myTable
SET myData = REPLACE(myData, 'oldString', 'newString')
as this would work just for one record (e.g. oldString = '12-feb-17|||' and newString = '12-feb-17')
How can I do it globally?
You can do:
UPDATE myTable
SET myData = LEFT(myData, LEN(myData) - 3)
WHERE myDATE LIKE '%|||';
I am trying to update a string but I also need to wipe out the rest of the values following the found string. I can't use the replace since the value at the end will change but the middle part will stay the same for many records. I haven't seen a post for removing the trailing portion of a string if you don't know it's exact value or location within the string.
I am using Oracle for my database thru SQL Developer to update the data.
"keep this data" "search on keyword" "wipe out trailing data" "result data"
xyz # psu.edu xyz
Column data value = xyz#psu.edu
I would search for a record with columnname like '#%'
Remove "#%"
End result value = xyz
Column data value = ABCabc123
I would search for a record with columnname like 'abc%'
Remove "abc%"
End result value = ABC
I have not seen any answers with this type of update. Please help!
select substr('xyz#psu.edu',1, instr('xyz#psu.edu','#') - 1) from dual
select substr('ABCabc123',1, instr('ABCabc123','abc') - 1) from dual
You might need to check in case the string you want is not found:
SELECT CASE
WHEN INSTR('xyz#psu.edu','#', 1, 1) > 0
THEN SUBSTR('xyz#psu.edu',1,INSTR('xyz#psu.edu','#', 1, 1)-1)
ELSE 'xyz#psu.edu'
END as "Result Data"
FROM DUAL
I have a database with one table, let's say 'tablename'. Now, I want the first column of that table, named 'text' to be updated with the text it containts PLUS some new text which comes as result from another query executed over 'tablename2'. So, I want something like this:
UPDATE tablename SET text="current text" + (SELECT * FROM tablename2 where ID=12);
If 'text' value is 'Result not available' I want to append ' here', so that the field value is 'Result not available here'
How is this possible? Thanks
Try the concatenation operator (||):
UPDATE tablename SET text='current text'||(SELECT * FROM tablename2 where ID=12);
And I'm not sure, but you should use ' instead of " .
I probably misunderstood your question, but let's try:
http://sqlfiddle.com/#!5/959e5/2
UPDATE Table1
SET text = text ||
CASE
WHEN text = 'Result not available' THEN ' here'
ELSE (SELECT text FROM Table2 where id = 12)
END;
I've got a column with some string data that somewhere has 'T##' (## being a two digit number) I want to copy this into another column, how do I do that?
something like this:
abc-T03-def -> 03
For Microsoft SQL Server:
update YourTable
set NewColumn = substring(OldColumn, patindex('%T[0-9][0-9]%', OldColumn) + 1, 2)
where patindex('%T[0-9][0-9]%', OldColumn) <> 0