A trouble with "LIKE" in a select statement
Hi!
I'm having trouble with "LIKE" in a select statement...
With Access I can make the following and everything works well:
SELECT name, birthday
FROM client
WHERE birthday LIKE '*/02/*';
but if try to do it in my application (it uses Access), it doesn't work - I just can't understand that!!!
In my application the "month" is always the currently month taken from the "System". Look what I'm doing...
String query1 = "SELECT name, birthday " +
"FROM client " +
"WHERE birthday " +
"LIKE '*/" +
pMonth +
"/*' " +
"ORDER BY birthday ASC ";
ResultSet rs = statement1.executeQuery(consulta1);
boolean moreRecords = rs.next();
The variable "moreRecords" is always "false", the query returns nothing although the table "client" has records that attend the query.
Please, anyone can help me?! It's a little bit urgent.
Thanks,
Katia.
Hi Katia,
I'll bet the problem lies with the characters you're using to escape the LIKE clause. You're using the ones that Access likes to see, but that's not necessarily what's built into the JDBC-ODBC driver class.
You can find out what the correct escape wildcard characters are from the java.sql.DatabaseMetaData.getSearchStringEscape() method. It'll tell you what to use in the LIKE clause.
I'm not 100% sure about your code. It doesn't use query1 anywhere. I'd do this:
String query = "SELECT name, birthday FROM client WHERE birthday LIKE ? ORDER BY birthday ASC";
PreparedStatement statement = connection.createStatement(query);
String escape = connection.getMetaData().getSearchStringEscape();
String test = escape + '/' + pMonth + '/' + escape;
statement.setString(1, test);
ResultSet rs = statement.executeQuery();
while (rs.hasNext())
// load your data into a data structure to pass back.
rs.close();
statement.close();Let me know if that works. - MOD
Similar Messages
-
Having trouble with put_line function in block statement..
Am a student, and the request does not appear to be in my book anywhere... Nothing on put_line, or block statements that I can see..
Need to write a block which will bring back via put_line for department 110: the department_id (departments table), the department_name (departments table), and the city in which the department is located (which must be pulled from locations table... can be joined by department_id). I can write the select statement with join, just not sure how to turn it into a block with put_line function..
any advice appreciated.. Thanks.Hi,
You need to format your code, so that it's easy to see what statements are in each section (DECLARE, BEGIN, etc.).
The compiler doesn't care about this, but it will help anyone who tries to read your code, including yourself.
Type {code} before and after formatted sections when posting messages on this site.
All statements, including each individual variable declaration) end with a semicolon.
If a query will return no more than one row, you can capture the results using an INTO-clause, right after the SELECT-clause. For every column in the SELECT-clause, there will be one variable in the INTO-clause.
Lists, including lists of table names in a query's FROM-clause, are delimited by commas.
In a query's FROM-clause, the real name of the table comes first, optionally followed by the alias you're using in the query.
I think this is what you're trying to do:
SET SERVEROUTPUT ON SIZE 50000
Declare
dep_id NUMBER;
Dep_string VarChar (100);
Begin
select d.department_id, d.department_name -- , l.city
into dep_id, dep_string
from departments d
, locations l
where d.location_id = l.location_id
and d.location_id = 110;
DBMS_OUTPUT.PUT_LINE (Dep_string);
END;
/No one should ever have this many errors at one time. Write code in much smaller increments, and test after each one. For example, you know you'll be using dbms_output.put_line, so start with something like:
SET SERVEROUTPUT ON SIZE 50000
BEGIN
dbms_output.put_line ('Hello, world!');
END;You may have problems if you forget semicolons, or SET SERVEROUTPUT. Solve those before you go any further.
Once the program above is working, add a little (and I mean a little) to it.
For example, you know you'll be printing a varibale, not a literal, so change it to a variable:
SET SERVEROUTPUT ON SIZE 50000
DECLARE
dep_string VARCHAR2 (100) := 'Hello, world!';
BEGIN
dbms_output.put_line (dep_string);
END;And so on.
Edited by: Frank Kulash on Dec 15, 2008 12:36 PM -
Where condition with optional fields in select statement
Hi,
In a function module I have to fetch data based on 7 fields out of which 6 are optional.
If we won't enter values for optional fields it will take default values as zeroes .
So this is not fetching data. But for some fields some values has zero values.
How to solve this condition?
Regards,
maha.Hi,
For those import parameters for which its mentioned as optional, use like condition in the where clause of select statement in the function module.
try like this,
concatenate pfield2 '%' into pfield2.
concatenate pfield3 '%' into pfield3.
concatenate pfield4 '%' into pfield4.
concatenate pfield5 '%' into pfield5.
concatenate pfield6 '%' into pfield6.
concatenate pfield7 '%' into pfield7.
select *
from db table
into itab
where field1 = pfield1 and
field2 like pfield2 and
field3 like pfield3 and
field7 like pfield7.
Now even if the optional fields are blank, data will be selected
Regards,
Vik -
Problem in using LIKE addition in SELECT statement
HI Experts,
Have a small query for you :
While selecting from Database I want to fetch all records which have the field name like "HR_".
I wrote following select query :
l_tsobj TYPE tst01-dname VALUE 'HR_%',
SELECT * INTO TABLE ltst01 FROM tst01 WHERE dname LIKE l_tsobj.
During debugging it shows me all records like HR_ & all records like HRL.
Not sure why it is fetching records like HRL from database .
In table tst01 total number of records having HR_ = 1231
but the above query select in total 1231 + 2000( records having HRL ..... pattern also ).
I want to restrict data selection at database level only & not at internal table level . Please suggest.
Thanks,
Abhinav.Hi,
you have to use ESCAPE statement.
DATA:
l_tsobj TYPE tst01-dname VALUE 'HR/_%'.
SELECT *
INTO TABLE ltst01
FROM tst01
WHERE dname LIKE l_tsobj ESCAPE '/'.
Regards Marcel -
Problem with LIKE operator in select query
Hi,
I want to compare one field using LIKE operator. But i m not able to do that.
DATA: l_doctext type c value ''''.
CONCATENATE l_doctext s_lbktxt-low '%' l_doctext INTO s_lbktxt-low.
SELECT bukrs belnr gjahr blart bldat budat xblnr bktxt waers awtyp
FROM bkpf INTO TABLE g_t_bkpf_labor
FOR ALL ENTRIES IN g_t_bseg_unique
WHERE bukrs = g_t_bseg_unique-bukrs
AND belnr = g_t_bseg_unique-belnr
AND gjahr = g_t_bseg_unique-gjahr
AND blart = p_ldtype
AND bktxt LIKE s_lbktxt-low.
plz help. Am i dng something wrong.
Value in s_lbktxt can be like 'TEA.....'hi ,
check this program..
tables:mara.
data: begin of it_mara occurs 0,
matnr like mara-matnr,
meins like mara-meins ,
mtart like mara-mtart,
end of it_mara.
select-options: s_matnr for mara-matnr.
select matnr
meins
mtart
from mara
into table it_mara
where matnr like '%7'.
sort it_mara by matnr.
loop at it_mara.
write:/ it_mara-matnr,
it_mara-meins,
it_mara-mtart.
endloop.
regards,
venkat. -
Group by function with CLOB fields in Select statement
I have the following sql trying to query our database. Removing the Clob field (a.description) I am successful. But I need the Clob field (a.description). Here is my current sql
select a.id_number, a.id_owner, MAX (n.next_action_required)
KEEP (DENSE_RANK LAST ORDER BY n.modified_date),
MAX (n.modified_date) KEEP (DENSE_RANK LAST ORDER BY n.modified_date)
from table1 a, table2 n
where a.id_number = n.id_number
group by a.id_number, a.id_ownerI tried the following which works sometimes but not always because the Clob field (a.description)'s length varies; anywhere from 10 characters up to 12,000 characters. I get some blanks with the following and some where text is cut off. I may not be writing the dbms_lob.substr function correct.
select a.id_number, a.id_owner, MAX (n.next_action_required)
KEEP (DENSE_RANK LAST ORDER BY n.modified_date),
MAX (n.modified_date) KEEP (DENSE_RANK LAST ORDER BY n.modified_date) , DBMS_LOB.SUBSTR (a.description, 8001, 10000)
from table1 a, table2 n
where a.id_number = n.id_number
group by a.id_number, a.id_owner, DBMS_LOB.SUBSTR (a.description, 8001, 10000)Please help. Thank you,Maybe,
but throw it away if the combination a.id_number,a.id_owner,n.next_action_required(a.id_number = n.id_number),n.modified_date(a.id_number = n.id_number) is not unique
select nd.id_number,
nd.id_owner,
nd.next_action_required,
nd.modified_date,
a.description
from (select a.id_number,
a.id_owner,
MAX (n.next_action_required) KEEP (DENSE_RANK LAST ORDER BY n.modified_date) next_action_required,
MAX (n.modified_date) KEEP (DENSE_RANK LAST ORDER BY n.modified_date) modified_date
from table1 a,
table2 n
where a.id_number = n.id_number
group by a.id_number,
a.id_owner
) nd,
table1 a
where nd.id_number = a.id_number
and nd.id_owner = a.id_owner
and nd.id_number = (select id_number
from table2
where next_action_required = nd.next_action_required
and modified_date = nd.modified_date
)Regards
Etbin -
I am having trouble with Powerpoint, when I select slideshow mode the program shuts down
Can someone help me? I am having troube with Powerpoint. When I select the slideshow mode the program shuts down.
Assuming you are using Office for Mac, you may want to ask your Office for Mac in the Office for Mac forums: http://answers.microsoft.com/en-us/mac?auth=1.
-
Case insensitive selects with 'like'
I use a 10g database (10.2.0.2 same behaviour with 10.2.0.1). What we want is case insensitive selects with 'like' operator in the where clause. NLS_COMP is set to 'LINGUISTIC' and NLS_SORT is set to 'BINARY_CI.
In a table we have two columns one of type 'varchar2' one of type 'nvarchar2'. The databases national character set is set to UTF8. Case insensitive sorting works with both columns. Select statements with '.... where varchar2col like '%r%' returns also values with upper case 'R' values (that is what I expect).
The select statements with '.... where nvarchar2col like '%r%' however does not return the row with upper case 'R' values.
I used SQL*Plus: Release 10.2.0.3.0 and other clients and the behaviour is the same so
I think it is not client related.
Is that a known issue or is there any other parameter to set for UTF8 nvarchar columns?
Any hint is very much appreciated! Here are the nls settings in database, instance and session:
DPARAMETER DVALUE IVALUE SVALUE
NLS_CHARACTERSET WE8ISO8859P1
NLS_COMP BINARY LINGUISTIC LINGUISTIC
NLS_LANGUAGE AMERICAN AMERICAN AMERICAN
NLS_NCHAR_CHARACTERSET UTF8
NLS_RDBMS_VERSION 10.2.0.1.0
NLS_SORT BINARY BINARY_CI BINARY_CI
NLS_TERRITORY AMERICA AMERICA AMERICAOK. Found out what the problem is. It is obviously the client.
While using the instant client and setting the parameters (NLS_SORT=BINARY_CI and NLS_COMP=LINGUISTIC) as environment variables does not work correctly, at least not for nvarchar2 fields. The nls_session_parameters show the correct values, but selects with 'like' operators in the where clause do not return case insensitive. Issuing the 'alter session' commands and again setting the nls parameters solves the problem.
Using the full client installation also works, in case the parameters are set in the registry on windows systems. With the full client it it not necessary to issue the 'alter session' commands again.
So obviously the problem is instant client and nvarchar2 field related. That's too bad.... -
Hi,
we are running a query with a big dunamic select statement from VB code using ADO command object. When Execute method is called system hangs and control won't return back to the application. it seems to be that there is some type limitation on Query string length. Please tell us if there is any?
we are running Oracle 8.1.7 Server on Windows 200 Server and connecting from a W2K professional, ADO 2.6 and Oracle OLEDB 8.1.7.1 OLEDB Driver.
Sample code:
Dim rs As ADODB.Recordset
Dim cmd As ADODB.Command
Set cmd = New Command
With cmd
.CommandText = ' some text with more than 2500 characters
.CommandType = adCmdText
Set rs = .Execute
End With
when i debug using VB6 and when .Execute line is called system hangs or return a message method <<somemethod> of <<some class name>> failed error.
Any help is appreciated.
Thanks,
AnilA stored procedure would only slow you down here if it was poorly written. I suspect you want to use the translate function. I'm cutting & pasting examples from the documentation-- a search at tahiti.oracle.com will give you all the info you'll need.
Examples
The following statement translates a license number. All letters 'ABC...Z' are translated to 'X' and all digits '012 . . . 9' are translated to '9':
SELECT TRANSLATE('2KRW229',
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'9999999999XXXXXXXXXXXXXXXXXXXXXXXXXX') "License"
FROM DUAL;
License
9XXX999
The following statement returns a license number with the characters removed and the digits remaining:
SELECT TRANSLATE('2KRW229',
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789')
"Translate example"
FROM DUAL;
Translate example
2229
Also, LIKE '%<string>%' is going to be rather expensive simply because it has to compare the entire string and because it forces full table scans, rather than using indexes. You could speed this sort of query up by using interMedia Text (Oracle Text now in 9i). If you can eliminate one of the '%' options, you could also improve things.
My guess is that your stored procedure is inefficient and that's causing the problem-- 5k rows per table should be pretty trivial.
If you post your query over on the PL/SQL forum, there are better performance tuners than I that might have more hints for you. To get really good advice, though, you'lllikely have to get at least the execution plan for this statement and may need to do some profiling to identify the problem areas.
Justin -
Can't concatonate more than three items in a portal select statement ?
I'm running a Report from SQL query
as Far as I can tell you can't do;
select 'blah1'||name||'blah3'||'blah3'
from scott.emp
as soon as you put in a 4th item it fails.
It also fails using select concat (,)
I'm trying to run a report which selects a web address stored in a database table ie. http://www.yahoo.com and wrap it in html tags so it will appear as a link.
I've got to do it with more than three items because the only way to display the < in <ahref is to quote it seperately.
ie
'< '||'ahref='||ADDRESS FROM DATABASE||etc.etc.
anyone any ideas?
should i do it another way?Let me explain a bit further.
If I want to display;
www.yahoo.com as a link I need add the html tags
Yahoo
www.yahoo.com is an entry in a database.
I want to select the entry www.yahoo.com from the database and concatonate it with the html tags.
ideally;
select 'Visit
This does not work
To concatonate characters like > in a select statement within portal you need to concat. seperately;
select '< '||'a href="'||ADDRESS FROM DATABASE||'"> '||'Visit< '||'/a> '
This does not work as you can't concat more than three items.
Could I write a trigger to add in the html tags to the data stored in the database, when data is entered via a portal form?
Again my problem is In need to add text to the start and end of the data being stored ie.
user enters www.yahoo.com in a form
a trigger or two converts that into;
Visit -
Error by using database procedure in select statement
hi ,
I have built a database procedure having one parameter with in out varchar type. that return value with some addition.
i am using it with some column in select statement only for display purpuses but i am facing error by doing that in select statement. that procedure is working well with bind variable but not with select statement.
plz help me how i can use a procedure in select statement. or can i do it or not.plz help me how i can use a procedure in select statement. or can i do it or not.A workaround could be to create a wrapper function for your procedure. One that only passes the input parameters and returns the output parameter.
The simply call this function in your select which internally calls the procedure. -
Explain plan: "Select statement"
Hi,
I'm using the explain plan command to retrieve information about query execution. In particular I want to estimate query execution time.
Let's consider the following example:
"| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |"
"| 0 | SELECT STATEMENT | | 4775 | 484K| 98886 (1)| 00:19:47 |"
"| 1 | HASH GROUP BY | | 4775 | 484K| 98886 (1)| 00:19:47 |"
"|* 2 | MAT_VIEW ACCESS FULL| Materialized_view1 | 4775 | 484K| 98884 (1)| 00:19:47 |"
In the calculation of total execution time should i consider the "select statement" operation?
The total execution time can be calculated like this: Time(Select statement) + Time(hash group by) + Time(access full)=19.47*3=58.41. Is it right?
ThanksNo, the expected time is 00:19:47. No time is expected to be spent in steps 0 and 1.
-
Trouble with city, state in Mysql Query
Hello,
I am having trouble with a query I am trying to setup. Basically, I want the user to type in their city. Then I want the system to ask them which city,state combo is theirs. So if they enter "Washington" the system will return:
Select your city/state:
Washington, NJ
Washington, PA
Washington, DE
City and state are stored as two fields in my mysql DB. I was considering using 'select distinct' but I wasnt sure how it would work in relation to two fields. So how would I go about making sure washington, nj only appears once (its in a database where there will be multiple washington, NJ listed).
So anyone have any ideas or code samples they have used to do something like this?
Thanks in advance,
Markdavella wrote:
City and state are stored as two fields in my mysql DB. I was considering using 'select distinct' but I wasnt sure how it would work in relation to two fields. So how would I go about making sure washington, nj only appears once (its in a database where there will be multiple washington, NJ listed).
DISTINCT works the same way for single field or multiple fields. It returns all distinct combinations of the values in the columns you have listed.
SELECT DISTINCT City, State
FROM Table
WHERE City = 'Washington'
(Data) City, State
Washington, NJ
Washington, PA
Washington, NJ
Washington, DE
Washington, NJ
Washington, PA
So for your two columns it would distinct combinations of the "City" and "State" values. But why are there multiple "Washington, NJ" records? Are they actually different cities or duplicate records?
(Results)
Washington, NJ
Washington, PA
Washington, DE -
Use of LIKE in where clause of select statement for multiple records
Hi Experts,
I have a account number field which is uploaded from a file. Now this account numbers uploaded does not match fully with sap table account numbers but it contains all of the numbers provided in the file mostly in the upright positions.
For example in file we have account number as 2ARS1 while in sap table the value is 002ARS1.
And i want to fetch data from sap table based on account number uploaded. So, i am trying to use LIKE with for all entries but its not working as mentioned below but LIKE is not working with FOR ALL ENTRIES.
data : begin of t_dda occurs 0,
dda(19) type c,
end of t_dda.
data : begin of t_bukrs occurs 0,
bukrs type t012k-bukrs,
end of t_bukrs.
data : dda type t012k-bankn,
w_dda type t012k-bankn.
CONCATENATE '%'
'2ARS1'
INTO W_DDA.
MOVE W_DDA TO T_DDA-DDA.
APPEND T_DDA.
CLEAR T_DDA.
free t_bukrs.
SELECT BUKRS
FROM T012K
into TABLE t_bukrs
for all entries in t_dda
WHERE BANKN like t_dda-dda.
Can anybody suggest what should i use to get the data for multiple account numbers using one select statement only instead on using SELECT UP TO 1 ROWS in LOOP....ENDLOOP ?
Thanks in advance,
AkashHi,
yes, For All entries won't work for LIKE with '% '.
I think the other alternative is go for Native SQL by writing sub-query
sample code is here:
data: begin of i_mara occurs 0,
matnr like mara-matnr,
matkl like mara-matkl,
end of i_mara.
exec sql.
select matnr, matkl from mara where matnr in (select matnr from marc) and matnr like '%ma' into :i_mara
endexec.
loop at i_mara.
write:/ i_mara-matnr, i_mara-matkl.
endloop.
hope u got it.
regards
Mahesh
Edited by: Mahesh Reddy on Jan 21, 2009 2:32 PM -
I m trying to find if LIMIT is a part of the select statement and if works like
set rowcount 1 in sybase....if anyone can post the generic select statement with LIMIT in it that would be great!
any help or input will be greatly appreciated
Thanks!Hallo,
i don't know sybase, but it is probably rownum
select * from scott.emp
where rownum < 3
Regards
Dmytro
Maybe you are looking for
-
Oracle application server release 2
hi i am trying to download oracle 10g application server Release2 from the link http://www.oracle.com/technology/software/products/ias/htdocs/101202.html but everytime when it starts downloading,it remains on 0% only.. is there any other link by wich
-
Can Printer over network but can't scan over network with Officejet 6500 E710n
Got an odd problem... I can print all day via my wireless network, I can even ping my printer's IP with no issues. I can't scan at all, it keeps giving me connection related error messages (Scanner Unreachable, etc). I checked the "Update IP Address"
-
Inkjet duplex printing -- okay to print on blank side of previously printed sheet?
I have a new F4580 Deskjet with manual duplexing. I would like to recycle paper by printing on the blank side of previously printed paper. Is it okay to print on the blank side of an inkjet-printed sheet? How about a laser-printed sheet? This means
-
I deleted iPhoto from my macbook (mid 2007, msc os 10.5.8) more than a year ago. Now I extremely need it , but could not recover it. Is here a way for some how to download the appropriate iPhoto version for my macbook?
-
Can I hook my i- Mac core 2 duo monitor to a PC netbook, so the Mac becomes the monitor?
Is it possible to hook my i- Mac core 2 duo monitor to a PC netbook, so the Mac becomes the monitor and the PC the computer?