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.
Related
I have column with data such as '123456789012'
I want to divide each of each 3 chars from the data with a '/' in between so that the output will be like: "123/456/789/012"
I tried "SELECT TO_CHAR(DATA, '999/999/999/999') FROM TABLE 1" but it does not print out the output as what I wanted. Previously I did "SELECT TO_CHAR(DATA, '$999,999,999,999.99') FROM TABLE 1 and it printed out as "$123,456,789,012.00" so I thought I could do the same for other case as well, but I guess that's not the case.
There is also a case where I also want to put '#' in front of the data so the output will be something like this: #12345678901234. Can I use TO_CHAR for this problem too?
Is these possible? Because when I go through the documentation of oracle about TO_CHAR, it stated a few format that can be use for TO_CHAR function and the format that I want is not listed there.
Thank you in advance. :D
Here is one option with varchar2 datatype:
with test as (
select '123456789012' a from dual
)
select listagg(substr(a,(level-1)*3+1,3),'/') within group (order by rownum) num
from test
connect by level <=length(a)
or
with test as (
select '123456789012.23' a from dual
)
select '$'||listagg(substr((regexp_substr(a,'[0-9]{1,}')),(level-1)*3+1,3),',') within group (order by rownum)||regexp_substr(a,'[.][0-9]{1,}') num
from test
connect by level <=length(a)
output:
1st query
123/456/789/012
2nd query
$123,456,789,012.23
If you wants groups of three then you can use the group separator G, and specify the character to use:
SELECT TO_CHAR(DATA, 'FM999G999G999G999', 'NLS_NUMERIC_CHARACTERS=./') FROM TABLE_1
123/456/789/012
If you want a leading # then you can use the currency indicator L, and again specify the character to use:
SELECT TO_CHAR(DATA, 'FML999999999999', 'NLS_CURRENCY=#') FROM TABLE_1
#123456789012
Or combine both:
SELECT TO_CHAR(DATA, 'FML999G999G999G999', 'NLS_CURRENCY=# NLS_NUMERIC_CHARACTERS=./') FROM TABLE_1
#123/456/789/012
db<>fiddle
The data type is always a string; only the format changes.
I have a column "Department" in my Table "College". Department is having data like
Commerce1-683877
Science2-678900
I need to write a select query which returns only Commerce1 and Science2.
I am completely new to DB2, please help me solve this.
select substring(department,0,CHARINDEX('-',department) as DEPT from College
Edit -1 : Thanks #Charles
I tried your solution :
select substring(department , 1, LOCATE('-',department) - 1) AS DEPT from College
but it's throwing me an 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 2: You're right #Charles, all my rows doesn't contain '-'
I tried using the below query as well but getting the same error :
select substring(department,1, LOCATE('-',department || '-') - 1) as DEPT from College
What platform and version of Db2?
Did you look for an appropriate Db2 SQL Reference manual?
Note Db2 SQL string & arrays start at 1, not 0.
select substring(department,1, LOCATE('-',department) - 1) as DEPT from College
One method uses regexp_substr():
regexp_substr(department, '^[^-]+')
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!!
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.
In my sql database I have a table like "REG_DET", This table mainly include 3 data member. NAME,AGE,SSN. Here SSN is a string include more than 10 character. Here my problem is when i read SSN from the table i need only last 4 character that is
Eg. Rohith , 25 , 1023456457
result only include Rohith , 25 ,6457(last 4 character of SSN)
Is It possible to do it in a sql select query(at the time of selection). If anyone know pls help me
You can use RIGHT function:
SELECT NAME,
AGE,
RIGHT(SSN, 4) As SSN_Short
FROM REG_DET
(assuming that you're using SQL-Server or MySQL)
Edit: Since you're using TEXT, you need to conver it to VARCHAR first:
SELECT NAME,
AGE,
RIGHT(CONVERT(VARCHAR(8000),SSN)), 4) As SSN_Short
FROM REG_DET
Yes it is possible with the RIGHT function:
SELECT NAME, AGE, RIGHT(SSN, 4) FROM REG_DET
To get the last 4 digits from the SSN column , you can also use SUBSTRING. If the length of the SSN column is 10, then you can use the following query:
SELECT NAME, AGE, SUBSTRING(SSN, 6, 4) FROM REG_DET