String masking with Regex at Oracle - sql

I want to mask name and surname in Oracle.
For example; John Smith => Jo** Sm**
I can write a PL/SQL function. But i want to do this with regex. I can't write the right template. Is regex the right solution? Is there anyone who can help?

If you want todo it with regexp you can use the following:
REGEXP_REPLACE('John Smith', '(\w{2})\w+', '\1**')

Related

Hibernate Search with Lucene Query for first and last name together

I have attempted this with phrase, wildcard and keyword queries but nothing really works perfectly.
...
#Field(name = "firstLastName", index = org.hibernate.search.annotations.Index.YES, analyze = Analyze.NO, store = Store.NO)
public String getFirstLastName() {
return this.firstLastName;
}
...
Now I want to query this field and return the correct results if a user types John Smith, Smith John or Smith Jo* or John Smi*....
junction = junction.should(qb.keyword().wildcard().onField("firstLastName")
.matching("John Smith*").createQuery());
If I search for just Smith or John given a keyword query, I get a hit. I am not analyzing the field as I didn't think I needed to but I tried it both ways with no success...
Several issues here:
You need to use an analyzer, be it only to split the strings on whitespaces. Define an analyzer and assign it to your field.
You can't use wildcard queries if you want the strings to be analyzed: wildcard queries are not analyzed. You should use an EdgeNGramFilter instead.
This answer to a very similar question will probably help: Hibernate Search: How to use wildcards correctly?

Easy way to search values with dash (-) and space seperated in SQL

Hi i have many record in my db as
- above the line...
- above-the line...
- above-the-line...
- above the-line...
I want to query them with like query
Select words.* from words where sentence ILIKE 'above the line%';
But this query naturaly gets only first result.
Is there a easy way for ignoring dash without using OR with every possibilities?
Underscore is a wild card in sql, so try 'above_the_line'.
You can do as :
Word.where("sentence ~* ?", '^above[- ]the[- ]line').all

RegEx in SQL - How to Change the values of a column in SQL Server Database

I am trying to change the values of a column in my SQL Server Database.
Table: crm.activity
Column: Content
The values look like this:
value 1:
{\rtf1\ansi\ansicpg1252\deff0\deflang2055{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\f0\fs17 Appointment made\par
}
value 2:
{\rtf1\ansi\ansicpg1252\deff0\deflang2055{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\f0\fs17 Ask for offer\par
}
What I need is just the bold text.
Is it possible like using RegEx to go through all the columns and change the value???
To my knowledge, you can't do regex replaces in MySQL out of the box. However, if there's only one bold string per column, it should be easy enough to do the trick with string functions like LOCATE and SUBSTR.
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html
Here you go. This is for MySQL:
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('{\rtf1\ansi\ansicpg1252\deff0\deflang2055{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}} \viewkind4\uc1\pard\f0\fs17 **Appointment made**\par}', '**', -2), '**', 1);
# => Appointment made
Or to update (try it first and make sure you have a backup):
UPDATE mytable SET mycolumn = SUBSTRING_INDEX(SUBSTRING_INDEX(mycolumn, '**', -2), '**', 1);
If its SQL Server, do a bit of googling (mssql update replace regex) on how to add a regex replace method to SQL server. The regex you will need is: .+\\fs\d+\s(.+)\\par\s?}, replace this with $1. As a fallback solution (not sure how much data you have in that table) you could write a small script that selects all records, replaces the string, and updates the database.

Replace space with dash before save using Rails 3

I am trying to save a name to the database and a single word (firstname) works fine but when the user enter both firstname and lastname I want Rails to save it to the database as firstname-lastname instead of firstname lastname (space between).
I know I perhaps should use a before create filter but I am not sure how this need to look like. I want the validation to work to, i.e. no two people should be able to use the same name.
I am using Rails 3.
You can use ActiveSupport's inflector method parameterize on the string.
name = 'john smith' # => john smith
name.parameterize # => john-smith
Further, parameterize takes an option to use for the word-break, so you can replace the dash with an underscore like this:
name.parameterize("_") # => john_smith
An advantage of using parameterize is that it normalizes the characters to the latin, so...
name = "jöhanne såltveç"
name.parameterize # => johanne-saltvec
EDIT: As of Rails 5.0.0.1 the separator needs to be passed as an option. Therefore: name.parameterize(separator: '_')
Why don't you just have first_name and last_name columns in the db, and create your own validation rule to make sure the combination is unique (http://guides.rubyonrails.org/active_record_validations_callbacks.html#creating-custom-validation-methods). You should also create a unique index over those two columns in your db.
Another option would be to us regexp and replace all existing spaces with. You'd put something along the lines of:
self.firstname.gsub(/\s+/, '-')
in your model.
Note: I'm not sure if ruby accepts \s as "any whitespace character" And I think the * should make sure that if someone enters a name with two neighbour spaces(or more) it won't convert each space into a separate dash, but only into one.
Other answer is correct,
But, if you want to preserve case while parameterizing
name = "Donald Duck"
name.parameterize(preserve_case: true) # => Donald-Duck

Lucene query syntax

I want to write a Lucene query which is the equivalent of the following SQL
where age = 25
and name in ("tom", "dick", "harry")
The best I've come up with so far is:
(age:25 name:tom) OR
(age:25 name:dick) OR
(age:25 name:harry)
Is there a more succinct way to write this?
Thanks,
Don
age:25 AND name:(tom OR dick OR harry)
alternatively
+age:25 +name:(tom OR dick OR harry)
Does this work?
age:25 AND (name:tom OR name:dick OR name:harry)
I understand this may not be what you're looking for. I didn't know if the purpose of your question was to factor out the age:25 clause or if it was to eliminate the name: prefixes.
If you make name your QueryParser's default field, you could reduce this down to:
age:25 AND (tom OR dick OR harry)
It's not much more succinct, but you can try:
(age:25) AND (name:tom OR name:dick OR name:harry)