Where x character equal value - sql

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

Related

Check specific integer is even

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 to check the last two digits?

SUBBIS
SUBB1D
SUBBD3
SUBB12
In above values, how can I check the last two digits (IS, 1D, D3, 12) are numbers using a sql code?
Do you mean to fetch those values? You can do that with like:
where column like '%[0-9][0-9]'
If you need to ensure that the values always end with 2 numbers, you can do it with similar check constraint.
To check the last two digits are numbers in column, you can use the following script.
... WHERE ISNUMERIC(RIGHT(your_column,2)) = 1
Here RIGHT(your_column,2) will return the last two digits from the string.
or
SELECT ISNUMERIC(RIGHT(your_column,2))
will return 1 (if its number) otherwise 0
You can do it this way:
SELECT MyId,
ISNUMERIC(RIGHT(MyColumn,2)) -- your column to check last 2 (if numeric)
FROM (
----- replace with your table
SELECT 1 MyId,'SUBBIS' MyColumn UNION SELECT 2,'SUBB1D' UNION
SELECT 3,'SUBBD3' UNION SELECT 4,'SUBB12'
----- replace with your table
) A
Hope it helps. :)
You can use like and _ "underscore" to get last one digits record columName
SELECT columName FROM sub WHERE columName LIKE "SUBB__" ;
Record :
columName
SUBBIS
SUBB1D
SUBBD3
SUBB12
SUBBBA

Select Where Like regular expression

I need to create a SQL Query.
This query need to select from a table where a column contains regular expression.
For example, I have those values:
TABLE test (name)
XHRTCNW
DHRTRRR
XHRTCOP
CPHCTPC
CDDHRTF
PEOFOFD
I want to select all the data who have "HRT" after 1 char (value 1, 2 and 3 - Values who looks like "-HRT---") but not those who might have "HRT" after 1 char (value 5).
So I'm not sure how to do it because a simple
SELECT *
FROM test
WHERE name LIKE "%HRT%"
will return value 1, 2, 3 and 5.
Sorry if I'm not really clear with what I want/need.
You can also change the pattern. Instead of using % which means zero-or-more anything, you can use _ which means exactly one.
SELECT * FROM test WHERE name like '_HRT%';
You can use substring.
SELECT * FROM test WHERE substring(name from 2 for 3) = 'HRT'
Are the names always 7 letters? Do:
SELECT substring (2, 4, field) from sometable
That will just select the 2-4th characters and then you can use like "%HRT"

pgsql parse string to get a string after certain position

I have a table column that has data like
NA_PTR_51000_LAT_CO-BOGOTA_S_A
NA_PTR_51000_LAT_COL_M_A
NA_PTR_51000_LAT_COL_S_A
NA_PTR_51000_LAT_COL_S_B
NA_PTR_51000_LAT_MX-MC_L_A
NA_PTR_51000_LAT_MX-MTY_M_A
I want to parse each column value so that I get the values in column_B. Thank you.
COLUMN_A COLUMN_B
NA_PTR_51000_LAT_CO-BOGOTA_S_A CO-BOGOTA
NA_PTR_51000_LAT_COL_M_A COL
NA_PTR_51000_LAT_COL_S_A COL
NA_PTR_51000_LAT_COL_S_B COL
NA_PTR_51000_LAT_MX-MC_L_A MX-MC
NA_PTR_51000_LAT_MX-MTY_M_A MX-MTY
I'm not sure of the Postgresql and I can't get SQL fiddle to accept the schema build...
substring and length may vary...
Select Column_A, substr(columN_A,18,length(columN_A)-17-4) from tableName
Ok how about this then:
http://sqlfiddle.com/#!15/ad0dd/56/0
Select column_A, b
from (
Select Column_A, b, row_number() OVER (ORDER BY column_A) AS k
FROM (
SELECT Column_A
, regexp_split_to_table(Column_A, '_') b
FROM test
) I
) X
Where k%7=5
Inside out:
Inner most select simply splits the data into multiple rows on _
middle select adds a row number so that we can use the use the mod operator to find all occurances of a 5th remainder.
This ASSUMES that the section of data you're after is always the 5th segment AND that there are always 7 segments...
Use regexp_matches() with a search pattern like 'NA_PTR_51000_LAT_(.+)_'
This should return everything after NA_PTR_51000_LAT_ before the next underscore, which would match the pattern you are looking for.

SQL LIKE only one letter

When i use LIKE statement in my SQL,
for example,
SELECT * FROM table WHERE name = "%k"
It will return all rows, where name ends on k.
It can return : Ok, OOk, OOOk,
How i can do same statement but with one letter, so it returns only Ok.
Or 2 letters, so it returns only OOk?
_ is a single character wildcard.
SELECT * FROM table WHERE name = `_k`
See this IBM Reference
Use the equality operator (=) or the IN operator instead of the LIKE operator:
SELECT * FROM table WHERE name IN ('Ok', 'OOk')
Use the _ wildcard. It matches only a single character.
_k for Ok.
__k for Ook
depending on the length you provide
For 1 character 'X' followed by k
Select *
from table
where length(name) = 2
AND name = "%k"
For 2 character 'X' followed by k
Select *
from table
where length(name) = 3
AND name = "%k"
You should use an underscore (_) character. See the documentation about operator LIKE.
So, the query you need (1 or 2 chars before k) is:
SELECT * FROM table WHERE name LIKE '_k' OR name LIKE '__k'