Logic of SQL Query kindly help me out [duplicate] - sql

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%'

Related

How to add 0s to a numeric attribute so that all numbers in the attribute are 10 digits in SQL [duplicate]

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.

How to remove the trailing dynamic zeros for the given numbers in MS-SQL server [duplicate]

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

Extract Last Name only from Lastname,Firstname [duplicate]

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, '[^,]*')

Is there any ways to count number of character in NVARCHAR column in SQL Server? [duplicate]

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

How can we calculate the number of days between two dates in oracle-sql ? exact syntax? [duplicate]

This question already has an answer here:
Get the number of days between two dates in Oracle, inclusive of the dates
(1 answer)
Closed 7 years ago.
I have tried this
select datediff(day,doi,dateeg) from Cardholders;
error:ORA-00904 : "DATEDIFF" invalid identifier
here doi and dateeg are of date datatype in cardholders relation ?
In Oracle, just use subtraction. This will probably do what you want:
select trunc(dateeg) - trunc(doi) from Cardholders