Delete fixed string contained in SQL Server Table Column - sql

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 '%|||';

Related

Add a new table column with data extracted from existing column

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.

Replace Function - Handling single quotes and forward slashes in SQL SERVER 2008

I want to replace some data from my database where single quotes and slashes are present.
The line below is exactly how it appears in the database and I only want to remove 'F/D', from the record.
('P/P','F/D','DFC','DTP')
Been using varations of
UPDATE tablename SET columnname = REPLACE(columnname, '''F/D,''', '')
WHERE RECORDID = XXXXX
Also been using varations of
UPDATE tablename SET columnname = REPLACE(columnname, 'F/D,', '')
WHERE RECORDID = XXXXX
Seems like it should be a simple fix but I haven't had any luck yet - all suggestions are appreciated.
The reason your's doesn't work is because you aren't including the quotes. You are looking for F/D, and 'F/D,' and your data it is 'F/D',.
If it's simply 'F/D' from all values you want removed, then you also need to remove a comma and the quotes. This method removes 'F/D' and then, any double commas (in case 'F/D' is in the middle of the string).
declare #var varchar(64) = '(''P/P'',''F/D'',''DFC'',''DTP'')'
select replace(replace(#var,'''F/D''',''),',,',',')
--update tablename
--set columnname = replace(replace(columnname,'''F/D''',''),',,',',')
--where RECORDID = 1324
If you want to replace the second element in the string, here is a way:
select
#var
--find the location of the first comma
,charindex(',',#var,0)
--find the location of the second comma
,charindex(',',#var,charindex(',',#var) + 1)
--Put it all together, using STUFF to replace the values between this range with nothing
,stuff(#var,charindex(',',#var,0),charindex(',',#var,charindex(',',#var) + 1) - charindex(',',#var,0),'')
Your first version should work fine if the comma is in the right place:
UPDATE tablename
SET columnname = REPLACE(columnname, '''F/D'',', '')
WHERE RECORDID = XXXXX;
Note that this will not replace 'F/D' if it is the first or last element in the value. If that is an issue, I would suggest that you ask another question.

Netezza convert char to numeric - arithmetic - and convert again to char

I have a field PersonNumb which is varchar(30) and needs to get converted to numeric, than do some arithmetic(in this case some simple addition Value + 10, just for example) and than convert this field again to varchar.
I do cut off the '|' to convert the field to number, without blanks or other characters, everything fine.
to_number(translate(PersonNumb,'|',''),999999999999999999999999999999) AS NewPersonNumb;
Than i do the arithmetic with
update XX..YY set NewPersonNumb = NewPersonNumb + 10;
But the last step wont work, the field is still numeric and not varchar.
update XX..YY set NewPersonNumb = to_char(NewPersonNumb,'999999999999999999999999999999');
Put the whole statement in one row doesnt work too...
update XX..YY set NewPersonNumb = to_char(NewPersonNumb + 10,'99999999999999999999999999');
I have done the following its working for me .
TEST.ADMIN(ADMIN)=> create table YY (PersonNumb varchar(30)) distribute on random;
CREATE TABLE
TEST.ADMIN(ADMIN)=> insert into YY values('22222222|111111|4');
INSERT 0 1
TEST.ADMIN(ADMIN)=> update yy set PersonNumb = to_number(translate(PersonNumb,'|',''),999999999999999999999999999999)+10 ;
UPDATE 1
TEST.ADMIN(ADMIN)=> select * from YY;
PERSONNUMB
-----------------
222222221111124
Perhaps someone will need it
Put everything in the select
(to_number(translate(PersonNumb,'|',''),99999999999999999999999999999) + 10)::varchar(30) AS NewPersonNumb

SQL Server: Update a Column, concatenate char with that column value

I have a table that contains a column that I need to update if the length of that value is equal to 1, the thing is that if this is true I need to get that value and concatenate a 0 before it, for example:
If the value of the column is equal to "5" I need to update that row to "05", I need to do this for all the rows that match this criteria.
I tried this:
UPDATE WS
SET WS.used_brand=CONCAT('0',(Select WS.used_brand FROM WS)) WHERE LEN(WS.used_brand) = 1;
It doesn't work because of the inner select, how can I fix this?.
Thanks.
I think this does what you want:
UPDATE WS
SET WS.used_brand = CONCAT('0', WS.used_brand)
WHERE LEN(WS.used_brand) = 1;
Note: Many databases support LPAD() or a similar function for padding values on the left.
In SQL Server, you would more likely write this as:
UPDATE WS
SET WS.used_brand = '0' + WS.used_brand
WHERE LEN(WS.used_brand) = 1;
You don't need a nested SELECT statement - you can reference the column just like this:
UPDATE WS
SET WS.used_brand=CONCAT('0', WS.used_brand)
WHERE LEN(WS.used_brand) = 1;

copy part of string from one column into another

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