• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

select statement

 
prabhu kandanur
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
how can i write sql state to get date, need to check only month not entire date, say i want to retrieve the dob(date of birth) and name "whose month of birth is "May"

select name,dob where dob =?

thnks
prabhu
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhu,
Since this is a JDBC forum, here's how I would do it -- using the
SQL Escape Syntax:

Good Luck,
Avi.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35274
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhu,
I would be careful of using sql statements that rely on a date format as this is often database specific. Most databases give you a function that can do this. For example,


Edited to add that I read the link and it sounds like a good way to do it.
[ June 13, 2004: Message edited by: Jeanne Boyarsky ]
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne, did you read Avi's SQL Escape Syntax link? I didn't know that existed, but it looks like his way is how to do it in a vendor-neutral manner - the JDBC driver does the work of translating it into the propert date format. Seems cool.
 
tommy k. lee
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When writing database queries involving Dates, I'd prepare to use PreparedStatement, instead of Statements.

PreparedStatements have an API that lets to type-cast the Java Date type to the corresponding database Date / Timestamp type:

void setDate(int parameterIndex, Date x)

For instance:

PreparedStatement pstmt = con.prepareStatement("select * from table where lastModified < ?");

pstmt.setBigDecimal(1, new Date (System.curTimeMillis()));
pstmt.execute ();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic