Solved

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

Posted on 2011-09-30
6
459 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 109

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 109

Accepted Solution

by:
Ray Paseur earned 500 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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 109

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cookie not unsetting 7 27
Change background color in td if any value is 1 9 24
Ajax and PHP 4 31
Use Select Query to Return Results as a Form 9 24
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses how to create an extensible mechanism for linked drop downs.
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 …

837 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