I have a table below and need to use a regular expression to display the correct course digits ( 3 digits being the max # of digits) and also max sure no number appears twice.
the following code I have created is this:
select regexp_substr(course_number,'([0-9][0-9][0-9])\1') as CourseNumber
from courses
/
this is correct showing me an output of this:
How can I sure the regexp in sql/pl to query the digits with no numbers repeating?
the table I created looks like this:
Related
I have a table with project numbers. The project numbers have different formats. I want to filter just the project numbers starting with C and 4 numbers (i.e. C1234) and with underscore 3 numbers (i.e. C1234_123). Other formats that i donĀ“t want have a lot more numbers, points in between etc.
Can you help me with the WHERE clause?
WHERE
Projectnumber LIKE 'C____%'
Here I also get the project numbers that have more than 4 numbers after the C. How to define a filter for this format C1234_123 ?
KR Julia
Here is the answer with a working example,
SELECT
[ProjectNumber]
FROM
[Stuff]
WHERE
[ProjectNumber] LIKE 'C[0-9][0-9][0-9][0-9]\_[0-9][0-9][0-9]' ESCAPE '\';
Note the use of the ESCAPE clause to prevent the _ being treated like a wildcard.
I have a column that I need to select but it has an inconsistent amount of numbers/formatting in the beginning
The column values are ideally supposed to be structured like:
# Question_-_Answer
But here are some examples which make it hard to remove the numbers in the beginning
0 Question1_-_50-60
1.Question_-_apple
12Question_-_40/50
13 Question_-_orange
14.Question_-_apple
15. Question_-_orange2
Is there a way I can query this column so that it ignores everything until the first alphabetical character while also not removing any characters/alphanumerical values in the question and answer portion?
You can use PATINDEX and STUFF to achieve this:
SELECT STUFF(V.YourString,1,PATINDEX('%[A-z]%',V.YourString)-1,'')
FROM (VALUES('0 Question1_-_50-60'),
('1.Question_-_apple'),
('12Question_-_40/50'),
('13 Question_-_orange'),
('14.Question_-_apple'),
('15. Question_-_orange2'))V(YourString);
This removes all characters up to the first alpha character.
The percentage sign (%) is used as the "everything" wildcard instead of an asterisk. It will match zero or more characters.
As #onedaywhen said, the two have same function.
But in impala, I find they only work in different specific situation.
show tables like ' '
Suppose in my database opd, there are there table,
opd.haha
opd.haha1
opd.abc
input:
show tables like 'haha*'
output:
opd.haha
opd.haha1
input:
show tables like 'haha%'
output:
Done. 0 results.
select ... like
select 'haha' like 'ha%' -- true
select 'haha' like 'ha*' -- false
select 'haha' like 'ha__' -- true
select 'haha' like 'haha%' -- true
My question is that
To summarise,
asterisk sign only works in show tables clause, and
percentage sign only works in select clause
Is this comment right?
The standard wildcards for like are:
_ which represents a single character
% which represents zero or more characters
like does not implement regular expressions.
If you want regular expressions, then use regexp_like().
The ID of research fields have three parts, each part separated by a period.
Consider a query to find the details of research fields where the first two parts of the ID are D and 2, and the last part is a single character (digit).
IDs like D.2.1 and D.2.3 are in the query result whereas IDs like D.2.12 or D.2.15 are not.
The SQL query given below does not return the correct result. Explain the reason why it does not return the correct result and give the correct SQL query.
select *
from field
where ID like 'B.1._';
I have no idea why it doesnt work.
Anyone can help on this? Many thanks
D.2.1 and D.2.3 are in the query result whereas IDs like D.2.12 or D.2.15 are not.
An underscore matches any single character in a LIKE filter so B.1._ is looking for the start of the string followed by a B character followed by a . character then a 1 character then a . character then any single character then the end of the string.
You could use:
SELECT *
FROM field
WHERE ID like 'B.1._%';
The % will match any number of characters (including zero) until the end of the string and the preceding underscore will enforce that there is at least one character after the period.
I have a column nr that contains strings in the format of 12345-12345. The numbers before and after the dash can be of any length. I would like to get the maximum value for nr taking into account only the part after the dash. I tried
SELECT MAX(nr) AS max_nr FROM table WHERE (nr LIKE '12345-%')
However, this works only for values < 10 (i.e. 12345-9 would be returned as max even if 12345-10 exists). I thought of removing the dash and doing a type conversion:
SELECT MAX(REPLACE(nr, '-', '')::int) AS max_nr FROM table WHERE (nr LIKE '12345-%')
However, this of course returns the result without the dash. What would be the best way to get the maximum value while including the dash and the number before the dash in the result?
PostgreSQL 9.1
I'm no expert in PostGres, but you can use regexp_replace('foobarbaz', 'b..', 'X') to extract the string after the dash and then convert the number to int. The following query will retrieve only one row the nr from your table where the nr is like 12345-%, sorted by the number after the dash in descending order (largest number first).
SELECT nr
FROM table WHERE (nr LIKE '12345-%')
ORDER BY regexp_replace(nr, '^\d+-', '')::integer DESC
LIMIT 1
The regular expression above removes the leading digits and the dash, leaving only the last set of digits. For example, 54352-12345 would become 12345.
Official documentation.
And here is a SQL Fiddle illustrating it's use.
Use substring function with position function:
http://www.postgresql.org/docs/8.1/static/functions-string.html
to extract number after dash, and then use this value in MAX function as you have in your code now. You can also try to_number function.
It will look similiar to this:
MAX(substring(nr from position('-' in nr))::int)