How to swap first character with last character in ename column from employee table. I amUsing EMP table default in SQL* plus
I have tried dual replace , but no success.
Ex : Smith is my name , my expected output is hmits
Just using substr() three times to fetch first, last and rest of the characters. And concatenate them all
select substr(ename,length (ename) )||
substr(ename,2,length(ename)-2)||
substr(ename,1,1)
from employee
In Oracle, you can do:
select upper(substr(ename, -1)) || substr(ename, 2) || substr(ename, 1, 1)
from employee
This should be easy to put into an update if you want to actually change the data.
Related
I have a column "employee_Id" in my Table "Employee".
employee_Id is having employee name and date of birth. For example :
Jason-21996 and Buttler
Please help me write a select query which returns Jason and Buttler as output.
This is the query I am trying :
select substring(employee_Id,1, LOCATE('-',employee_Id) - 1) as Emp_ID from Employee
I am seeing this error:
SQL Error [42815]: THE DATA TYPE, LENGTH, OR VALUE OF ARGUMENT 3 OF SUBSTRING IS INVALID. SQLCODE=-171, SQLSTATE=42815, DRIVER=4.9.78
Edit 1: As suggested by #Mark, I have edited the query as follows
select substring(employee_Id,1, LOCATE('-',employee_Id || '-') - 1) as Emp_ID from Employee
I am getting the same error. I tried to run the LOCATE and found that it is returning the index as 15 for Buttler as the column length is 15.
Run the following as is.
select substring(employee_Id, 1, LOCATE('-', employee_Id || '-') - 1) as Emp_ID
from
(
SELECT 'Jason-21996' FROM SYSIBM.SYSDUMMY1
UNION ALL SELECT 'Buttler' FROM SYSIBM.SYSDUMMY1
) Employee (employee_Id);
Does it work for you?
If your version of DB2 supports regular expressions, then a simple method is regexp_substr():
regexp_substr(employee_id, '^[^-]+')
Here is a db<>fiddle.
I need to extract every thing before last two periods
eg.
Input: AA.BBB.12.11.cc
Output: AA.BBB.12
Following is the sample query I am using but that returns only the characters before first period, but that is not I needed.
SELECT REGEXP_SUBSTR(t.column,'[^.]+',1,1)
AS output
FROM MY_Table t where t.column is not null and rownum=1
I would use REGEXP_REPLACE here:
SELECT REGEXP_REPLACE(t.column, '\.[^.]+\.[^.]+$', '')
FROM MY_table
WHERE t.column IS NOT NULL AND rownum = 1;
The regex pattern \.[^.]+\.[^.]+$ will match starting with the second to last dot, all content until the end (including also the last dot).
You can simply use INSTR and SUBSTR as following:
SQL> SELECT
2 SUBSTR('AA.BBB.12.11.ccCC', 1, INSTR('AA.BBB.12.11.ccCC', '.', -2, 2) - 1) AS RESULT
3 FROM DUAL;
RESULT
---------
AA.BBB.12
SQL>
-- Update --
For the question asked in the comment, use the following query:
SQL> SELECT
2 SUBSTR('AA.BB.CC.DD', 1, INSTR('AA.BB.CC.DD', '.', 1, 3) - 1) AS RESULT
3 FROM DUAL;
RESULT
--------
AA.BB.CC
SQL>
Cheers!!
I'm sure this is super easy, but how would I go about converting LastName,FirstName to LastName,FirstInitial?
For example changing Smith,John to Smith,J or Johnson,John to Johnson,J etc.
Thank You!
In case of LastName and FirstName columns:
select LastName,substr(FirstName,1,1)
from mytable
;
In case of a fullname saved in a single column:
select substr(fullname,1,instr(fullname || ',',',')-1) || substr(fullname,instr(fullname || ',',','),2)
from mytable
;
or
select regexp_replace (fullname,'([^,]*,?)(.).*','\1\2')
from mytable
;
Here is one way, using just "standard" instr and substr. Assuming your input is a single string in the format 'Smith,John':
select substr(fullname, 1, instr(fullname, ',')+1) from yourtable;
yourtable is the name of the table, and fullname is the name of the column.
instr(fullname, ',') finds the position of the comma within the input string (it would be 6 in 'Smith,John'); thensubstrtakes the substring that begins at the first position (the1in the function call) and ends at the position calculated byinstr`, PLUS 1 (to get the first initial as well).
E.g:- BRAKE,CRANE etc.
In my employees table , I have ENAME,ENO,JOB,SALARY.
Here, I want to extract out those enames that have an 'A' as the center character in their name.
If length of ename is odd,then center one, so i need to detect odd and even position in ename.
So, I tried this, but stuck up ,so can i expect a help from here?
SELECT ENAME
FROM EMPLOYEES
WHERE A IN
(SELECT ENAME,
SUBSTR(ENAME,LENGTH(ENAME)/2+1,1)
FROM EMPLOYEES)
;
This works for odd length strings, which I think is what you wanted. Next time please don't use caps like that. It took me 5 minutes just to read your post.
SELECT `ENAME` FROM `EMPLOYEES` WHERE SUBSTR(`ENAME`, LENGTH(`ENAME`)/2+1, 1) = 'A'
SELECT ename
FROM employees
WHERE
INSTR(
CASE
WHEN MOD(LENGTH(ename),2) = 0 THEN SUBSTR( ename, LENGTH(ename)/2, 2 )
ELSE SUBSTR( ename, (1+LENGTH(ename)/2), 1 )
END,
'A'
) > 0
This checks first that they have an odd number of letters in the name, then does the check.
The second part checks the middle 2 letters for even-numbered lengths to see if either is A.
This is SQL Server syntax but I think Oracle should be similar.
SELECT ENAME
FROM EMPLOYEES
WHERE ((LEN(ENAME) %2) = 1
AND SUBSTRING(ENAME, LEN(Ename)/2+1, 1) = 'A')
OR
((LEN(ENAME) %2) = 0
AND SUBSTRING(ENAME, LEN(ENAME)/2-1, 2) LIKE '%A%')
I think this is what you mean:
SELECT ENAME FROM EMPLOYEES where ENAME=SUBSTR(ENAME,LENGTH((ENAME+1)/2),1)
What Database service are you using? (for instance in MS SQL server you must use Len)
You may even try this one:
select ename from emp where substr(ename,ceil((length(ename))/2),1)='A';
This will work for both even and odd length strings...hope it helped.
SELECT ename FROM employees
WHERE instr(ename, 'A', 1, 1) = round(length(ename) / 2);
I need to compare the value of a column (LASTNAME) with a system variable (:VARIABLE), but the variable is an email address, so I need to trim off the "#email.com" and "firstname." Some things I've tried:
select *
from TABLENAME
where LASTNAME LIKE :VARIABLE
select *
from TABLENAME
where LASTNAME IN :VARIABLE
I've been able to trim off the #email.com, can't figure out how to trim off FIRSTNAME. at the same time.
Regular expressions can help:
SQL> SELECT LTRIM(regexp_substr('firstname.lastname#abc.com','\.[^#]*'),'.') last_name from dual;
LAST_NAME
---------
lastname
Your query could then look like:
SELECT *
FROM tablename
WHERE UPPER(LTRIM(regexp_substr(:VARIABLE,'\.[^#]*'),'.')) = UPPER(lastname);
You can use a combination of SUBSTR and INSTR.
The instr function returns the location of a substring in a string. With this one you can locate the "."or the "#" char.
Then use the substr to get the substring from the begining to the previous located position
SELECT SUBSTR('Take the first four characters', 1, 4)
Use:
SELECT t.*
FROM TABLE t
WHERE t.lastname LIKE '%' || SUBSTR(:VARIABLE,
INSTR(:VARIABLE, '.') +1,
INSTR(:VARIABLE, '#')) || '%'
Add UPPER or LOWER to both sides if you need to be sure of case insensitive matching.
Reference:
SUBSTR
INSTR