Quote field name and table name in Advantage database SQL - advantage-database-server

I am working on a legacy system developed with Advantage Database 8.1, I need to run SQL, like:
"SELECT CODETEST.DESC FROM CODETEST"
This SQL won't run at .NET due to the "CODETEST.DESC" name which is belong to SQL reserved keyword "DESC", I think to quote the "CODETEST.DESC", for example, in MySQL, we can use 'CODETEST'.'DESC' (I changed ` to ' here) to avoid that.
I read Advantage Database Help but can't find how to do so, it's a legacy system, and the database structure won't be changed. So is there any way to quote the table and field name in Advantage SQL?

Found the answer, here Use of Non-Standard Characters in Names, its simple by [CODETEST].[DESC]
Double quotes and [] (brackets) are used to delimit identifiers that
contain non-alphanumeric characters or that start with numbers. For
example, if a database contains a table name or column name that
starts with a number, contains spaces, or that has non-alphanumeric
characters, the application must enclose the name in double quotes or
[] (brackets) (e.g., "3D", "Contact Date", "l/c", [Full Name]). Also,
full path names or table names that include extensions must be
enclosed in double quotes or [] (brackets) (e.g., "x:\pathname\table",
"\server\volume\path\table", "table.abc", "..\otherdir\table").

Related

Neo4j - search like query with non english characters

Is there an option in neo4j to write a select query with where clause, that ignores non-latin characters ?
MATCH (places:Place)
WHERE (places.name =~ '.*(?ui)Fabergé.*')
RETURN places
I have place with Fabergé name in graph and i want to find it when user type Fabergé or Faberge without this special character.
I'm not aware of an easy way to do this directly with a regex match in Cypher.
One possible workaround is to store the string in question in a normalized form in a second property e.g. place.name_normalized and then compare it with the normalized search string. Of course normalization needs to be done on client side, see another SO question on how to achive this: Remove diacritical marks (ń ǹ ň ñ ṅ ņ ṇ ṋ ṉ ̈ ɲ ƞ ᶇ ɳ ȵ) from Unicode chars

Rails query by number of digits in field

I have a Rails app with a table: "clients". the clients table has a field: phone. phone data type is string. I'm using postgresql. I would like to write a query which selects all clients which have a phone value containing more than 10 digits. phone does not have a specific format:
+1 781-658-2687
+1 (207) 846-3332
2067891111
(345)222-777
123.234.3443
etc.
I've been trying variations of the following:
Client.where("LENGTH(REGEXP_REPLACE(phone,'[^\d]', '')) > 10")
Any help would be great.
You almost have it but you're missing the 'g' option to regexp_replace, from the fine manual:
The regexp_replace function provides substitution of new text for substrings that match POSIX regular expression patterns. [...] The flags parameter is an optional text string containing zero or more single-letter flags that change the function's behavior. Flag i specifies case-insensitive matching, while flag g specifies replacement of each matching substring rather than only the first one.
So regexp_replace(string, pattern, replacement) behaves like Ruby's String#sub whereas regexp_replace(string, pattern, replacement, 'g') behaves like Ruby's String#gsub.
You'll also need to get a \d through your double-quoted Ruby string all the way down to PostgreSQL so you'll need to say \\d in your Ruby. Things tend to get messy when everyone wants to use the same escape character.
This should do what you want:
Client.where("LENGTH(REGEXP_REPLACE(phone, '[^\\d]', '', 'g')) > 10")
# --------------------------------------------^^---------^^^
Try this:
phone_number.gsub(/[^\d]/, '').length

How to handle special characters like " ’ " when fetched from DB

Actually, i have an entry in my db of title "You’re Accepted to College, Now What: Deciding Upon...".
So when user types "you'are accepted...." it doesn't match with the above because of the special character ’.
What should be done for such type of characters.
Thanks in advance. :)
So in the case that you do have the apostrophe within your database in all places, so that there is no record that has a quote instead, I'd just correct the queries via gsub so that they contain the correct quote.
The true, nice and elegant solution would be to add a collation to your database server. The collation is what makes the database return 1 for SELECT 'ä' LIKE 'a'. Unfortunately, neither utf8_general_ci nor utf8_unicode_ci (which are built-in collations to MySQL) fold the apostrophe together with the quote. This is why you would have to add a new one. How this is accomplished in MySQL, you may find here: http://dev.mysql.com/doc/refman/5.0/en/adding-collation.html

Heroku/postgresql search

I have a database with names of cities that uses all different kinds of characters, from åäö to vietnamese signs. The place names are all in a capitalized format e.g. "New York" and "Aix En Provence".
My problem now is to try to search for these city names in Heroku which uses postgresql.
p = Place.where("upper(name) LIKE '%" + place_name_searched.to_s.upcase.tr('åäöüñï','ÅÄÖÜÑÏ') + "%'").first
This is the best I have come up with but it is only but a fix for åäö etc and doesn't catch all the 100+ different characters there is out there, e.g. "é". The solution is not to fill out that string any further.
A general problem is that upper(name) seems to translate everything fine but upcase can only handle english letters. So the correspoding search will be .where("TÄBY like 'TäBY') if place_name_searched = 'täby'.
So, how can I match postgresql entries such as "Täby" and "Jidd Ḩafş" to corresponding strings in downcase, entered by the user ("täby", "jidd hafs")?
It all works fine in my Mysql-version of the application but once I upload to Heroku it all fails.
Postgres has an extension called "ILIKE" for case insensitive pattern matching.
p = Place.where("upper(name) ILIKE '%" + place_name_searched.to_s.upcase.tr('åäöüñï','ÅÄÖÜÑÏ') + "%'").first
But please note this only works with Postgres, so you need to check which environment you are running on to decide if you would use this one instead of the regular one.

Prevent EF from escaping wildcard character

I have something like this
var query = repo.GetQuery(); // IQueryable
query.Where(item => item.FieldName.Contains("xxx%yyy"));
It results in following statement on SQL server
exec sp_executesql N'SELECT
// clipped
WHERE ([Extent1].[FieldName] LIKE #p__linq__0 ESCAPE N''~'')',
N'#p__linq__0 nvarchar(4000),#p__linq__0=N'%xxx~%yyy%'
#p__linq__0=N'%xxx~%yyy% causes the SQL server to look for xxx%yyy with % as literal (as it is escaped) while I would like it to match string like xxx123yyy, xxxABCyyy, xxxANYTHINGyyy, xxxyyy etc. Addition of prefix % and suffix % is fine but I could do it manually if needed.
In the above example I have simplified and written only one where condition but I have a dynamic logic that build the predicate with many of such keywords and I would like to allow the wildcards to be embedded inside the keywords. Is there a way to tell EF not to escape the % in the search keyword?
It is not possible. Contains("xxx") means that in SQL you want LIKE '%xxx%'. Linq-to-entities and none of its String mapped methods offer full wildcard searching = any wildcard character is always escaped. If you want to use wildcard searching you must use Entity SQL.

Resources