I have row,
example : 1,2,3,5,9,7 -> not in (3,7)
(This character need to delete -> result select 1,2,5,9.
How do it ?
For example :
drop table test.table_4;
create table test.table_4 (
id integer,
list_id text
);
insert into test.table_4 values(1,'1,2,3,5,9,7');
insert into test.table_4 values(2,'1,2,3,5');
insert into test.table_4 values(3,'7,9');
insert into test.table_4 values(5,'1,2');
insert into test.table_4 values(9,'1');
insert into test.table_4 values(7,'5,7,9');
query :
select list_id from test.table_4 where id not in (3,7) --return 4 row
id list_id
1. 1 '1,2,3,5,9,7'
2. 2 '1,2,3,5'
3. 5 '1,2'
4. 9 '1'
How to remove 3 and 7 in row 1 and 2 ?
id
1. 1 '1,2,5,9'
2. 2 '1,2,5'
3. 5 '1,2'
4. 9 '1'
The following should deal with 3 or 7 at the start of the string, at the end of the string, or anywhere in the middle. It also ensures that the 3 in 31 and the 7 in 17 don't get replaced:
select
list_id,
regexp_replace(list_id, '(^[37],|,[37](,)|,[37]$)', '\2', 'g')
from test.table_4
where id not in (3,7)
Explanation:
^[37], matches a 3 or 7 followed by a comma at the start of the string. This should be replaced with nothing.
,[37](,) matches a ,3, or ,7, in the middle of the string. This needs to be replaced with a single comma, which is captured by the brackets around it.
[37]$ matches a 3 or 7 preceded by a comma at the end of the string. This should be replaced with nothing.
\2 is used to replace the string - this is , for the second case above, and empty for cases 1 and 3.
You could use the following statements to update all of the records. In the below example the first statement will remove any ,7 found. Then you execute the next statement to find any sting that has the 7 in the front of the string.
UPDATE test.table_4 SET list_id = REPLACE(list_id, ',7', '')
UPDATE test.table_4 SET list_id = REPLACE(list_id, '7', '')
If you also want to remove all occurrences of 3 then execute the following statements:
UPDATE test.table_4 SET list_id = REPLACE(list_id, ',3', '')
UPDATE test.table_4 SET list_id = REPLACE(list_id, '3', '')
However, it is a bad design to store values that you need to search agianst, work with, and etc in a string.
You can use regexp_replace to get the expected output:
select id, regexp_replace(list_id,'3,|,7', '','g')
from table_4
where id not in (3,7)
Output:
id regexp_replace
1 1,2,5,9
2 1,2,5
5 1,2
9 1
Here is the SQL Fiddle
Related
I want to know if the 4th integer in the ID, is even, or if its odd.
If the 4th number is even (if the number is either 0,2,4,6,8 I want to put the ID into a new column named 'even'
IF the 4th number is odd, the column should have the name 'Odd'
select ID as 'Female'
from Users2
where ID LIKE '%[02468]'
This shows if any of the numbers are even. I want to specify the 4th number
Try this:
select *, OddOrEven = iif(substring(ID,4,1) in ('0','2','4','6','8') , 'Even', 'Odd') from Users2
This will tell you whether the 4th character is Odd or Even.
This is of course assuming that the 4th character of ID column will be numeric.
To make it permanently part of the table, you can add a computed column as shown below.
alter table Users2
add OddOrEven as iif(substring(ID,4,1) in ('0','2','4','6','8'), 'Even', 'Odd')
Substring the character you are interested in
Convert to an int
Check whether modulus 2 returns 0 (i.e. even).
select id
, case when convert(int,substring(id, 4, 1)) % 2 = 0 then 'Even' else 'Odd' end
from Users;
Example:
select id
, case when convert(int,substring(id, 4, 1)) % 2 = 0 then 'Even' else 'Odd' end
from (values ('4545-4400'), ('4546-4400')) X (id);
Returns
id
4545-4400
Odd
4546-4400
Even
Thats assuming there is always a 4th character. If not you would need to check for it.
You were close, but only need to check a single character against a set of characters:
where Substring( Id, 4, 1 ) like '[02468]'
Note that there is no wildcard (%) in the pattern.
It can be used in an expression like:
case when Substring( Id, 4, 1 ) like '[02468]' then 'Even' else 'Odd' end as Oddity
I wish to convert redundant row values into a comma separated string to build a JSON. Here in my example the columns I need to convert to comma separated string is attrValueId, attrValue and name.
Please use the snippet to build the schema
CREATE TABLE t
([attrId] int, [displayPosition] int,
[attrValueId] int, [attrValue] varchar(30),
name varchar(30), attrName varchar(30),attrType varchar(30),
isRequired bit);
INSERT INTO t VALUES
(1,2,1,'123',NULL,'testattribute','dropdown',0);
INSERT INTO t VALUES
(1,2,2,'1234',NULL,'testattribute','dropdown',0);
INSERT INTO t VALUES
(3,1,6,'miuu2',NULL,'mult','multi-select',1);
INSERT INTO t VALUES
(3,1,7,'miuu3396',NULL,'mult','multi-select',1);
The table data is like
attrId displayPosition attrValueId attrValue name attrName attrType isRequired
1 2 1 123 NULL testattribute dropdown 0
1 2 2 1234 NULL testattribute dropdown 0
3 1 6 miuu2 NULL mult multi-select 1
3 1 7 miuu3396 NULL mult multi-select 1
My required result is
attrId displayPosition attrValueId attrValue name attrName attrType isRequired
1 2 1,2 1234,1234 NULL,NULL testattribute dropdown 0
3 1 6,7 miuu2,miuu3396 NULL,NULL mult multi-select 1
My ultimate aim is to construct a JSON string in the format
[
{"attrId":"1","displayPosition":"2","attrValueId":["1,2"],"attrValue":["1234,1234"],"name":["null","null"],"attrName":"testattribute","attrType":"dropdown","isRequired":"0"}
,
{second row goes here}]
Try using STRING_AGG() by doing GROUP BY over attrId & displayPosition
I have one sample for you.
SELECT
[attrId]
,[displayPosition]
,STRING_AGG([attrValueId],',') [attrValueId]
,STRING_AGG(name,',') name
,STRING_AGG([attrValue],',') [attrValue]
,STRING_AGG(attrType,',') attrType, isRequired
FROM t group by [attrId],[displayPosition],isRequired
I have assumed that [attrId],[displayPosition],isRequired uniquely represent one row.
For Old SQL Server versions, try STUFF()
Check the following threads:
Group By and STUFF combined result in sql server
How to use GROUP BY to concatenate strings in SQL Server?
I am trying to generate a query in Oracle where i can get records that has first character in String as 3 or 4 AND second character is an alphabet. The rest can be anything else.
Something like this
SELECT COL1 FROM TABLE
WHERE REGEXP_LIKE (COL1, '3[A-Za-Z]')
OR REGEXP_LIKE (COL1, '4[A-Za-z]')
I Do get the output but for few records the data doesn't start with 3 or 4.
Meaning it selects those records who have 3 and An alphabet together anywhere in the column.
ex: 10573T2 (10573T2). I have to query records that should start with either 3 or 4 and the next character should be a letter.
Any help would be great
SQL> with test (col) as
2 (select '10573T2' from dual union all
3 select '3A1234F' from dual union all
4 select '23XXX02' from dual union all
5 select '4GABC23' from dual union all
6 select '31234FX' from dual
7 )
8 select col
9 from test
10 where regexp_like(col, '(^3|^4)[[:alpha:]]');
COL
-------
3A1234F
4GABC23
SQL>
begins ^ with 3 or | 4
and is followed by a letter [[:alpha:]]
As of your ^ doubts: that character has two roles:
[^ ... ] - Non-Matching Character List: matches any character not in list ...
^ - Beginning of Line Anchor: match the subsequent expression only when it occurs at the beginning of a line.
You need to anchor the pattern at the beginning of the string:
REGEXP_LIKE(COL1, '^[34][A-Za-z]')
Here is a db<>fiddle
I have data stored in table's column and it has a line break in the data. When I count the length of the string it returns me the count just fine. I want to make some changes and take the line break as 2 characters so if the data in table is something like this.
This
That
This should return length as 10 instead it is returning 9 for now which is understandable but I was to count the length of line break as 2 characters. So if there are 2 line breaks in data it will count them as 4 characters.
How can I achieve this ?
I want to use this in SUBSTR(COL, 1, 7)
By counting line break as 2 character it should return data like this
This
T
Hope someone can help
Just replace new line in the string with 2 characters, for example 'xx', before counting string length. More info on how to replace new lines in Oracle: Oracle REPLACE() function isn't handling carriage-returns & line-feeds
Update your value to have a line feed character before the carriage return character.
So if you have the table:
CREATE TABLE test_data ( value VARCHAR2(20) );
INSERT INTO test_data ( value ) VALUES ( 'This
That' );
Then you can insert the LF before the CR:
UPDATE test_data
SET value = REPLACE( value, CHR(10), CHR(13) || CHR(10) )
WHERE INSTR( value, CHR(10) ) > 0
Then your query:
SELECT SUBSTR( value, 1, 7 ) FROM test_data;
Outputs:
| SUBSTR(VALUE,1,7) |
| :---------------- |
| This |
| T |
db<>fiddle here
How can I select records where in the column Value the 5th character is letter A?
For example the following records:
ID Value
-------------------------
1 1234A5636A6363
2 1234A4343B6363
3 1234B5353A6363
if I run
select * from table
where Value like '%A%'
this will return all records
but all I want is the first 2 where the 5th character is A, regardless if there are more A characters in the text or not
select *
from your_table
where substring(Value, 5, 1) = 'A'
The LIKE operator, in addition to %, which matches any number of any character, can use _, which matches any one single character. You may try:
SELECT *
FROM yourTable
WHERE Value LIKE '____A%'; -- 4 underscores here
use like below by using _(underscore)
LIKE '____A%'
SQL Server
select *
from YourTableName
where CHARINDEX('A', ColumnName) = 5
Note:- This finds where string 'A' starts at position 5
AND specify Your ColumnName