我有一个 SQL 查询,它经过搜索并显示数列的数百条记录。但是必须更改两列的显示;生效日期和清除日期。目前,它们都显示如下:(例如 1991 年 8 月 29 日)。我想将输出更改为 1991 年 8 月 29 日。我不确定我会如何做到这一点,因为它将这些数据直接从数据库中提取到表中。
请注意我在这个项目中使用 MS-SQL 和 PHP。
这是我的 SQL 查询:
$sql = "select SerialNum as [Serial Number],ts_sitename As Site,(case m.Scratched
when 0 then 'Live'
when 1 then 'Free'
END) as Status, Note as Comment, (case Destroyed when 0 then 'NO'
when 1 then 'YES' END) as [Destroyed],
SUBSTRING(cast(mg_effectivedate as char), 1, 12) AS [Effective Date],
SUBSTRING(cast(mg_effectivedate as char), 12, 8) AS [Effective Time],
SUBSTRING(cast(ScratchedDate as char), 1, 4) + '-' +
SUBSTRING(cast(ScratchedDate as char), 3, 2) + '-' +
SUBSTRING(cast(ScratchedDate as char), 5, 2)
AS [Scratched Date],
SUBSTRING(cast(ScratchedDate as char), 10, 2) + ':' +
SUBSTRING(cast(ScratchedDate as char), 12, 2) + ':' +
SUBSTRING(cast(ScratchedDate as char), 14, 2)
AS [Scratched Time],
SUBSTRING(cast(mg_scratchdate as char), 1, 12) AS [Purge Date],
(select fl_filename from TheFiles_tab where mg_filenum = fl_filenum) as [Dataset],
(select hs_hostname from TheHosts_tab where mg_hostnum = hs_hostnum) as [Host],
(select UserCode from [User] where mg_usernum = UserId) as [UserCode]
from ((Media m left join MediaGenT g on m.MediaId = g.mg_medianum)
join TheSites_tab s on m.SiteId = s.ts_sitenum)
join Note n on m.NoteId = n.NoteId
where SerialNum like '" . $userQuery . "%'";
这是我显示两列的代码:
echo '<th>Effective Date</th>';
echo '<th>Purge Date</th>';
foreach ($result as $r)
{
echo "<tr>";
echo "<td>".$r['Serial Number'] . " </td>";
echo "<td>".$r['Site'] . " </td>";
echo "<td>".$r['Status'] . "</td> ";
echo "<td>".$r['Comment'] . " </td>";
echo "<td>".$r['Destroyed'] . " </td>";
echo "<td>".$r['Effective Date'] . " </td>";
echo "<td>".$r['Effective Time'] . "</td> ";
echo "<td>";
if ($r['Scratched Date'] == "") {
echo "NULL";
}
else {
echo $r['Scratched Date'];
} echo "</td>";
echo "<td>" ;
if ($r['Scratched Time'] == "") {
echo "NULL";
}
else {
echo $r['Scratched Time'];
} echo "</td>";
echo "<td>".$r['Purge Date'] . " </td>";
echo "<td>".$r['Dataset'] . "</td> ";
echo "<td>".$r['Host'] . " </td>";
echo "<td>".$r['UserCode'] . " </td>";
echo "<td>".$r['NoteId'] . " </td>";
echo "</tr>";
}
请注意,我是 SO 社区的新手。我一直在研究中使用这个网站,但第一次创建了一个帐户。