trim whitespaces in oracle where condition - sql

I'm facing the following issues when i tried to retrieve the data from oracle database by trimming the whitespace. I'm using oracle 12G version.
Let's say the name column has some whitespaces like 'Test '.
Table name - PGM_DETAIL
ID NAME
1 Test
When I'm trying to fetch the data using following query. It's not fetching it.
SELECT * FROM PGM_DETAIL WHERE TRIM(NAME) = TRIM('Test');
Could you anyone please suggest me what the problem in my sql statement.
Thanks in advance...,

Oracle comparisons are case-sensitive by default.
So try:
WHERE UPPER(TRIM(NAME)) = TRIM('TEST');

try
SELECT * FROM PGM_DETAIL WHERE TRIM(NAME) = TRIM('Test');

Related

Error show when update using phpmyadmin

When I update a column using phpmyadmin in database with following query
UPDATE members
SET `refered` = (SELECT COUNT (*)
FROM `user_details`
WHERE `user_details.sponser`=`members.username`
)
It show a error message like this
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '*) FROM `user_details` WHERE `user_details.sponser`=`members.username`)' at line 1
What may be reason?
Error is in
COUNT (*)
-----^
Remove the space between COUNT and (*).
Try the below query
UPDATE members SET refered = (SELECT COUNT(*) FROM user_details
WHERE user_details.sponser=members.username)
Does the Select query returns any result. If so what is the result. Looks like all your query is inside '' single quotes that you are using it should be removed. Single quotes need to be removed for example .
UPDATE members
SET refered = (SELECT COUNT (*)
FROM user_details
WHERE user_details.sponser=members.username
)
-- there is not single quotes in the query above. please remove it from yours.
Part of your problem, or maybe the whole problem, is the WHERE clause. You've used backticks for the table name, which is correct (or, at least, it's optional in this case; it's needed if your database name or table name has a MySQL reserved name or is otherwise ambiguous). The problem, though, is that the dot separating the database from the table needs to be outside the backticks. So your WHERE clause should look like this instead:
WHERE `user_details`.`sponser`=`members`.`username`

Replacing values with wildcards (parsing text data)

I have rows which contains HTML tags. e.g.
<b>Abc</b> <strong>Bca</strong>
So I need to cut it out. As I suggest I need to find something like '%<%>%' and make a REPLACE to ''.
How can I do it? Interested for both solutions - MS SQL & Oracle also.
Assuming table is called yourtable and field is called htmltag.
In SQL Server:
SELECT
SUBSTRING(substring(htmltag,charindex('>',htmltag)+1,250),0,CHARINDEX('<',
substring(htmltag,charindex('>',htmltag)+1 ,250),0))
FROM yourtable
SQL FIDDLE
In Oracle
SELECT regexp_replace(htmltag, '<[^>]+>', '') htmltag
FROM yourtable
SQL FIDDLE

Hibernate - Raw Query execution_KEY Words Issue in query?

The setup consists of Hibernate 3. Am trying to execute the raw query as it is. The setup works fine for other simple queries , db inserts & updates.
The query in issue is :
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 29 [
SELECT keyMain, value FROM (select distinct K.[key] as keyMain,
( SELECT value FROM com.trans.dto.Resources as L WHERE L.[key] = K.[key]
and L.lang_code = 'A11' ) as value from com.trans.dto.Resources as K )
as test order by keyMain ]
Resources is the table & has mapping setup in hibernate.cfg.xml
I was under a thought "KEY" is name of one of the column which can not be changed. How do i escape key words ?
If not 1, then is the multi selects in sub query.
Please advise. Any suggestion is of great help.
From here:
You can force Hibernate to quote an identifier in the generated SQL
by enclosing the table or column name in backticks in the mapping document.
Hibernate will use the correct quotation style for the SQL Dialect.
This is usually double quotes, but the SQL Server uses brackets and MySQL uses backticks.
So, try to escape your field with double quotes or with square parenthesis('[key]').

How to get correct query in sql when field of coloumn has apex inside

i'm trying to do this query:
SELECT *
FROM TABLE
WHERE value1='THIS'S AN EXAMPLE'.
The problem is that apex inside content will give error on query. how can i solve it? can someone help me?
Try escaping it like that:
SELECT *
FROM TABLE
WHERE value1='THIS''S AN EXAMPLE'
where '' is 2 single quotes.
But best would be to make this a parametrized query. If you're running it from your code.
In oracle you use '' instead of one ':
SELECT *
FROM TABLE
WHERE value1='THIS''S AN EXAMPLE'.
In mysql you need to escape it with \'
read this: http://dev.mysql.com/doc/refman/5.0/en/string-literals.html

How can I update many rows with SQL in a single table?

I have a table and one of the columns holds web addresses like: 'http://...' or 'https://...'.
The problem is that there are some invalid entries, like 'shttp://...' or '#http//...' (the first character is invalid) and I want to correct all of them.
I use the following SQL statement:
'SELECT [...] FROM MyTable WHERE WebAddress LIKE '_http%'
and I successfuly get the problematic rows.
But how am I going to change/correct all of them using an UPDATE statement?
If you have some other solution please share it!
Simply change the SELECT to an UPDATE (of course, with some syntax changes) with a "fix" expression
UPDATE
MyTable
SET
WebAddress = SUBSTRING(WebAddress, 2, 8000)
WHERE
WebAddress LIKE '_http%'
You Can use Sub string property as you can trim odd letters .Also like '_word start' suitable for your question