so I have a table TEST with 2 columns - id and value. I would like a count of the number of spaces before a specific string value.
id value
1 AB CD EFD RA
I would like a query to count the number of spaces in the value column to the left of "EFD". In this case it should be 2.
Thanks!
I tried:
select ID,
VALUE,
regexp_substr(Value, 'EFD') AS "SUBSTRING",
regexp_instr(Value, 'EFD') AS "POSITION"
I would like to get the position of EFD in this array as well. I should get 3.
select id, value,
regexp_substr(Value, 'EFD') AS substring,
regexp_count(substring(Value, 1, regexp_instr(Value, 'EFD')-1), ' ') AS spaces_before_EFD,
regexp_instr(Value, 'EFD') AS position
from test;
Related
I have a table with a column having multi line value and each line is kind of key value pair (separated by = sign and probably ending with new line breaks)
Example value in one cell in the column:
List of key value pair
key00=value00 <\n> key01=value01 <\n> key02=value02
I am looking for a SQL (Oracle) query to find the specific key (say Key01) and display it in following format
KEY
VALUE
Key01
Value01
Please help.
Here's one option
Sample data:
SQL> select * from test;
ID COL
---------- --------------------------------------------------
1 key00=value00
key01=value01
key02=value02
Subquery returns substring that begins with the "key" value (passed as a parameter), while outer query splits that substring into key and value themselves:
SQL> select regexp_substr(str, '^\w+') key,
2 regexp_substr(str, '\w+$') value
3 from (select regexp_substr(col, '&par_key=\w+') str
4 from test
5 );
Enter value for par_key: key01
KEY VALUE
--------------- ---------------
key01 value01
SQL>
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
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
I'm using postgresql as DB.
Using my query to select column housenumber of varchar type(and some other columns) from table with buildings info. So I want the result to be ordered other way, rather then string comparison.
For example, if I have following results:
"1"
"1 block2"
"1 b30"
"1 b3"
"1 b3 s4"
"10"
"2"
I want this result to be sorted by following logic:
1) getting source string "1 b3 s4"
2) split it into ["1" , "b3" , "s4"]
3) try to parse all substrings to integer, ignoring letters, which
are not numbers into [1 , 3, 4]
4) calculate bigger number for future sorting as
1 * 1000000 + 3 * 1000 + 4 = 1003004.
Is this possible and how could I implement this methoad and use it for sorting query result?
Here is my sql query(shorted):
SELECT housenumber, name
FROM osm_buildings
where
housenumber <> ''
order by housenumber
limit 100
I'm not sure why you would want to convert to some big integer for sorting. You can do the following:
Remove all characters that are not digits or spaces.
Convert to an array, splitting on one or more spaces.
Convert the array to an integer array.
Then you can can sort on this:
order by regexp_split_to_array(regexp_replace(v.addr, '[^0-9 ]', '', 'g'), ' +')::int[]
You can store this as a value in a table, if you want to persist it.
Here is a db<>fiddle.
I'm trying to get the max value from a text field. All but two of the values are numbers with a single decimal. However, two of the values have something like 8.2.10. How can I pull back just the integer value? The values can go higher than 9.n, so I need to convert this field into a number so that I can get the largest value returned. So all I want to get back is the 8 from the 8.2.1.
Select cast(VERSION as int) is bombing out because of those two values with a second . in them.
You may derive by using regexp_substr with \d pattern :
with tab as
(
select regexp_substr('8.2.1', '\d', 1, 1) from dual
union all
select regexp_substr('9.0.1', '\d', 1, 1) from dual
)
select * from tab;
For Oracle you must attend the value as string for retire only the part before the dot. Ex:
SELECT NVL( SUBSTR('8.2.1',0, INSTR('8.2.1','.')-1),'8.2.1') AS SR FROM DUAL;
Check than the value is repeated 3 times in the sentence, and if the value is zero or the value didn't have decimal part then it will return the value as was set.
I had to use T-SQL rather PL/SQL, but the idea is the same:
DECLARE #s VARCHAR(10);
SELECT #s='8.2.1';
SELECT CAST(LEFT(#s, CHARINDEX('.', #s) - 1) AS INT);
returns the integer 8 - note that it won't work if there are no dots because it takes the part of the string to the left of the first dot.
If my quick look at equivalent functions was correct, then in Oracle that would end up as:
SELECT CAST(SUBSTR(VERSION, 1, INSTR(VERSION, '.') - 1) AS INT)