Update/Replace with a substring - triple backslashes - sql

I have a column full of duplicate filepaths:
\\C:\298788\DOC1\SUB1\\\C:\298788\DOC1\SUB1\FILE.txt
\\C:\298788\DOC1\SUB1\\\C:\298788\DOC1\SUB1\FILE.txt
\\C:\298788\DOC1\SUB1\\\C:\298788\DOC1\SUB1\FILE.txt
I need only the second part of the string ie. C:\298788\DOC1\SUB1\FILE.txt
How do I replace up to the triple backslashes with nothing. I have tried:
UPDATE [TABLE].[dbo].[ColumnName]
SET [ColumnName] = REPLACE([ColumnName], '%\\\', '');
It says all rows updated however nothing has changed. Assuming its something to do with the backslashes.
Using SQL SERVER 2012.

Using stuff()
select col = stuff(col,1,charindex('\\\',col,2)+2,'')
from tbl
rextester demo: http://rextester.com/QRKWP8606
returns:
+------------------------------+
| col |
+------------------------------+
| C:\298788\DOC1\SUB1\FILE.txt |
| C:\298788\DOC1\SUB1\FILE.txt |
| C:\298788\DOC1\SUB1\FILE.txt |
+------------------------------+
as an update:
update tbl
set col = stuff(col,1,charindex('\\\',col,2)+2,'')
where charindex('\\\',col,2)>0

Related

How to replace rows containing alphabets and special characters with Blank spaces in snowflake

I have a column "A" which contains numbers for example- 0001, 0002, 0003
the same column "A" also contains some alphabets and special characters in some of the rows for example - connn, cco*jjj, hhhhhh11111 etc.
I want to replace these alphabets and special characters rows with blank values and only want to keep the rows containing the number.
which regex expression I can use here?
If you want to extract numbers from these values (even if they end or start with non digits), you may use something like this:
create table testing ( A varchar ) as select *
from values ('123'),('aaa123'),('3343'),('aaaa');
select REGEXP_SUBSTR( A, '\\D*(\\d+)\\D*', 1, 1, 'e', 1 ) res
from testing;
+------+
| RES |
+------+
| 123 |
| 123 |
| 3343 |
| NULL |
+------+
I understand that you want to set to null all values that do not contain digits only.
If so, you can use try_to_decimal():
update mytable
set a = null
where a try_to_decimal(a) is null
Or a regexp match:
update mytable
set a = null
where a rlike '[^0-9]'

SQL: Select rows that contain a word

The goal is to select all rows that contain some specific word, can be in the beginning or the end of the string and/or surrounded by white-space, should not be inside other word, so to speak.
Here are couple rows in my database:
+---+--------------------+
| 1 | string with test |
+---+--------------------+
| 2 | test string |
+---+--------------------+
| 3 | testing stringtest |
+---+--------------------+
| 4 | not-a-test |
+---+--------------------+
| 5 | test |
+---+--------------------+
So in this example, selecting word test, should return rows 1, 2 and 5.
Problem is that for some reason, SELECT * FROM ... WHERE ... RLIKE '(\s|^)test(\s|$)'; returns 0 rows.
Where am I wrong and maybe, how it could be done better?
Edit: Query should also select the row with just a word test.
The answer to my first question is:
I haven't escaped special characters, so \s should be \\s.
Working query: SELECT * FROM ... WHERE ... RLIKE '(\\s|^)test(\\s|$)';. (or just a space ( |^)/( |$), also works)
Hi you could grab with trailing space and with leading space
SELECT * from new_table
where text RLIKE(' test')
union
SELECT * from new_table
where text RLIKE('test ')
REGEXP_INSTR() function, which's is an extension of the INSTR() function, might be used for version 10.0.5+ case-insensitively as default :
SELECT *
FROM t
WHERE REGEXP_INSTR(str, 'TeSt ')>0
OR REGEXP_INSTR(str, ' tESt')>0
Demo
SELECT * FROM ...
WHERE ... LIKE 'test';
This should do the trick.
Is this what you want?
SELECT * FROM ... WHERE ... LIKE
'%test%';
Use word boundary tests:
Before MySQL 8.0, and in MariaDB:
WHERE ... REGEXP '[[:<:]]test[[:>:]]'
MySQL 8.0:
WHERE ... REGEXP '\btest\b'
(If that does not work, double up the backslashes; this depends on whether the client is collapsing backslashes before MySQL gets them.)
Note that this solution will also work with punctuation such as the comma in "foo, test, bar"

add leading zeros to a varchar column

Select len(productid),productid,*
FROM dbo.produts
where Place = 'KA'
and len(productid) <> 10
order by len(productid)
this query filters the data that needs to be updated
-- i need to update the data in 'productid' that is not in the correct format
--(the data should be 10 characters, in 4-2-4 format with leading 0s where they are needed) (productid column is a varchar (256) column)
basically i need to add leading zeros to a varchar column that has a where condition
|productid| |Correct productid value| |
---------------------------------------
|234-55-43||000234-55-43|
|76-7-89||0000076-7-89|
what are the possible solutions for updating these records?
This is very simple, actually:
SELECT productid,
RIGHT('000000000'+productid,10) CorrectProductid
FROM dbo.YourTable
;
If you wanted the corrected format to be in the 4-2-4 format you mentioned:
Using parsename() to parse out the pieces of the string, and right() to add the extra '0's.
select
col
, Corrected = right('0000'+parsename(replace(col,'-','.'),3),4)
+ '-' + right('00'+parsename(replace(col,'-','.'),2),2)
+ '-' + right('0000'+parsename(replace(col,'-','.'),1),4)
from t
where col not like '____-__-____'
rextester demo: http://rextester.com/OXM28014
returns:
+-----------+--------------+
| col | Corrected |
+-----------+--------------+
| 234-55-43 | 0234-55-0043 |
| 76-7-89 | 0076-07-0089 |
+-----------+--------------+
As an update:
update t
set col = right('0000'+parsename(replace(col,'-','.'),3),4)
+ '-' + right('00'+parsename(replace(col,'-','.'),2),2)
+ '-' + right('0000'+parsename(replace(col,'-','.'),1),4)
where col not like '____-__-____'

How to remove duplicates within a string from the table in SQL server 2016

I got a table which contains a column of strings. Those strings are separated by a ;. Now I want to remove the duplicates after the string is splitted. For example:
-----------
| w;w;e;e |
-----------
| q;r;r;q |
-----------
| b;n;n;b |
-----------
The result should be:
-------
| w;e |
-------
| q;r |
-------
| b;n |
-------
Also it should not be a Select function but a (not 100% sure) delete function. So the values in the original table won't be duplicated anymore.
For an update statement, this will de-duplicate your column:
update t
set col = stuff((
select distinct
';'+s.Value
from string_split(t.col,';') as s
for xml path (''), type).value('.','varchar(1024)')
,1,1,'');
In sql server 2016, you can use string_split() along with the stuff() with select ... for xml path ('') method of string concatenation to concatenate only distinct values.
select
t.id
, t.col
, dedup = stuff((
select distinct
';'+s.Value
from string_split(t.col,';') as s
for xml path (''), type).value('.','varchar(1024)')
,1,1,'')
from t
dbfiddle demo: here
rextester demo: http://rextester.com/MAME55141; this demo uses a CSV Splitter function by Jeff Moden in the absence of string_split().
returns:
+----+---------+-------+
| id | col | dedup |
+----+---------+-------+
| 1 | w;w;e;e | e;w |
| 2 | q;r;r;q | q;r |
| 3 | b;n;n;b | b;n |
+----+---------+-------+
splitting strings reference:
Tally OH! An Improved SQL 8K “CSV Splitter” Function - Jeff Moden
Splitting Strings : A Follow-Up - Aaron Bertrand
Split strings the right way – or the next best way - Aaron Bertrand
string_split() in SQL Server 2016 : Follow-Up #1 - Aaron Bertrand
If "e", "r", and "w" are the only values in the string, then the simplest way is to reconstruct the string:
select stuff( (case when string like '%e%' then ';e' else '' end) +
(case when string like '%r%' then ';r' else '' end) +
(case when string like '%w%' then ';w' else '' end),
1, 1, ''
)
I suspect the values might be limited, because these look like file permissions (read/write/execute). Otherwise, you will need to parse the string into separate rows (using XML, a UDF, or a recursive CTE) and recombine the values.
You should learn a lesson here. Don't store lists in strings. These values should either be flags (if I am right about there only being a handful of values). Or, they should be on separate rows of a another table.

SQL Regex to select string between second and third forward slash

I am using Postgres/Redshift to query a table of URLs and am trying to use
SELECT regex_substr to select a string that is between the second and third forward slash in the column.
For example I need the second slash delimited string in the following data:
/abc/required_string/5856365/
/abc/required_string/2/
/abc/required_string/l-en/
/abc/required_string/l-en/
Following some of the regexs in this this thread:
SELECT regexp_substr(column, '/[^/]*/([^/]*)/')
FROM table
None seem to work. I keep getting:
/abc/required_string/
/abc/required_string/
What about split_part?
SELECT split_part(column, '/', 3) FROM table
Example:
select split_part ('/abc/required_string/2/', '/', 3)
Returns: required string
This may work :
SQL Fiddle
PostgreSQL 9.3 Schema Setup:
CREATE TABLE t
("c" varchar(29))
;
INSERT INTO t
("c")
VALUES
('/abc/required_string/5856365/'),
('/abc/required_string/2/'),
('/abc/required_string/l-en/'),
('/abc/required_string/l-en/')
;
Query 1:
SELECT substring("c" from '/[^/]*/([^/]*)/')
FROM t
Results:
| substring |
|-----------------|
| required_string |
| required_string |
| required_string |
| required_string |