Count a letter in string [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)
Closed last month.
How can i count the number of 'a' in the string 'rajarajeshwari'
I want count of 'a' in that string

You can try like this
select length('rajarajeshwari') - length(replace('rajarajeshwari','a',null))
from dual;
DEMO
or you can also use the REGEXP_COUNT function
select REGEXP_COUNT('rajarajeshwari', 'a') from dual;
DEMO

Related

How to Query between string dates in Oracle DB [duplicate]

This question already has answers here:
convert string to required timestamp format in oracle
(2 answers)
Closed 5 months ago.
I have the following under mycolumn in an Oracle DB
1996-02-14-02.28.37.404000
1996-02-14-02.28.37.404001
1996-02-14-02.28.37.418000
1996-02-16-02.53.00.248000
1996-02-16-02.53.00.265000
How can I select some values using WHERE in Oracle SQL?
I tried the following
SELECT TO_DATE(mycolumn, '%Y-%m-%d-%H.%i.%s.%f')
FROM mytable
WHERE TO_DATE(mycolumn, '%Y-%m-%d-%H.%i.%s.%f') > timestamp '2013-02-14 02:28:37.404'
AND TO_DATE(mycolumn, '%Y-%m-%d-%H.%i.%s.%f') < timestamp '2013-03-14 02:28:37.404';
If mycolumn column's datatype is timestamp (should be; I hope you aren't storing those values into a varchar2 column), then you'd e.g.
select *
from mytable
where mycolumn > to_timestamp('2013-02-14 02:28:37.404', 'yyyy-mm-dd hh24:mi:ss.ff3');
In other words, don't touch mycolumn and provide valid format model for value you're comparing it with.
If it really is a string (in a varchar2 column), then do the same for both values, e.g.
where to_timestamp(mycolumn, 'yyyy-mm-dd-hh24.mi.ss.ff6') >
to_timestamp('2013-02-14 02:28:37.404', 'yyyy-mm-dd hh24:mi:ss.ff3')

Compare Columns Date /w Todays Date [duplicate]

This question already has answers here:
DATEDIFF function in Oracle [duplicate]
(4 answers)
Closed 3 years ago.
I'm trying to compare the column: LastUpdated with todays date in days, rounded to 1 decimal place. I keep getting the error
ERROR at line 4:
ORA-00904: "DATEDIFF": invalid identifier
Any ideas?
SELECT
DISTINCT "AppName",
"ApprovedForRelease",
DATEDIFF(DAY,"LastUpdated",GETDATE()) AS "DaySinceUpdated"
FROM BR_APP
WHERE "ApprovedForRelease" = 'Y';
In Oracle, you can use subtraction. To get the dates between, truncate off the time:
SELECT DISTINCT "AppName", "ApprovedForRelease",
(TRUNC(sysdate) - TRUNC("LastUpdated")) AS "DaySinceUpdated"
FROM BR_APP
WHERE "ApprovedForRelease" = 'Y';
The code you have used is based on SQL Server.

Sql find the last non-numeric character in a string [duplicate]

This question already has answers here:
SQL to find first non-numeric character in a string
(3 answers)
Closed 7 years ago.
How to find last non-numeric character in a string such as "10jnklgm51". In order to find 'm' in the example what is the best simple way?
The last non-numeric character is the first non-numeric character in the reverse string. So, something like this:
select substring(reverse(str),
patindex('%[^0-9]%', reverse(str)),
1)

Validate date in Oracle without using function [duplicate]

This question already has answers here:
Regular Expression of a specific Date format
(2 answers)
Closed 8 years ago.
I am newbie in Oracle. I have to select invalid dates in column in table in oracle database. But I also can't write function because of having read only rights in database.
Can anybody help to write simple query to select invalid date in columns:
e.g. select dates, to_date(dates 'yyyy/mm/dd') from table
In the above query if the date is not in valid format it give error. But instead of error I have to output that date. Can we do it in simple query?
You can test the format using a regular expression.
It would be something like:
select dates
from tbl
where regexp_like(dates, '[[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}')
This works okay. It checks that the format is in "4-digit number / 2 digit number / 2 digit number". You might want something stronger, such as:
select dates
from tbl
where regexp_like(dates, '[[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}') or
(substr(dates, 1, 4) not between '1900' and '2014' or
substr(dates, 6, 2) not between '01' and '12'
substr(dates, 9, 2) not between '01' and '31'
)
This checks the format and for reasonable values in each column. Of course, it doesn't check for June 31st, but it will catch many errors.
You can try:
select *
from tbl
where not regexp_like (dtstring,'^[[:digit:]{4},/,[:digit:]{2},/,[:digit:]{2}]')
Fiddle: http://sqlfiddle.com/#!4/c7da3/11/0
Will show any values that are not 4 digits, followed by a slash, 2 more digits, followed by a slash, followed by 2 more digits.
It will not however check to see if a value matching that format is invalid, ie. 2012/23/99

convert decimal to date during where clause [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert DB2 SQL Decimal to DATE
I have a db2 database and I want to convert a decimal to a date during the where clause in a select statement.
The decimal date could be 12 or 13 characters long depending on the month.
12 characters:
1,241,999.00 should become: 1999/1/24
13 Characters:
12,241,999.00 should become: 1999/12/24
The column name is DECIMALCOLUMN:
Select * from table1 WHERE
cast(replace(convert,DECIMALCOLUMN,date)) = #date
I see: You want some way of rearranging the digits of a number to become a date. It looks like the encoding is [m]mddyyyy. (Why isn't that a date datatype in the database?)
The number needs to be converted to a string and then substrings arranged and converted to a date. Given the complexities here, a conversion function should be written in lieu of the field being altered to be a proper datatype. Something like this should do it (untested, no access to db2):
create function decimaldate(decdate DECIMAL)
returns DATE
return
with tmp (dstr) as
(
select substr(digits (decdate),8)
from sysibm.sysdummy1
)
select
date (substr(dstr,4,4) || '-' ||
substr(dstr,1,2) || '-' ||
substr(dstr,3,2)
)
from tmp
This converts the number to a string, reformats the string as yyyy-mm-dd and converts to a date. (I used this as a basis.)
So the solution to the original question is simply to write:
SELECT *
FROM table1
WHERE decimaldate(DECIMALCOLUMN) = #date
With such a screwy way of encoding the date in the database, having the function always available should prove invaluable.