Forum Moderators: open

Message Too Old, No Replies

DATE FORMAT within a Select *?

         

neophyte

5:42 am on Dec 31, 2006 (gmt 0)

10+ Year Member



Hello all -

I've got a basic query that selects ALL records (Select *) from a particular table, but what I also want to do is to format 2 date fields from #*$!X-XX-XX to January 1, 2006... but I'd like to somehow do this within the Select * query.

I've tried different combinations, like:

Select *
(CheckIn, '%M %e, %Y') as CheckIn
from reservations

...but always get errors back.

The only way I can get it to work is to create a query what individually selects each column name, like:

Select
reservationid, firstname, lastname, (CheckIn, '%M %e, %Y') as CheckIn, roomcategory, roomnumber, roomrate, season
from reservations

But, yuck, that's a lot of stuff to write when one would think it could be done more efficiently.

Any ideas/syntax that would get me a desired date format within a simple Select *?

Neophyte

justageek

2:08 pm on Dec 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks like your missing the 'date_format. Try this: Select *, date_format(CheckIn, '%M %e, %Y') as CheckIn from reservations

The problem though is that while you can do what you want to do you will get back 2 columns named 'CheckIn'. One will be formatted the way you want and the other will not so which one will you use?

Your best bet would be to specify all the columns you want or if you really do need all the columns then format the date after you get it back from the db.

JAG

neophyte

8:41 am on Jan 1, 2007 (gmt 0)

10+ Year Member



JAG -

I accidently left off the DATE_FORMAT part in the post, but did have that in the code. I'll try your suggestion and maybe alias that additional date field as CheckInFormatted or some such.

I've wondered about formatting that particular result column after it's already in the set, but can't find a built in (yes, I could write a special function myself but at this point would just prefer not to) php function that would convert an ISO date (2006-01-01) into a user friendly format (January 1, 2006). Maybe I'll throw that into the php forum and ask if anyone knows of away to do this after the information is already in a result set as that would solve all my problems.

But, I'll try your suggestion as well; appreciate the suggestion.

Neophyte