如何从SQL Server2008中获取仅日期部分? [英] how to get only date part from sql server2008?

查看:90
本文介绍了如何从SQL Server2008中获取仅日期部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c#和sql server 2008进行项目.
我从datePicker获取日期并将其另存为datePicker1.SelectedDate.ToString()
在sql表中,其中Date列的数据类型为Date.
现在,当我从表格中检索此日期并显示在textBox上时,Date + Time将会像这样显示5/13/2013 12:00:00 AM.
我只想在textBox中显示日期部分,例如5/13/2013.

i m working on project with c# and sql server 2008.
I get the date from datePicker and save it as datePicker1.SelectedDate.ToString()
in sql table where Date column has datatype Date.
Now when i retrive this date from table and display on textBox the Date+Time will be display like this 5/13/2013 12:00:00 AM.

I want to display only date part in textBox like 5/13/2013 .
How Can i do this????

推荐答案

问题不是SQL部分-您将SQL中的值返回到相应的.NET中class:DateTime-因此时间部分会自动设置为0或午夜. DateTime始终包含一个时间元素(因为它是过去任意点以来的毫秒数).

因此,您需要做的就是停止使用默认的ToString实现来显示日期值,因为它始终包含时间.
It''s not the SQL part that is the problem - you return the value from SQL into the appropriate .NET class: DateTime - so the time part is automatically set to 0, or midnight. DateTime always contains a time element (since it is a number of milliseconds since an arbitrary point in the past).

So what you need to do is stop using the default ToString implementation to display your date value as it always include the time.
myTextBox.Text = myDateTime.ToString("MM/dd/yyyy");

会这样做,或查看以下内容:
格式化显示日期时间-格式字符串说明 [ ^ ]

Will do it, or look at this:
Formatting a DateTime for display - format string description[^]


Use
DateTime.ToShortDateString()


您也可以尝试这样

you can try like this also

SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS
[DD/MM/YYYY]


这篇关于如何从SQL Server2008中获取仅日期部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆