Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

MySQL WEEK vs DATE_FORMAT

Posted on 2014-02-03
2
498 Views
Last Modified: 2014-02-03
I am going to write a report based on week number of the year.

DATE Example: 20161225

When I run this date using mysql I get 2 different values for the WEEK Number.

My select statement:

WEEK(`charters`.`start_date`, '%u') AS 'ISO_WEEK'

This returns "52" for the week number

DATE_FORMAT(`charters`.`start_date`, %v') AS 'ISO_WEEK2'

This returns "51" for the week number

Witch one should I use?
0
Comment
Question by:Robert Saylor
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39829219
for WEEK, the 2nd argument will specify what the result value will be, and %u seems like incorrect, it must be a value from 0 to 7 instead.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week

and in date_format:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
if you used %v, this would match 3 or 7 (see the exact rules)
if you used %u, this would have 1 or 5 (see the exact rules)

so, the answer is: it depends on what you want to use as rules/output
0
 
LVL 7

Author Closing Comment

by:Robert Saylor
ID: 39829299
Thank you! I was not passing the mode correctly in WEEK. Looks good now and I have something to report on when the WEEK number starts.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Union 2 queries to a cte (temp table perhaps) 9 41
SQL Syntax 24 46
sql server query 12 26
RAISERROR WITH NOWAIT 2 17
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Creating and Managing Databases with phpMyAdmin in cPanel.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

829 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