简体   繁体   English

从选择查询的日期时间字段中删除时间

[英]Removing time from a datetime field in a select query

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: 我在网站上使用以下SQL查询,并且要在显示结果表时从datetime列“ Date_Required”中删除时间:

$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" 因此显示为“ Jan 01 2018”,而不是“ Jan 01 2018 12:00:00:000PM”

The query is in a PHP file. 该查询位于PHP文件中。

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语句中像这样一一提及

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

using * will be more tricky. 使用*将更加棘手。

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): 如果您确定要使用SQL进行此操作,请阅读此文章(花了我几秒钟的时间):

Best approach to remove time part of datetime in SQL Server 在SQL Server中删除日期时间的时间部分的最佳方法

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进行此操作,则有几种选择,可通过substr或使用功能强大的DateTime类来剪切字符串的前10个字符

<?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

2018-03-05 2018-03-05

live demo 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM