Title the Result from SphinxSearch - sphinx

Any way I can title the search results?
Example:
select name from person ;
Will give
abhilash joseph c
But i want it to be 'Abhilash Joseph C'
Can I write udf for sphinx? How can I do it ?

Related

How to extract an uppercase word from a string in Postgresql only if entire word is in capital letters

I am trying to extract words from a column in a table only if the entire word is in uppercase letters (I am trying to find all acronyms in a column). I tried using the following code, but it gives me all capital letters in a string even if it is just the first letter of a word. Appreciate any help you can provide.
SELECT title, REGEXP_REPLACE(title, '[^A-Z]+', '', 'g') AS acronym
FROM table;
Here is my desired output:
title
acronym
I will leave ASAP
ASAP
David James is LOL
LOL
BTW I went home
BTW
Please RSVP today
RSVP
You could use this regular expression:
regexp_replace(title, '.*?\m([[:upper:]]+)\M.*', '\1 ', 'g')
.*? matches arbitrary letters is a non-greedy fashion
\m matches the beginning of a word
( is the beginning of the part we are interested in
[[:upper:]]* are arbitrarily many uppercase characters
) is the end of that part we are interested in
\M matches the end of a word
.* matches arbitrary characters
\1 references the part delimited by the parentheses
You could try REGEXP_MATCHES function
WITH data AS
(SELECT 'I will leave ASAP' AS title
UNION ALL SELECT 'David James is LOL' AS title
UNION ALL SELECT 'BTW I went home' AS title
UNION ALL SELECT 'Please RSVP today' AS title)
SELECT title, REGEXP_MATCHES(title, '[A-Z][A-Z]+', 'g') AS acronym
FROM data;
See demo here

getting the "title" between the names

The name I'm working on is formatted like this:
King, Mr. Jay Thomas
Smith, Miss. Jane
How do I get the middle title part only using Postgres?
I'm a noob so this is definitely wrong:
SELECT position('%#," #"%#' for '#') AS TITLE
FROM titanic;`
You could use SUBSTRING with the regex pattern \w+\.:
SELECT SUBSTRING(title from '\w+\.')
FROM titanic;

SAS hash table - multivalues for single key and find_next

I am using a hash table to look up values and determine whether there is a match. These are the testing codes.
What I am trying to do is to check whether students in roster have entries in grade_roster; if a student has entries in grade_roster, I attach that grade_on_report to a new dataset that is the dup of roster.
name is my key and the grades are values. One name may have several grades, i.e. multivalues for a single key. I was able to find all names in the roster that have a match in grade_roster using find_next(), but I could not attach the right grade to the new dataset.
It seems like whenever find_next() is called, the value for the key was set to the next item in the list, and therefore assign that value to all previous keys.
Here is my code:
data roster;
input name $ course $ grade_on_paper $;
datalines;
Mary English A
Mary German B
Josh English B
Lily Spanish B
Lucy Physics C
John Music A
Eric Math A
Eric Music B
;
run;
data grade_roster;
input name $ course $ grade_on_report $;
datalines;
Mary English A
Mary German B
John Music A
Eric Math A
Eric Music B
;
run;
data assign_grade;
set roster;
format grade_on_report $1.;
declare hash ht1(dataset:"grade_roster", multidata:"Y");
ht1.defineKey("name");
ht1.defineData("grade_on_report");
ht1.defineDone();
rc = ht1.find();
do while(rc = 0);
rc = ht1.find_next();
end;
run;
What I got was this:
name course grade_on_paper grade_on_report name_found
1 Mary English A B Y
2 Mary German B B Y
3 Josh English B
4 Lily Spanish B
5 Lucy Physics C
6 John Music A A Y
7 Eric Math A B Y
8 Eric Music B B Y
What I want is:
name course grade_on_paper grade_on_report name_found
1 Mary English A A Y
2 Mary German B B Y
3 Josh English B
4 Lily Spanish B
5 Lucy Physics C
6 John Music A A Y
7 Eric Math A A Y
8 Eric Music B B Y
Note: name and course together are not unique identifiers. It seems like they are unique identifiers in this particular test code but they are not unique identifiers in the actual dataset I am working on. The goal is to use name as the only key in ht1.defineKey() and get the correct result.
Any help would be appreciated. Thank you!
If you are trying to find data on a specific student and course, you need to make both name and course part of the key.
data assign_grade;
set roster;
format grade_on_report $1.;
declare hash ht1(dataset:"grade_roster", multidata:"Y");
ht1.defineKey("name", "course");
ht1.defineData("grade_on_report");
ht1.defineDone();
rc = ht1.find();
do while(rc = 0);
rc = ht1.find_next();
end;
run
I tested this on sligthly different data
data roster;
input name $ course $ grade_on_paper $;
datalines;
Mary English A
Mary German B
Josh English B
Lily Spanish B
Lucy Physics C
John Music A
Eric Math A
Eric Music B
;
run;
data grade_roster;
input name $ course $ grade_on_report $;
datalines;
Mary German B
Mary English A
John Music A
Eric Math C
Eric Music B
;
run;
and got what I expected.

PostgreSQL: Sorting letters before numbers

I would like to order a bpchar column in the following order (first order by a-z, then numbers):
abc
bcd
xrf/1
zyd
0/abc
0/bdc
0/efg
How could I accomplish that?
Thank you
Can't fully tell from your question what you actually want. If it is the first character of the string you want to check whether it's numeric or alphabet, you may use a CASE expression in ORDER BY like this.
select * FROM t ORDER BY
CASE
WHEN col ~ '^[a-zA-Z]' THEN 1
WHEN col ~ '^[0-9]' THEN 2
END,col;
Demo

Extracting words from a string in postgres SQL

I am brand new to 'postgres' and trying to extract a value from a string. I'm trying to use a combination of regexp_substr and replace to implement the desired outcome.
UPDATED Example: I have a string "When Harry met Sally" (following the pattern, When X met Y). I'd like to extract the word Harry, which is X.
I am trying the syntax:
regexp_substr(REPLACE('When Harry met Sally', 'When ', ''),' met [^.]*'); but am receiving the error message: ERROR: function regexp_substr(text, unknown) does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Can anyone help? I imagine that this is child's play for some pro out there.
Before using some function, check into the documentation about its syntax... or if it exists.
regexp_substr does not exist in Postgresql. What are you trying to do ?
I'd like to extract the word Harry.
By which criteria? The second word? The X in "When X met Y"?
Without more info, it's impossible to answer.
postgres=# select regexp_replace('When Harry met Sally',
'When (.*) met.*',
'\1' );
regexp_replace
----------------
Harry
(1 row)
This is just an example. It could be wrong if, for example you want to support other kind of blank space, or be case sensitive, or admit some content before 'When', or...