I have a column with data as abc.123, def.345 and so on. It is basically a name followed by a . and then a code. I am able to divide them using
select
LEFT(Campaign, ISNULL(NULLIF(CHARINDEX('.', Campaign + ' ') -1, -1), LEN(Campaign))),
STUFF(Campaign, 1, Len(Campaign) +1- CHARINDEX('.',Reverse(Campaign)), '')
from myTable;
Input set:
| myColumn |
| abc.123 |
| def.345 |
| 444 |
However, for data like '444' it shows:
|Name| Code |
|abc | 123 |
|def | 345 |
|444 | | (SHOULD BE => | | 444 |)
Assumptions:
Data can be without a '.' In such case we can insert the data in either Name or Code depending on datatype ie Name=>Alphanumeric, Code=>Numeric.
eg: Data like
999 => | | 999 |
a24.345 => | a24 | 345 |
a72 => | a72 | |
Use CASE expressions together with the LIKE operand in order to differentiate the cases:
SELECT
CASE
WHEN Campaign LIKE '[a-z]%' THEN LEFT(Campaign, CHARINDEX('.', Campaign + '.') - 1)
ELSE null
END AS Name,
CASE
WHEN Campaign LIKE '[0-9]%' THEN Campaign
WHEN Campaign LIKE '%.[0-9]%' THEN
RIGHT(Campaign, LEN(Campaign) - CHARINDEX('.', Campaign))
ELSE null
END AS Code
FROM myTable
You can see an example here: http://sqlfiddle.com/#!3/ae7ef7/1
Use a case statement:
select (case when myColumn like '%.%'
then <your code here>
else myColumn
end) as code
Related
I want to remove the ".com" suffix from the domain name field which is an array of strings separated by a pipe(|)
The data looks like below-
ID domain
1 ab23c45 | xyz167a.com | d1ef76om.com |rx08bj23
2 omg23hy5 | xyz167a | ab23c45.com | jhy2ft3.com
The result should look like-
ID domain
1 ab23c45 | xyz167a | d1ef76om |rx08bj23
2 omg23hy5 | xyz167a | ab23c45 | jhy2ft3
Below is for BigQuery Standard SQL
#standardSQL
SELECT id,
(
SELECT STRING_AGG(TRIM(REPLACE(TRIM(domain), IFNULL(NET.PUBLIC_SUFFIX(TRIM(domain)), ''), ''), '.'), ' | ')
FROM UNNEST(SPLIT(domain, '|')) domain
) domain
FROM `project.dataset.table`
if to apply to sample data from your question - output is
Row id domain
1 1 ab23c45 | xyz167a | d1ef76om | rx08bj23
2 2 omg23hy5 | xyz167a | ab23c45 | jhy2ft3
Note: above code handles any suffix - not just '.com'
I need help wit removing "'pp'" from search results which appear at the biginning of text. Values in search resuls contain spaces and also '. I need to remove only 'pp from bigginig
This sounds like:
select regexp_replace(col, '^pp', '')
Or a case expression:
select (case when col like 'pp%' then substr(col, 3) else col end)
You don't need regular expressions and can use simple string functions.
If you want to use SELECT then:
SELECT value,
CASE
WHEN value LIKE 'pp%'
THEN SUBSTR( value, 3 )
ELSE value
END AS replaced_value
FROM table_name
Outputs:
VALUE | REPLACED_VALUE
:---- | :-------------
pp123 | 123
pp1pp | 1pp
123pp | 123pp
12345 | 12345
and, if you want to UPDATE the table:
UPDATE table_name
SET value = SUBSTR( value, 3 )
WHERE value LIKE 'pp%';
Then:
SELECT * FROM table_name;
Outputs:
| VALUE |
| :---- |
| 123 |
| 1pp |
| 123pp |
| 12345 |
db<>fiddle here
I have table like this
-----------------------------------
| id | col_name | colname_suf |
-----------------------------------
| 1 | textSuff_ix| null |
| 2 | strSuff_ix2| null |
| ... |
-----------------------------------
The main idea of my solution is to move suffix to another column. But suffix and text before can be in known range(text1, text2, str, Suff_ix1, Suff_ix2, Suff_ix3...). How i can fix it using sql query?
Use string functions like left(), right() and charindex() to split the column and update:
update tablename
set [col_name] = left([col_name], charindex('_', [col_name]) - 1),
[colname_suf] = right([col_name], len([col_name]) - charindex('_', [col_name]))
where [col_name] like '%[_]%';
See the demo.
Results:
> id | col_name | colname_suf
> -: | :------- | :----------
> 1 | textSuff | ix
> 2 | strSuff | ix2
You can use string operations:
select left(col_name, charindex('_', col_name) - 1) as prefix,
stuff(col_name, 1, charindex('_', col_name), '') as suffix
I want to remove all of the following characters from a field: * - : .
Currently I can do this by chaining TRANSLATE statements together, but I hope there's an easier way.
INPUT
SELECT field FROM myTable
| field |
=========
| asdf* |
| as.df |
| a-sdf |
| :asdf |
DESIRED OUTPUT
| field |
=========
| asdf |
| asdf |
| asdf |
| asdf |
MY CODE
SELECT TRANSLATE(TRANSLATE(TRANSLATE(TRANSLATE(field, '*', ''), '-', ''), ':', ''), '.', '')
translate() takes longer strings:
select translate(field, '*-:.', '')
You might be confusing it with replace(), where you would need to nest the functional calls.
I am trying to left pad with a single zero after the '-'.
I did check the other answers here but didnt help me.
Here is the table :
+---------+
| Job |
+---------+
| 3254-1 |
| 3254-25 |
| 3254-6 |
+---------+
I need to left pad with single zero after '-' if the value is between 1 and 9 in the end
I want the results to be :
+---------+
| Job |
+---------+
| 3254-01 |
| 3254-25 |
| 3254-06 |
+---------+
You can use CHARINDEX(), SUBSTRING() and REPLACE() as:
CREATE TABLE Jobs(
Job VARCHAR(45)
);
INSERT INTO Jobs VALUES
('3254-1'),
('3254-25'),
('3254-6');
SELECT CASE
WHEN CHARINDEX('-', Job, 1)+1 < LEN(Job) THEN Job
ELSE
REPLACE(Job, '-', '-0')
END AS Job
FROM Jobs;
Results:
+----+---------+
| | Job |
+----+---------+
| 1 | 3254-01 |
| 2 | 3254-25 |
| 3 | 3254-06 |
+----+---------+
If you want an update, I think this is the simplest method:
update t
set job = replace(job, '-', '-0')
where job like '%-_';
This problem is simplified greatly because you are only adding a single padding character.
If you have version 2012+, then format function may be used as :
select concat(nr1, '-', format( cast ( q2.nr2 as int ), '00')) as result
from
(
select substring(q1.str,1,charindex('-',q1.str,1)-1) as nr1,
substring(q1.str,charindex('-',q1.str,1)+1,len(q1.str)) as nr2
from
(
select '3254-1' as str union all
select '3254-25' as str union all
select '3254-6' as str
) q1
) q2;
result
------
3254-01
3254-25
3254-06
Rextester Demo