Solved

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

Posted on 2011-09-30
6
451 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 108

Expert Comment

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

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:mavmanau
Comment Utility
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
Comment Utility
Thank you for your help!
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 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

14 Experts available now in Live!

Get 1:1 Help Now