Solved

query syntax

Posted on 2013-06-02
4
111 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 110

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 110

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 7

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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

685 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