0

I have the following SQL query to use on my website and I want to remove the time from the datetime column 'Date_Required' when the resulting table is displayed:

$query = "SELECT Job_No, Sub_No, Visit_Status, Engineer, CAST(Date_Required AS DATE) FROM dbo.VSMF_SERVICE_VISITS WHERE Visit_Status = 'O' and Engineer='*AY' and Date_Required >= '2018-01-01 00:00:00.000' ORDER BY Date_Required DESC";

So it's displayed as "Jan 01 2018" instead of "Jan 01 2018 12:00:00:000PM"

The query is in a PHP file.

Ross
  • 66
  • 12

3 Answers3

3

Try this one.

$query = "SELECT CAST(Date_Required AS DATE) as Date_Required FROM dbo.VSMF_SERVICE_VISITS WHERE Visit_Status = 'O' and Engineer='*AY' and Date_Required >= '2018-01-01 00:00:00.000' ORDER BY Date_Required DESC";

I hope this will work for you. If you want to select all the columns in the table then mention them in the select statement one by one like this

SELECT id, name, CAST(Date_Required AS DATE) as Date_Required from ...

using * will be more tricky.

noman tufail
  • 341
  • 2
  • 8
  • Thank you for the answer, but I get no results when I run that query. – Ross Mar 05 '18 at 08:51
  • was there any exception ? – noman tufail Mar 05 '18 at 08:59
  • okay try this one. $query = "SELECT DATE(\`Date_Required\`) as Date_required FROM dbo.VSMF_SERVICE_VISITS WHERE Visit_Status = 'O' and Engineer='*AY' and Date_Required >= '2018-01-01 00:00:00.000' ORDER BY Date_Required DESC"; – noman tufail Mar 05 '18 at 09:02
  • I think problem is in some single quote. Otherwise this query is working fine in phpmyadmin sql editor. – noman tufail Mar 05 '18 at 09:03
  • No that doesn't work either, blank again with no exception. – Ross Mar 05 '18 at 09:04
  • You can try this `DATE(\`Date_Required\`)` technique on a simple table in your sql editor and than i think you can better understand how to make it work in your current problem. – noman tufail Mar 05 '18 at 09:07
  • okay once more try removing backticks like this. `$query = "SELECT DATE(Date_Required) as Date_required FROM dbo.VSMF_SERVICE_VISITS WHERE Visit_Status = 'O' and Engineer='*AY' and Date_Required >= '2018-01-01 00:00:00.000' ORDER BY Date_Required DESC";` – noman tufail Mar 05 '18 at 09:09
  • I found the error, it says "DATE is not a recognised built in function name." – Ross Mar 05 '18 at 09:12
  • Okay try this one. `CAST(Date_Required AS DATE)` instead of `DATE(Date_Required) as Date_required`. I never worked on sql server but you can try this. – noman tufail Mar 05 '18 at 09:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166224/discussion-between-ross-h-and-noman-tufail). – Ross Mar 05 '18 at 09:19
0

If you are determined to do this in SQL, then have a read of this post (which it took me a couple of seconds to find):

Best approach to remove time part of datetime in SQL Server

MJH
  • 1,710
  • 1
  • 9
  • 19
  • I'm struggling with selecting multiple columns and having multiple where conditions. The link you gave only shows one select and no where conditions. – Ross Mar 05 '18 at 08:48
0

If you want to do this with PHP you have several options, cutting the first 10 chars of the string by substr or using the powerful DateTime class

<?php
$dateTime = "2018-03-05 01:30:00";

echo substr($dateTime, 0 , 10);// option 1 
echo "\n";
$dateTimeObj = new DateTime($dateTime);// option 2 
echo $dateTimeObj->format("Y-m-d");

this outputs

2018-03-05

2018-03-05

live demo

Community
  • 1
  • 1
Accountant م
  • 6,975
  • 3
  • 41
  • 61
  • Thank you, but the date is a variable from an SQL Server database not a hardcoded date. – Ross Mar 05 '18 at 08:57
  • the `$dateTime` is for demonstration. Replace it with your actual value at the presentation layer (right before you put it in the HTML) – Accountant م Mar 05 '18 at 09:02