Solved

query syntax

Posted on 2013-06-02
4
107 Views
Last Modified: 2013-06-10
How do I proper write the following:

This works:
SELECT date_format(value,'%a %c/%e') AS end_date FROM....

Open in new window

This does not:

SELECT magentocatalog_category_entity_datetime.date_format(value,'%a %c/%e') AS end_date FROM...

Open in new window


How do I write the second code example the correct way?
0
Comment
Question by:rgranlund
  • 2
4 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39214772
"Works" or "does not" isn't very revealing.  Please complete the SSCCE and show us some code we can copy and experiment with.  You might include a CREATE TEMPORARY TABLE statement and load some data into the table so we can copy the example, install it on our servers and run the queries.  Then we can show you how to get MySQL to reveal the error messages and when we see the messages we can show you how to find the solutions in the MySQL online documentation.

I'm going to take a guess on this one... but something we can test with will almost certainly get you a better response, faster.

SELECT 
DATE_FORMAT(magentocatalog_category_entity_datetime.value,'%a %c/%e') AS end_date 
FROM magentocatalog_category_entity_datetime

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39215089
date_format() is a MySQL function, and functions require the parameters inside the parentheses that follow the function. (Just like Ray has used above.)

so when you see something like this:
SELECT date_format(value,'%a %c/%e')

the first parameter: "value" is where you would include the field
the second parameter is the required format pattern - some good examples of this at:
http://www.dan.co.uk/mysql-date-format/

:) It did not work because adding ".date_format(value,'%a %c/%e')" to the end of the field isn't the way to use functions in SQL.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39215876
Some man page refs...
http://dev.mysql.com/doc/refman/5.0/en/function-resolution.html
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

'%a %c/%e' should be expected to yield something like Mon 6/6 today.  In the other questions where the formatted date was used in an ORDER BY clause, the results of the ordering may be counterintuitive, since Friday would come before Monday and Thursday would come before Tuesday, etc.  A more useful ORDER BY would be to use the ISO-8601 DATETIME string.  This article shows how you use that.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

HTH, ~Ray
0
 
LVL 6

Expert Comment

by:Robert Saylor
ID: 39216228
Might not be a bad idea to use alias in your names to shorten the query.

SELECT
DATE_FORMAT(`mced`.`value`,'%a %c/%e') AS 'end_date'
FROM magentocatalog_category_entity_datetime mced


Note: value would be the column name.

IE: `mced`.`end_date`
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now