This question already has answers here:
How to Select a substring in Oracle SQL up to a specific character?
(8 answers)
Closed 5 years ago.
I just started working with Oracle recently so please bear with me.
I have a column of names as LASTNAME,FIRSTNAME and I need to extract JUST the last name. I know how to do this in excel, where I want to so search for the "," find the length of characters before the comma and return those characters.
I know to use SUBSTR as my LEFT() function, but I'm stuck from there.
One method in Oracle is regexp_substr():
select regexp_substr(name, '[^,]*')
Related
This question already has answers here:
How to pad zeroes for a number field?
(4 answers)
How to add leading zero in a number in Oracle SQL query?
(3 answers)
Closed 1 year ago.
I have a numeric attribute that contains IDs in a table in my database. However, the IDs are not of the same length (e.g. 54261, 73284619, 3723). I want all of them to be in the same length by adding 0s in front of them so that all of them are 10 digits (e.g. 000054261, 0073284619, 0000003723). I'm wondering if I can write a SQL query to achieve this or I have to do this in Excel.
Usually functions like LPAD, RPAD are recommended to achieve your usecase
Try something like below
Select TO_NUMBER(LPAD(TO_CHAR(ID) , 10,'0')) FROM
DUAL;
LPAD will add zeroes to the left of the string id to make it a 10 digit and RPAD likewise is vice versa adding 0s to the right
Note: may not be supported accross all dbs but mainly oracle, postgres etc.
This question already has answers here:
Remove trailing zeros from decimal in SQL Server
(23 answers)
Closed 5 years ago.
Given value in database table is 23.045000,45.6090, 23.900 .
Output should be displayed as 23.045, 45.609, 23.9
Cast the value as Float. like this
select cast(23.045000 as float)-->23.045
select cast(45.6090 as float)-->45.609
select cast(23.900 as float)-->23.9
This question already has answers here:
How can you find the number of occurrences of a particular character in a string using sql?
(4 answers)
Closed 6 years ago.
For example, I have 100001,100002, stored in a NVARCHAR column.
I want to count the number of occurrences of , in this column.
select len('100001,100002,') - len(replace('100001,100002,',',',''))
returns 2
This question already has answers here:
How to count the number of occurrences of a character in an Oracle varchar value?
(9 answers)
Counting the number of occurrences of a character in Oracle SQL [closed]
(2 answers)
Closed 8 years ago.
If a column contains a certain value (for example, SEEMA), what method might one to use to determine how many times the letter E occurs in that value?
regexp_count should do the trick:
SELECT col, REGEXP_COUNT(col, 'E')
FROM some_table
WHERE col LIKE '%SEEMA%'
This question already has answers here:
Oracle Date TO_CHAR('Month DD, YYYY') has extra spaces in it
(6 answers)
Closed 8 years ago.
I have a table called room_allocation and want to extract details of those who were admitted in the month of January.So I have used the following query:
select * from room_allocation where to_char(adm_date,'Month')='January ';
the output for this is:
no data found
but when I give it as:
select to_char(adm_date,'Month') from room_allocation;
I get the output as:
TO_CHAR(ADM_DATE,'MONTH')
October
November
December
January
So please tell me why its not working in the first case.
Thank you.
Use the format modifier FM. This will remove the trailing space.
SELECT *
FROM room_allocation
WHERE to_char(adm_date,'FMMonth')='January';
Maybe the extra space you have at the end of January.
select * from room_allocation where to_char(adm_date,'Month')='January ';
Remove that and try it again