Replace Function for specific values - sql

I have a table like this:
Article
Date
Status
Code
In the column "Status" are values like '00', '01', 'OB'
In the column "Code" are values like:
[05]+000569
[1B] 5555
690KB
-859
I am just interested for rows where "Status" = '00'.
The status '00' shows just values like [05]+000569.
These values should be trimmed with the following function:
select replace(regexp_substr(Code, '(^|[+])[0-9]+'), '+', '')
How can I build a SELECT SQL with this function just for rows where "Status" = '00' ?

I see. You want a case expression:
select (case when status = '00' then replace(regexp_substr(Code, '(^|[+])[0-9]+'), '+', '')
end)

"Trim" for status = '00', don't for other statuses:
SELECT CASE
WHEN status = '00' --> trim
THEN --> for
REPLACE (REGEXP_SUBSTR (Code, '(^|[+])[0-9]+'), '+', '') --> '00'
ELSE
code --> else, do nothing
END
AS code
FROM your_table

Related

How to find values that contains only 0's and any other digit for example 000000001 or 0000010001 or 010101 or 0002 or 02020 or 0090 etc.?

I want to find 'default type values' in SQL that is entered when something like an ID number of company registration number is entered. Some of the values I see is a combination of 0's and another digit from 1-9. Examples I have seen is 00000001, 0000100, 000000002, 000001111, 0000090009, etc. The values vary in length also. Is there a way to find these values without hard coding? The value should contain at least one 0 and one or more of any other digit.
You want all strings that consist of only zero and one other digit. I.e. you want to find '0101', but not '0102'.
In order to do this, remove all zeros first. From the remaining string remove all digits equaling to its first character. This will result in an empty string or a string consisting of additional digits or characters. Only select those resulting in an empty string.
select *
from mytable
where replace(replace(value, '0', ''), substring(replace(value, '0', ''), 1, 1), '') = '';
Demo: https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=c307bbbf21ceeae619a966e995c3a567
You can use ISNUMERIC() function
SELECT ISNUMERIC(0000004);
This will return 1
SELECT ISNUMERIC('A');
This will return 0
So if you want to select all columns that are numeric only you can use this logic:
select *
from test
where ISNUMERIC(colA) = 1
Here is a small demo
Or you can use TRY_CAST() function:
select *
from test
where try_cast(colA as int) is not null
Alternative solution:
SELECT value
FROM mytable
CROSS JOIN (
SELECT '1' AS num
UNION ALL
SELECT '2'
UNION ALL
SELECT '3'
UNION ALL
SELECT '4'
UNION ALL
SELECT '5'
UNION ALL
SELECT '6'
UNION ALL
SELECT '7'
UNION ALL
SELECT '8'
UNION ALL
SELECT '9'
) n
WHERE REPLACE(REPLACE(value, '0', ''), num, '') = ''
AND REPLACE(value, '0', '') <> ''
AND value LIKE '%0%'

LIKE statement to compare strings with hyphen

I am working in SQL and I have 3 columns Current Name, Given Full Name and Whether the names match (Y or No)
The problem with that is that when I am comparing the strings in the first 2 columns, it is not showing me the current result. For example, I am not finding a way to prove that 'Tushar Sharma' is same as 'Tushar-Sharma' considering that Tushar Sharma is the current full name and Tushar-Sharma is the name that has been extracted from a report.
I am stuck at the LIKE statement as to what to do if I want to have hyphen(-) included in the comparison so that I get a Y in the 3rd column.
Thank you
One option is to remove the hyphen for the comparison:
select (case when replace(given_name, '-', '') = replace(full_name, '-', '') then 'Y' else 'N' end) as names_match
You can use replace() with like as well:
select (case when replace(given_name, '-', '') like '%' + replace(full_name, '-', '') '%' then 'Y' else 'N' end) as names_match
Replace - with whitespace and compare, you can also use regex or fuzzy matching to improve the match for other conditions.
AND REPLACE(CurrentName, '-', ' ') = REPLACE(GivenName, '-', ' ');
Ex:
AND REPLACE('Tushar Sharma', '-', ' ') = REPLACE('Tushar-Sharma', '-', ' ')
will eval to
AND 'Tushar Sharma' = 'Tushar Sharma'
this will work:
select currentname,givenfullname,case when regexp_replace(currentname,' ','') like
regexp_replace(givenfullname,' ','') the 'Y' else 'N' end as matchstatus from
table_name;

Add some numbers in front of a column

I have a column in SQL server and I want to add 7 for the words which begin by a and add 8 for the words which begin by t.
Here is my column:
add-on
above
tv
their
french
And I want this:
7add-on
7above
8tv
8their
french
I am looking for a query which could do that.
Thank you very much!
You can use left() & do the concatenation :
select t.*,
(case left(col, 1)
when 'a' then concat(7, col)
when 't' then concat(8, col)
else col
end)
from table t;
Use a case expression with substring to get the results you require.
select concat(
(case when substring(col,1,1)='a' then '7'
when substring(col,1,1)='t' then '8'
end
)
,col
) as modified_col
from table
You need 2 update queries. Try like:
UPDATE myTable SET myColumn='7'+myColumn WHERE myColumn LIKE 'a%';
UPDATE myTable SET myColumn='8'+myColumn WHERE myColumn LIKE 't%'
Use CASE statement to check the first char, returned by SUBSTRING function, and calculate a prefix to add to your data:
select
case substring(column, 1, 1)
when 'a' then '7'
when 't' then '8' else '' end + column as NewColumn
from table
In sql server 2008 :
select
case when substring(column,1,1)='a' then ( '7 ' + column)
case when substring(column,1,1)='t' then ( '8 ' + column)
else column end as column from table
In sql server 2012 or above :
select
case when substring(column,1,1)='a' then concat( '7 ',column)
case when substring(column,1,1)='t' then concat( '8 ',column)
else column end as column from table

Select Statement that returns a result where the text characters contains only 0s or only 9s but length doesn't matter

I'm trying to write a select statement that returns column A when column B contains '0' or '9' or '00' or '99' or '000' or '999' and so forth. The values are TEXT. Any help is appreciated!
You could use:
SELECT colA
FROM tab
WHERE REPLACE(colB, '9', '') = ''
OR REPLACE(colB, '0', '') = '';
DBFiddle Demo
SELECT *
FROM tab
WHERE col like '%9%' or col like '%0%'

using decode with input as comma separated list

This could be simple question for experts.
Here is my requirement. I have to use DECODE and match the string containing comma separated values.
Here is sample SQL: 'A' is column value from table, 'A,B'C' is comma separated string passed as expression to sql
SELECT DECODE('A' , 'A,B,C', 'true', 'false') FROM DUAL;
The above SQL should return true as csv expression contains 'A'.
Please help me writing this SQL
If I understand your question, you are trying to determine if a string is within another string?
select case when instr( 'A,B,C', 'A' ) > 0
then 'True'
else 'False'
end
from dual
Add the DECODE version
select decode( instr( 'A,B,C', 'A' ), 0, 'False', 'True' )
from dual
First, don't use decode(). Use the ANSI standard CASE statement. The answer to your question is that you can use LIKE:
SELECT (CASE WHEN ',' || 'A,B,C' || ',' LIKE '%,' || 'A' || ',%'
THEN 'true' ELSE 'false'
END)
FROM DUAL;
Note: The string concatenation is just because I suspect the values are variables. The simpler form is:
SELECT (CASE WHEN ',A,B,C,' LIKE '%,A,%'
THEN 'true' ELSE 'false'
END)
FROM DUAL;
Using DECODE, you can do it like this:
SELECT DECODE(INSTR('A,B,C', 'A'), 0, 'false', 'true') FROM DUAL;
where
INSTR('Comma separated string', 'column value to match') will return 0 if the column value is not in the comma separated string. In your example,
INSTR('A,B,C', 'A') will return a value greater than 0 and INSTR('D,B,C', 'A') will return 0. So then DECODE can check if the return value of INSTR() is 0 then return false otherwise return true.