繁体   English   中英

VB.Net - 如何让 GridView 只显示 DateTime SQL 列中的日期?

[英]VB.Net - How to get GridView to only show Date from DateTime SQL column?

我有一个 SQL 公式,我计划在即将在 VB.NET 中开发的 Web 程序中使用该公式,该程序将在 GridView 中显示从我的 SQL 公式获得的数据。

我的 SQL 的某个特定部分遇到了问题

select A.[Inv No], A.[Project No], 
cast([Record Date] as date)as [Date],   --This area is problem field
A.[Description], A.[Problem + Repair Details], 
A.[Status], convert(float, A.[Accumulative Stroke]) 
as [Accumulative Stroke],
convert(float, A.[Preventive Stroke]) 
as [Preventive Stroke], 
A.[PIC], A.[Measurement (OK/NG)] 
from [SQL].[dbo].[MAINT_ENTRY] A 

left join (select [Inv No], max(Date +' '+ Time) as [Record Date] 
from [SQL].[dbo].[MAINT_ENTRY] 
group by [Inv No]) B 

on A.[Inv No] = B.[Inv No]

where [Record Date] = Date +' '+ Time and 
[Problem + Repair Details] <> '""' 
Order by A.[Status], A.[Inv No], A.[Date]desc

[Record Date]是将DateTime列合并为单个合并列的列,以便我获得最新数据以显示在我的程序中。 这两个字段都需要采用datetime格式才能加入。

我希望 GridView 只在Record Date显示 Date ,并且由于它在datetime我尝试使用CAST将格式更改为date

虽然它在 SQL Management Studio 中显示了正确的结果,我在其中模拟了结果,但它似乎不想在 VB.Net 中很好地发挥作用,因为我得到的结果仍然使用datetime格式。 它会删除时间数据,但仍保留时间格式。

22/4/2019 08:45:00 AM ---> 22/4/2019 12:00:00 AM

似乎是什么问题导致了这个?

您正在尝试直接在 SQL 查询中强制转换日期时间,以便这样做而不是

cast([Record Date] as date)as [Date]

你应该这样做:

CONVERT(varchar(10), [Record Date], 103) as [Date]

正如CONVERT ( data_type [ ( length ) ] , expression [ , style ] )语法指定的那样: 参考

如果我理解你的问题,你是从一些具有 DateTime 字段的 SQL 服务器返回一个对象。 您想使用 VB 仅显示日期组件。

在 VB 中,DateTime 对象有一个“Date”属性,您可以使用它来仅显示 DateTime 属性的日期组件。 一个简单的代码示例:

Dim myDateTime as DateTime = now()
MessageBox.Show("Today's date is: " + myDateTime.Date)

如果您需要更多参考,请查看https://docs.microsoft.com/en-us/dotnet/api/system.datetime.date?view=netframework-4.8

暂无
暂无

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

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