Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to format a date in the MS SQL query using PHP and PDO

Posted on 2011-09-30
6
Medium Priority
?
477 Views
Last Modified: 2013-12-12
Hello Experts!

Firstly thank you for taking a look at my question.

Essentially I have a query which one of the fields is a date field where it contains the time like below.  I want to only pull out the date format like this: 2011-09-01 and do a group by on this field as well.

What it currently comes out like: 2011-09-01 12:38:06.413

I can do a date format on it when I am printing it to the screen and to a csv file, but to do the group by I need it to get rid of the time.

This is my query I am currently using which works, but wanted to add the group by to it.

$query = "SELECT BrandID,DateFunded,RiskLevelID FROM DB Where DateFunded is not NULL and DateFunded between '".$firstmonth."' and '".$today."' order by DateFunded" ;
//Load the query
$stmt = $c->prepare($query) ;
 //Run the query
 $stmt->execute();

Also how do I add some trouble shooting to the end of the query so I can see any errors that pop up?
Thank you, your assistance is appreciated!

0
Comment
Question by:mavmanau
  • 3
  • 3
6 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36895260
SQL has ways of formatting a DATETIME field so that all you get is a DATE and not a time.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 36895272
This might not be my final choice, but SELECT CONVERT might be perfect for your needs.  
http://blog.sqlauthority.com/2007/06/10/sql-server-retrieve-select-only-date-part-from-datetime-best-practice/
0
 

Author Comment

by:mavmanau
ID: 36895759
Hi,

following that process this should work then yes?  

SELECT BrandID,CONVERT(VARCHAR(10),DateFunded,111),RiskLevelID FROM DB Where CONVERT(VARCHAR(10),DateFunded,111) is not NULL and CONVERT(VARCHAR(10),DateFunded,111) between '2011-09-01' and '2011-09-30' order by CONVERT(VARCHAR(10),DateFunded,111)

I must have missed something however, because it returns nothing out of the DB, and if I remove all the converts etc, it works perfectly.

Can you see anything blatently obvious that I have missed?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mavmanau
ID: 36895906
Hi,

Worked it out.  the format of the data had slashes rather than dashes.  Thank you for your help!
0
 

Author Closing Comment

by:mavmanau
ID: 36895926
Thank you for your help!
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36896506
Aha!  Slashes have some different meanings to date conversion.  You might like the article here.  It has a lot about mySQL (not MSSQL) but most of the principles are equally applicable.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

926 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