If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > truncating time

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-12-03, 19:47
bobwilli bobwilli is offline
Registered User
 
Join Date: Nov 2003
Posts: 7
truncating time

I am trying to query a tale containing a column with type datetime in the format yy-mm-dd hh:mm:ss

when displaying I should truncate the time. Should only get the yy-mm-dd as 'Mon dat, year' (ex. 'Jan 4, 2003) format.

Is it possible with the SQL itself. Atleast to truncate the time part?
Reply With Quote
  #2 (permalink)  
Old 11-12-03, 20:10
aus aus is offline
Registered User
 
Join Date: Oct 2003
Location: Denver, Colorado
Posts: 137
Re: truncating time

You can get the date formatted the way you want with the function DATE_FORMAT. Use it like this:

SELECT DATE_FORMAT(datetime_col, "%M %e, %Y") FROM table1;

Quote:
Originally posted by bobwilli
I am trying to query a tale containing a column with type datetime in the format yy-mm-dd hh:mm:ss

when displaying I should truncate the time. Should only get the yy-mm-dd as 'Mon dat, year' (ex. 'Jan 4, 2003) format.

Is it possible with the SQL itself. Atleast to truncate the time part?
Reply With Quote
  #3 (permalink)  
Old 11-12-03, 20:38
bobwilli bobwilli is offline
Registered User
 
Join Date: Nov 2003
Posts: 7
Re: truncating time

Quote:
Originally posted by aus
You can get the date formatted the way you want with the function DATE_FORMAT. Use it like this:

SELECT DATE_FORMAT(datetime_col, "%M %e, %Y") FROM table1;
thanks, it works if I query the db seperately.

But when I construct this into a SQL statement inside a jsp page, it is not.

How do I specify the format?.

my statement looks like this

<%
String dateStart = request.getParameter("dateStart") ;
String dateEnd = request.getParameter("dateEnd") ;
String sql = "select firstname, email, (date_format(loginpw.expires_dtime, %M, %D, %Y )), amount"
+ " from loginpw pw "
+ "left join payments pt on pw.loginpw_id = pt.loginpw_id "
+ " where expires_dtime >= " + dateStart
+ " and expires_dtime <= " + dateEnd ;
Result result = csuser.execute(csuser.newStatement(sql));

%>
Reply With Quote
  #4 (permalink)  
Old 11-13-03, 11:24
asherh asherh is offline
Registered User
 
Join Date: Jul 2003
Posts: 34
Hi,

You need to escape the quotes around the "%M, %D, %Y" value in your statement.

Try something like...

...String sql = "select firstname, email, (date_format(loginpw.expires_dtime, \"%M, %D, %Y\" )), amount"
+ " from loginpw pw "...

Chrs,
Ash
Reply With Quote
  #5 (permalink)  
Old 11-13-03, 18:25
bobwilli bobwilli is offline
Registered User
 
Join Date: Nov 2003
Posts: 7
Thanks it works.

Something more I need. How do I only show the first three letters of a month.

Now it shows 'November"

I ony need "Nov'

----

Quote:
Originally posted by asherh
Hi,

You need to escape the quotes around the "%M, %D, %Y" value in your statement.

Try something like...

...String sql = "select firstname, email, (date_format(loginpw.expires_dtime, \"%M, %D, %Y\" )), amount"
+ " from loginpw pw "...

Chrs,
Ash
Reply With Quote
  #6 (permalink)  
Old 11-14-03, 06:26
asherh asherh is offline
Registered User
 
Join Date: Jul 2003
Posts: 34
Hi,

You can replace...

%M

...with...

%b

This is the abbreviated month value.

http://www.mysql.com/doc/en/Date_and...functions.html

You may find it easier to use java's SimpleDateFormat class (or similar)?

Chrs,
Ash
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On