in stored procedure, i have this field
LTRIM(ISNULL(O.Column1, ''))
If there is a dash(-) symbol at end of the value, want to remove it. only in conditions if a dash symbol exist at start/end.
Any suggestions
EDIT:
Microsoft SQL Server 2014 12.0.5546.0
Expected output:
1)input: "abc-abc" //output: "abc-abc"
2)input: "abc-" //output: "abc"
3)input: "abc" //ouput: "abc"
I think you might be stuck with string manipulation here.
The CASE expression here takes the LTRIM/RTRIM result from your column and checks both ends for a dash, and then each end for a dash. If dashes exist, it strips them out. It's not pretty, and won't perform well on a mountain of data, but will do what you need.
Data setup:
create table trim (col1 varchar(10));
insert trim (col1)
values
('abc'),
(' abc-'),
('abc- '),
('abc-abc '),
(' -abc'),
('-abc '),
(NULL),
(''),
(' -abc- ');
The query:
select
case
when right(ltrim(rtrim(isnull(col1,''))),1) = '-'
and left(ltrim(rtrim(isnull(col1,''))),1) = '-'
then substring(ltrim(rtrim(isnull(col1,''))),2,len(ltrim(rtrim(isnull(col1,''))))-2)
when right(ltrim(rtrim(isnull(col1,''))),1) = '-'
then left(ltrim(rtrim(isnull(col1,''))), len(ltrim(rtrim(isnull(col1,''))))-1)
when left(ltrim(rtrim(isnull(col1,''))),1) = '-'
then right(ltrim(rtrim(isnull(col1,''))), len(ltrim(rtrim(isnull(col1,''))))-1)
else ltrim(rtrim(isnull(col1,'')))
end as trimmed
from trim;
Results:
+---------+
| trimmed |
+---------+
| abc |
| abc |
| abc |
| abc-abc |
| abc |
| abc |
| |
| |
| abc |
+---------+
SQL Fiddle Demo
Since the Database is not mentioned, here is how you do it (rather find it)
SQL Server
Remove the last character in a string in T-SQL?
Oracle
Remove last character from string in sql plus
Postgresql
Postgresql: Remove last char in text-field if the column ends with minus sign
MySQL
Strip last two characters of a column in MySQL
You can use LEFT function, along with SUBSTRING to achieve the result.
SELECT CASE WHEN RIGHT(stringVal,1)= '-' THEN SUBSTRING(stringVal,1,LEN(stringVal)-1)
ELSE stringVal END AS ModifiedString
from
( VALUES ('abc-abc'), ('abc-'),('abc')) as t(stringVal)
+----------------+
| ModifiedString |
+----------------+
| abc-abc |
| abc |
| abc |
+----------------+
Related
My column "ColumnOne" in my table "MyTable" has values like this: Delimiter is character '-'
|Something |
|Something - SomeOtherThing |
|Something - SomethingElse |
|Something - Whatever |
|OtherThing - |
I want to update the values so eventually it look like this:
|Something |
| SomeOtherThing |
| SomethingElse |
| Whatever |
| |
So basically algorithm being to replace with white space and keep going until you see '-' , replace that too also with whitespace.
I tried the REPLACE command to say like
UPDATE MyTable SET ColumnOne = REPLACE(ColumnOne, ' - ', ' ' + ColumnOne) but that's wrong. I couldn't figure out the pattern for its second argument.
Any suggestions are appreciated.
Use charindex to find the amount of characters to change, stuff to perform the change, and replicate to generate a string of N spaces. Try this:
stuff(ColumnOne,1,charindex('-',ColumnOne),replicate(' ',charindex('-',ColumnOne))
I am using SQL Server 2016 and I am trying to extract the first set of numbers of a certain string. Here are some examples
12345
123456
12345-ks
12345-12
123456-ks
I want:
12345
123456
12345
12345
123456
I have tried SUBSTRING(#str, 0, charindex('-', str#, 0). But that excludes any strings without '-'
I have also tried using a case statement to include those strings. But, I can't group by a case statement. Any thoughts?
I've used a case statement to return the whole value when the column does not constain -.
create table strings ( s varchar(10));
insert into strings values
('12345'),
('123456'),
('12345-ks'),
('12345-12'),
('123456-ks');
select
case when charindex('-',s,0) = 0
then s
else SUBSTRING(s,0,
charindex('-', s, 0)
)
end as first_group
from strings;
GO
ci | first_group
-: | :----------
0 | 12345
0 | 123456
6 | 12345
6 | 12345
7 | 123456
db<>fiddle here
In Oracle database I have such table.
| TREE | ORG_NAME |
|---------------------------------|----------|
| \Google earth\Nest global\ATAP | ATAP |
| \Google earth\Nest\Beemoney\ | Beemoney |
| \Google\\\BeeKey\ | |
| | York |
I am trying to make sql query which would return such result.
| ORGANIZATION |
|-----------------------------------|
| Google earth > Nest global > ATAP |
| Google earth > Nest Beemoney |
| Google > BeeKey |
| York |
As you can see I want:
1) Replace \ symbol at the beginning and end of the sentence.
2) Replace \ symbol which is inside sentence to > symbol.
3) Replace \\\ symbol which is inside sentence to > symbol.
4) If TREE colomn is empty take record from ORG_NAME colomn.
Here is how I started. This SQL query solve 2, 3 and 4 part. How to solve problem with 1 part. I think I need to use REGEXP_REPLACE, right? How to make it correctly? Is there any other more elegant way to redisign sql query? As you can see I walk on the same table a few times.
SELECT
COALESCE (TREE, ORG_NAME) as ORGANIZATION
FROM (
SELECT
REPLACE(TREE, '\', '>') AS TREE,
ORG_NAME
FROM (
SELECT
REPLACE(TREE, '\\\', '>') AS TREE,
ORG_NAME
FROM
ORG
)
)
This could be a way with a regexp_replace and a trim to remove the characters from the beginning and the end of the string:
select nvl(regexp_replace( trim('\' from tree), '\\+', ' > '), org_name)
from yourTable
Here is a working solution which uses two calls to regexp_replace:
select
regexp_replace(
regexp_replace('\Google\\\BeeKey\', '^\\?(.*?)\\?$', '\1'), '\\+', ' > ')
from dual;
Google > BeeKey
Demo
The inner call to regexp_replace strips off any possible leading or trailing path separators. The outer call converts any number of internal path separators / to > separators as a replacement.
This question already has answers here:
How do you strip a character out of a column in SQL Server?
(4 answers)
Closed 4 years ago.
I have a table [ABCTable]
When I query with
SELECT [XYZ] from [ABCTable]
there is a possibility that [XYZ] set of rows returned might contain - [~], [!], [#], [#], [$], [%], [^], [&], [*], [,], [.] , [?].
Is there a way to write just a SQL Query (not stored procedure or sub routines) to ensure these characters are removed while selecting the needed data ?
Do you want something like
CREATE TABLE T(
ID INT IDENTITY(1,1),
Value VARCHAR(45)
);
INSERT INTO T(Value) VALUES
('.A*B$C#'),
('D#E$,F'),
('.G,H*I#$');
DECLARE #Chars VARCHAR(45) = '#$.,*#';
SELECT *, REPLACE(TRANSLATE(Value, #Chars, REPLICATE(' ', LEN(#Chars))), ' ', '') Result
FROM T;
Returns:
+----+----------+--------+
| ID | Value | Result |
+----+----------+--------+
| 1 | .A*B$C# | ABC |
| 2 | D#E$,F | DEF |
| 3 | .G,H*I#$ | GHI |
+----+----------+--------+
Demo
Note: If you have WhiteSpaces there I suggest that you use CHAR(9) instead as
REPLACE(TRANSLATE(Value, #Chars, REPLICATE(CHAR(9), LEN(#Chars))), CHAR(9), '')
I am trying to replace the first occurrence of '-' in a string in Hive table. I am using HiveQL. I searched this topic here and other websites, but could not find clear explanation how to use metacharacters with regexp_replace() to do that.
This is a string from which I need to replace first '-' with empty space: 16-001-02707
The result should be like this: 16001-02707
This is the method I used:
select regexp_replace ('16-001-02707','[^[:digit:]]', '');
However, this doesn't do anything.
select regexp_replace ('16-001-02707','^(.*?)-', '$1');
16001-02707
Following the OP question in the comments
with t as (select '111-22-333333-4-555-6-7-8888-999999' as col)
select regexp_replace (col,'^(.*?)-','$1')
,regexp_replace (col,'^(.*?-.*?)-','$1')
,regexp_replace (col,'^((.*?-){2}.*?)-','$1')
,regexp_replace (col,'^((.*?-){3}.*?)-','$1')
,regexp_replace (col,'^((.*?-){4}.*?)-','$1')
,regexp_replace (col,'^((.*?-){5}.*?)-','$1')
from t
+------------------------------------+------------------------------------+------------------------------------+------------------------------------+------------------------------------+------------------------------------+
| _c0 | _c1 | _c2 | _c3 | _c4 | _c5 |
+------------------------------------+------------------------------------+------------------------------------+------------------------------------+------------------------------------+------------------------------------+
| 11122-333333-4-555-6-7-8888-999999 | 111-22333333-4-555-6-7-8888-999999 | 111-22-3333334-555-6-7-8888-999999 | 111-22-333333-4555-6-7-8888-999999 | 111-22-333333-4-5556-7-8888-999999 | 111-22-333333-4-555-67-8888-999999 |
+------------------------------------+------------------------------------+------------------------------------+------------------------------------+------------------------------------+------------------------------------+