[英]SQL Select Query returning DATE without TIME inside SQL but i when i run my ASP project it keeps adding “12:00:00 AM”
SELECT
Lname + ' ' + Ename as Name,
DateOfBirth,
DATEDIFF(hour,dateOfBirth,GETDATE())/8766 AS Age
FROM EmpTBL
WHERE
DATEADD( Year, DATEPART( Year, GETDATE()) - DATEPART( Year, DateOfBirth), DateOfBirth)
BETWEEN CONVERT( DATE, GETDATE()) AND CONVERT( DATE, GETDATE() + 30);
The above query is what i use when i was testing inside the SQL it seems to work but when i add it to my ASP.net project the result is different, i already add "Convert(date, getdate()) " How to select date without time in SQL 上面的查询是我在SQL内进行测试时使用的,它似乎可以正常工作,但是当我将其添加到ASP.net项目中时,结果却有所不同,我已经添加了“ Convert(date,getdate())” 如何选择SQL中没有时间的日期
Output from my project 我项目的输出
Output from my SQL 我的SQL输出
When we fetch date from database to asp.net it's in Date-Time format not in Date so you can convert it to custom date as you want: 当我们从数据库获取日期到asp.net时,它采用Date-Time格式而不是Date,因此您可以根据需要将其转换为自定义日期:
<%# Convert.ToDateTime(Eval("DateOfBirth")).ToString("yyyy/MM/dd")%>
You can use any Format specifier / Custom Date and Time Format String according to your requirement. 您可以根据需要使用任何格式说明符 / 自定义日期和时间格式字符串 。
将您的查询更改为
select CONVERT(VARCHAR(10),dateOfBirth,101) FROM EmpTBL WHERE <>
You can use the CONVERT
function of ASP.Net, you can convert into string then format it. 您可以使用ASP.Net的CONVERT
函数,可以将其转换为字符串,然后对其进行格式化。
Some thing like adding .ToString("MM/dd/yyyy")
. 例如添加.ToString("MM/dd/yyyy")
。
I Think this post can help you: String Format Date - C# or VB.NET 我认为这篇文章可以为您提供帮助: 字符串格式日期-C#或VB.NET
.net has no such datatype for date only. .net没有仅用于日期的此类数据类型。 Your simply getting the default time in your datetime object. 您只需在datetime对象中获取默认时间即可。
Sql has date, time and datetime concepts so Sql can just show the date. Sql具有日期,时间和日期时间概念,因此Sql只能显示日期。
You can ignore the time in code or choose not to show the time on the UI by formatting it. 您可以忽略代码中的时间,也可以选择不通过格式化在UI上显示时间。
Use the DataFormatString
property in the GridView
. 使用GridView
的DataFormatString
属性。 If you set it to {0:d}
, you should just get a short date format. 如果将其设置为{0:d}
,则应该只获得短日期格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.