[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Database java date query

hello there,

i have this code below where i get todays data and want to query the databse to return all the values for today.but it does not return anything.
Timestamp  todayDate = null;
        String currentDate = null;
          SimpleDateFormat df = new SimpleDateFormat( "dd/MM/yy HH:mm:ss" ) ;
        java.util.Date d = new java.util.Date() ;
        try
        {
          currentDate = df.format( d ).toString();
          java.util.Date oDate1 = df.parse(currentDate);
                  long tf1 = oDate1.getTime();
                  todayDate = new Timestamp(tf1);
        }
        catch( Exception e )
        {
          System.out.println( e);
        }
        String dslamLinecard= "SELECT * FROM Bookht_User_Session_Action WHERE ACTION_TIME= " +todayDate;
0
zolf
Asked:
zolf
  • 5
  • 4
  • 2
1 Solution
 
CEHJCommented:
If the field is a timstamp, you probably need to query *between* times or you will almost certainly get nothing
0
 
objectsCommented:
try something like:

SELECT * FROM Bookht_User_Session_Action WHERE DATE(ACTION_TIME)=?

exact format will depend on your database
0
 
zolfAuthor Commented:


ok then how do i get today's date and query the db to return all the rows with todays date.
please help
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
zolfAuthor Commented:

i am using sql server 2005.
objects:
'DATE' is not a recognized function name.
0
 
objectsCommented:
http://www.sql-server-helper.com/functions/get-date-only.aspx

you can also use GETDATE() in your query instead of having to build parameter.
Simpler and more efficient
0
 
zolfAuthor Commented:

i tried this but still nothing is returned
SELECT * FROM Bookht_User_Session_Action WHERE ACTION_TIME= getdate()
0
 
objectsCommented:
that because you still need to convert it to a date (as the times will not be the same as the current time)
0
 
objectsCommented:
another option would be to use convert to convert ACTION_TIME and GETDATE() to a date string and compare the strings

http://www.sql-server-helper.com/tips/date-formats.aspx
0
 
zolfAuthor Commented:

for some reason i cannot open these links which you provide.can you please give me the code example.appreciate your help.
i just need to get todays date and query db to return all the rows
0
 
objectsCommented:
try:

... where convert(VARCHAR(8), ACTION_TIME, 1) = convert(VARCHAR(8), GETDATE(), 1)
0
 
CEHJCommented:
Not a good idea to use db-specific code. It will make your code unportable and there's no need for it anyway
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now