While it is easy to do replace of the the search text in the code after getting the dataset from an sql with a where clause:
x = x.Replace("SearchText","<span style='color:RED'>SearchText</span>");
I was wondering if there was a way to this IN the SQL.
Select x.Replace("SearchText","<span class='highlight'>SearchText</span>") as x from t where x like '%SearchText%'
or something like that.
The reason I am asking is because I do a:
COALESCE(LastName + ', ' + FirstName, LastName, FirstName) as Name
and I don't want a returned Name field of "Bobbly, Bob" to get two highlighted areas when searching for a LastName that includes "Bob" or a FirstName that includes "Bob" (Noting that first and last names have different search phrases"
Yeah, I could just return the two field separately and join them in code, but I just want to see if it can be done in SQL.
T-SQL includes a Replace Function
REPLACE ( string_expression , string_pattern , string_replacement )
That should do what you need
COALESCE(REPLACE(FirstName,'SearchText1','<span class=''highlight''>' + 'SearchText1' + '</span>')+','
+REPLACE(LastName,'SearchText2','<span class=''highlight''>'+'SearchText2'+'</span>')
,REPLACE(FirstName,'SearchText1','<span class=''highlight''>' + 'SearchText1' + '</span>')
,REPLACE(LastName,'SearchText2','<span class=''highlight''>'+'SearchText2'+'</span>')
,'')
Related
I am trying to format the information from a column that I am querying and compare that to information in a cell. I have tried to hack together various ways to do this, but I am not a proficient SQL/spreadsheet user.
In COLUMN I there is nothing.
In COLUMN K there is a match on A2.
In COLUMN N there is Information formatted like 31'-40' and 41'+.
I would prefer to use = instead of contains.
The REPLACE Function seems to work when I substitute N for a String and run it on the W3 School Website.
The REGEXREPLACE seems to work on D2. I would expect them to match, but they do not.
COUNT( QUERY( '2019'!A2:P, "select D where I='' and upper(K) contains '" & UPPER(A2) & "' and REPLACE(REPLACE(REPLACE(N, '-', ''), '''', ''), '+','') contains '"& Regexreplace(D2,"[[:punct:]]","") &"' ")
I get 0 matches.
you almost had it, but try like this:
=COUNTA(FILTER(2019!D2:D, I2:I="",
REGEXMATCH(UPPER(K2:K), UPPER(A2)),
REGEXMATCH(UPPER(N2:N), UPPER(D2))))
If Phone has "918-435-0000" and want to remove"-" result should be - 9184350000, I can use substring with position 4 and 8? or shall I be using replace function ?
here in my example are special characters like - or sometimes +1(505) 000-2798
You should provide more details on your attempts instead of statements like just its not working.
Does the following work for you?
with t(phone1) as (values
'+1(505) 000-2798'
, '918-435-0000'
, '9184350000'
)
select phone1, REGEXP_REPLACE(phone1, '[^\d]', '') phone1_replaced
from t
where REGEXP_LIKE(phone1, '[^\d]');
I have 100000 lines as such:-
/** http://www.marinetraffic.com/en/ais/details/ships/8720175/vessel:SEMUTIK_NO_1 **/INSERT INTO `vessel`(id,name,`imo`, `flag`, `type`, `speed`, `callsign`, `tonnage`, `length`, `deadweight`, `year`, `status`, `draught`,mmsi) VALUES (27797,'SEMUTIK_NO_1'','8720175','-','Fishing Vessel','N/A','-','152','0m x 0m','0','1974','Active','0m',-8720175)
^
I've generated these queries and saved it in various files.However now i found that there is a problem with the query and it will be taking me a lot of time to regenerate the query.See the name value in the query, there is an extra quote. I wanna know a regex to find and remove it, mostly in Java or even php.
Just open your file with queries in a text editor of your choice (Sublime, TextMate, vim, what have you) and issue find and replace '', for ',
you can try this,
String str = "INSERT INTO vessel(id,name,imo, flag, type, speed, callsign, \n" +
"tonnage, length, deadweight, year, status, draught,mmsi) \n" +
"VALUES (27797,'SEMUTIK_NO_1'','8720175','-','Fishing Vessel',\n" +
"'N/A','-','152','0m x 0m','0','1974','Active','0m',-8720175)";
System.out.println(str.replaceAll("''", "'"));
For example we have a large database contains lots of oracle packages, and now we want to see where a specific table resists in the source code. The source code is stored in user_source table and our desired table is called 'company'.
Normally, I would like to use:
select * from user_source
where upper(text) like '%COMPANY%'
This will return all words containing 'company', like
121 company cmy
14 company_id, idx_name %% end of coding
453 ;companyname
1253 from db.company.company_id where
989 using company, idx, db_name,
So how to make this result more intelligent using regular expression to parse all the source lines matching a meaningful table name (means a table to the compiler)?
So normally we allow the matched word contains chars like . ; , '' "" but not _
Can anyone make this work?
To find company as a "whole word" with a regular expression:
SELECT * FROM user_source
WHERE REGEXP_LIKE(text, '(^|\s)company(\s|$)', 'i');
The third argument of i makes the REGEXP_LIKE search case-insensitive.
As far as ignoring the characters . ; , '' "", you can use REGEXP_REPLACE to suck them out of the string before doing the comparison:
SELECT * FROM user_source
WHERE REGEXP_LIKE(REGEXP_REPLACE(text, '[.;,''"]'), '(^|\s)company(\s|$)', 'i');
Addendum: The following query will also help locate table references. It won't give the source line, but it's a start:
SELECT *
FROM user_dependencies
WHERE referenced_name = 'COMPANY'
AND referenced_type = 'TABLE';
If you want to identify the objects that refer to your table, you can get that information from the data dictionary:
select *
from all_dependencies
where referenced_owner = 'DB'
and referenced_name = 'COMPANY'
and referenced_type = 'TABLE';
You can't get the individual line numbers from that, but you can then either look at user_source or use a regexp on the specific source code, which woudl at least reduce false positives.
SELECT * FROM user_source
WHERE REGEXP_LIKE(text,'([^_a-z0-9])company([^_a-z0-9])','i')
Thanks #Ed Gibbs, with a little trick this modified answer could be more intelligent.
A bit of a confusing title, but I'll try to be clear here. When using the query object inside of <cfscript> and you have some sql in the form:
SELECT city + ', ' + state + ' ' + zip as Address2
FROM users WHERE user_id = :userid
ColdFusion will error out. :userid is the parameter that I added using addParam and the query works perfectly when I remove the ' ' between state and zip. For some reason adding that space is causing the query object go out of whack and it gives me incorrect syntax near the : symbol.
This query also works fine when I simply use <cfquery>, but I would like to use it in <cfscript>. Any ideas? Is this a bug? or am I missing something?
Edit: I'm using Coldfusion 9, MS SQL 2005 and this is being done inside of a CFC with the cfscript syntax. Like so:
component
{
public function getAgent(member_id)
{
qryAgent = new query(dataSource="Members");
qryAgent.setName("get_agent");
qryAgent.addParam(name="memberid",value=member_id,cfsqltype="CF_SQL_INTEGER");
result = qryAgent.execute(sql="SELECT FirstName, LastName, FirstName + ' ' + LastName as FullName FROM Member m WHERE m.member_id = :memberid");
return result.getResult();
}
}
Your syntax, from the code that is posted, looks solid for MS SQL. I ran the following test on my local box with ACF 9 and MS SQL Server 2008 installed (I know you're on 2005 but it should be identical for something like string concatenation).
<cfscript>
qry = new query();
qry.setDatasource("mydsn");
qry.setName("myqry");
qry.addParam(name="userid",value="3735",cfsqltype="cf_sql_integer");
result = qry.execute(sql="SELECT firstname + ' ' + lastname AS fullname FROM mydb where userid = :userid");
writeDump(result);
</cfscript>
Running this in a browser returns a "fullname" value that is correctly concatenated.
By chance, does this code snippet help uncover anything unusual in your full cfscript query? If not, is it possible for you to post more of your code as it relates to setting up the query service?
EDIT: I also ran it with the following 'result =' expression to make it more like the concatenation you're doing:
result = qry.execute(sql="SELECT firstname + ', ' + lastname + ' ' + email AS fullname FROM mydb where userid = :userid");
After debugging and finding the answer I found that someone else had already found this here: http://forums.adobe.com/thread/683656?tstart=-4 and adobe has fixed it in CF 9.0.1
The problem comes in the query.cfc file that is in the folder ColdFusion9\CustomTags\com\adobe\coldfusion\query.cfc. The replaceDelimsWithMarkers function has a call to listtoarray (line 346 in my version). This call doesn't have the 3rd argument includeEmptyFields set to true which is causing my sql string to get mangled.
This can be manually fixed by adding the third argument of true.