简体   繁体   English

SQL Select查询返回的DATE在SQL中没有TIME,但是当我运行ASP项目时,它不断添加“ 12:00:00 AM”

[英]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输出

我的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 . 使用GridViewDataFormatString属性。 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.

相关问题 SQL查询的时间大于今天的日期和时间,时间默认为12:00:00 AM - SQL to query greater than today's date and time, with time defaulting to 12:00:00 AM Oracle中的SQL查询删除时间或将其设置为默认值00:00:00而不影响日期 - SQL query in Oracle to remove the time or set it to default 00:00:00 without impacting the date 我的SQL代码显示00而不是12 AM。 我该怎么调整呢? - My SQL Code is showing 00 instead of 12 AM. How can i adjust this? 从00:00 GMT到12:00 GMT修改SQL查询中的日期字段 - Modify Date Field in SQL Query from 00:00 GMT to 12:00 GMT 从SQL日期格式读取日期会额外增加12:00:00 AM? - Reading date from sql date format adds extra 12:00:00 AM? SQL查询从上午12:00开始 - SQL query to start from hour 12:00 am Oracle-SQL中的日期是否始终为00:00:00? - Is a date in Oracle-SQL without a time-value always 00:00:00? 如何在 PostgreSQL 中找到没有日期的 00:00:00 和 23:28:05 之间的时间间隔 - How can I find time interval between 00:00:00 and 23:28:05 without date in PostgreSQL sql:其中日期包含12:00到22:00之间的时间 - sql : which dates contain time between 12:00 and 22:00 当我输入$ time = strftime(&#39;%c&#39;); 它在数据库中显示值00:00:00 - when i am entering $time=strftime('%c'); it showing value 00:00:00 in database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM