SQL Get char at position in field - sql

How can I get the character at position 4 in a field?
e.g.
field contents = "hello"
I want to return the value of position 2 = "l"

In SQL Server you can use SUBSTRING
SELECT SUBSTRING('hello', 3, 1)
Take care: index is 1-based.

In Oracle, use SUBSTR
Syntax is SUBSTR(<string to parse>,<start position>,(<length>)) - i.e.
SELECT SUBSTR('hello',3,1)
Start position and length are one-, not zero-based. Zero is accepted, but will be interpreted as 1.

following Query will search from specific index for char and will return result
select * from tbPatientMaster
where SUBSTRING (fname,CHARINDEX ('.',fname,0)+1,LEN (fname)) like 'a%'

Related

SQL Server substring without upper bound

What is the best way in SQL Server to do
SELECT
SUBSTRING('CATCH ME IF YOU CAN', 2, 10000)
-- ATCH ME IF YOU CAN
with no upper bound?
Use STUFF instead (STUFF (Transact-SQL)):
SELECT STUFF('CATCH ME IF YOU CAN',1,1,'');
Here, STUFF replaces 1 character, from position 1 with the string ''. The 2nd parameter is the start position, and the 3rd is the number of characters (from that position) to replace. The 4th is the replacement string.
So, as a further example, you could do something like this:
SELECT STUFF('2019-07-09 11:38:00',11,1,'T');
This replaces 1 character from position 11 with the character 'T', which returns '2019-07-09T11:38:00', changing the above value to the ISO8601 format. As you can see, the length of the string to replace does not need to be the same length as the replacement string as well (in fact, the 3rd parameter can have a value of 0, meaning that no characters are replaced and the "replacement" string is simply injected into the existing value).
Using LEN() with variable
DECLARE #Val AS VARCHAR (MAX) = 'CATCH ME IF YOU CAN';
SELECT SUBSTRING(#Val, 2, LEN(#Val));
or directly with LEN()
SELECT SUBSTRING('CATCH ME IF YOU CAN', 2, LEN('CATCH ME IF YOU CAN'));
You can use RIGHT() function, combined with LEN().
All you have to do is subtract from LEN() the number of chars that you want to exclude from the start of the string:
SELECT RIGHT('CATCH ME IF YOU CAN', LEN('CATCH ME IF YOU CAN') - 1)

Regular expression for gettin data after - in sql

I have a column with assignment numbers like - 11827,27266,91717,09818-2,726252-3,8716151-0,827272,18181
Now i am selecting the records like
select assignment_number from table;
But now i want that the column detail is retreived in such a way that numbers are only retrieved without -2 -3 etc like
726252-3---> 726252 8716151-0-->8716151
I know i can use regex for this but i do not know how to use it
This will select everthing before the character -:
^([^-]+)
From 726252-3 will match 726252
You would use regexp() substr:
select regexp_substr(assignmentnumber, '[0-9]+')
This will return the first string of numbers encountered in the string.

How to return the second to last character in a string using SQL

I am trying to only return the second to last character from a string using MS SQL.
I've tried using MID and Substring but the length of the string isn't always the same for the column I am trying to return, So I can't do it that way.
So say I am returning the codes of something:
Code
'1234'
I want to just return '3' from that code.
How can I do this?
Cheers in advance :)
Use SUBSTRING and LEN. LEN gives you the length of the string, then subtract 1 to get the previous char:
SELECT SUBSTRING(Code, LEN(Code)-1,1)
How about
select left(right(code, 2), 1) from MyTable;
You might need to validate that the string actually has at least 2 chars, however.
SqlFiddle here

how to print some letters from a string in sql server

I had a table like bellow image. I want 2,3 letters from the column "name"
The result is like bc,da,bc,bc,bc,bc
SELECT SUBSTRING(name,2,2) AS ShortComp FROM table;
You can also use the string function LEFT() to return the leftmost specified number of characters, like this:
SELECT LEFT(name, 2) AS ShortComp FROM table;

SQL Select by condition on a integer field

I have an integer column in my table. It is product id and has values like
112233001
112233002
113311001
225577001
This numbering (AABBCCDDD) is formed of 4 parts:
AA : first level category
BB : second level category
CC : third level category
DDD : counter
I want to check condition in my SELECT statement to select rows that for example have BB = 33 and AA = 11
Please help
Would this suffice:
select x from table where field >= 113300000 and field < 113400000
SELECT * FROM YOURTABLE
WHERE
substr(PRODUCT_ID, 3, 2)='33'
AND
substr(PRODUCT_ID, 1, 2)='11'
OR
SELECT * FROM YOURTABLE
WHERE
PRODUCT_ID LIKE '11%33%'
and yes in short you have to convert to string
reference of substr
Purpose
The SUBSTR functions return a portion of char, beginning at character position, substring_length characters long. SUBSTR calculates lengths using characters as defined by the input character set. SUBSTRB uses bytes instead of characters. SUBSTRC uses Unicode complete characters. SUBSTR2 uses UCS2 code points. SUBSTR4 uses UCS4 code points.
If position is 0, then it is treated as 1.
If position is positive, then Oracle Database counts from the beginning of char to find the first character.
If position is negative, then Oracle counts backward from the end of char.
If substring_length is omitted, then Oracle returns all characters to the end of char. If substring_length is less than 1, then Oracle returns null.
char can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. Both position and substring_length must be of datatype NUMBER, or any datatype that can be implicitly converted to NUMBER, and must resolve to an integer. The return value is the same datatype as char. Floating-point numbers passed as arguments to SUBSTR are automatically converted to integers.
Select field from table where substr(field,,) = value
This seems like it could work. Otherwise you may have to cast them as strings and parse the values out that you need which would make your queries much slower.
SELECT *
FROM table t
WHERE t.field >= 113300000
AND t.field < 113400000
u need to use _ wildcard char -
SELECT *
FROM TABLE
WHERE
FIELD LIKE '1133_____'
here, each _ is for one char. So you need to put the same number of _ to keep the length same