如何在Sql server 2008中将String转换为Date [英] How to convert String to Date in Sql server 2008

查看:129
本文介绍了如何在Sql server 2008中将String转换为Date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好,我有一个数据网格。



Hi Guys, I have a datagrid .

Dim strQuery As String
            strQuery = "SELECT BENCHMARKNO 'BMK NO',BENCHMARKNAME 'BMK Name',CLIENTCODE 'Client ID',NOFILES 'No.of Files',REFERENCENO 'Reference No',NORESOURCES 'Resources',REPLACE(CONVERT(VARCHAR,STARTDATE,106),' ','-') AS 'Start Date', REPLACE(CONVERT(VARCHAR,FINISHDATE,106),' ','-')  AS  'Finish Date', AUTHORIZEDBY 'Authorized By',JOBNO 'Converted To Job No',REMARKS 'Remarks' FROM QMS_BENCHMARK ORDER BY BENCHMARKNO"
            Dim pBMKTrack As DataTable = GetTable(strQuery)
            dGridview.DataSource = pBMKTrack





在该查询中,启动日期和结束日期是列。



如果我可以尝试对列进行排序。它没有排序。



我得到了这样的日期



例如:01-jan-2012



03-Feb-2010

12-aug-2013



我想:03- 2010年2月

01-jan-2012

12-aug-2013



总日期格式应该是改变。



在DataBase STARTTIME和FINISHTIME COLATNS DATATYPE是DATETIME。





请帮帮我....



in that query , start date and finish date are columns.

if i can tried to sort the column. it doesn''t sorted.

I got the dates like these

example : 01-jan-2012

03-Feb-2010
12-aug-2013

I want : 03-Feb-2010
01-jan-2012
12-aug-2013

total date format should be changed.

In DataBase STARTTIME AND FINISHTIME COLUMNS DATATYPE ARE DATETIME.


Please help me ....

推荐答案

对不起,但你的问题完全是胡说八道。



如果您的 StartDate 类型, EndDate 字段是 DATETIME ,您不需要将其转换为日期时间类型。它类似于:如何将字符串转换为字符串?



要使用日期格式化DGV列,请参阅: http://msdn.microsoft.com/en-us/library/f9x2790s.aspx [ ^ ]



BTW ,您在这里得到了答案:如何对DataGridview中的DateTime列进行排序 [ ^ ]



将来,不要重新发布。如果你有一个问题(对于解决方案/答案的作者),请使用有问题或评论小部件。
Sorry, but your question has totally nonsense.

If your type of StartDate, EndDate fields is DATETIME, you don''t need to convert it to datetime type. It''s similarly like: how to convert string to string?

To format DGV columns with dates, see this: http://msdn.microsoft.com/en-us/library/f9x2790s.aspx[^]

BTW, you got your answer here: How to Sort the DateTime Column in DataGridview[^]

In the future, do not repost. If you have a question (to the author of solution/answer), please, use "Have a question or comment" widget.


这些天我们在很多帖子上看到了一些东西,人们用于处理带字符串而不是保留类型的DateTime值。



在查询中,不是从数据库中获取两个DateTime值,而是将它们转换为字符串。当你想对它们进行排序时会导致严重的问题;因为你做了一个字符串排序,这与DateTime排序完全不同(如果你考虑的话,这是有意义的。)



所以,你帮个忙吗?只需从数据库中获取DateTime值;您的DatagridView将知道如何处理DateTime排序。

并且,对于DatTime值的字符串表示,使用特定的DateTime.Format()重载,因此您将获得DateTime的字符串表示形式您想要的格式化文化中的值。



重要的是:不要尝试在查询中格式化您的值;只显示它们时格式化它们。



我希望这会有所帮助。
There is something we see on many posts these days, people use to handle DateTime values with strings rather than with reserved type.

In your query, instead of getting two DateTime values from your database, you transform them into strings. Which leads you to serious problems when you want to sort them ; because you make a string sort, which is quite different from a DateTime sort (which makes sense, if you think about it).

So, do you a favor, just get a DateTime value from your database ; your DatagridView will know how to handle a DateTime sort.
And, for the string representation of your DatTime value, use a specific DateTime.Format() overload, so you will get a string representation of your DateTime value in the formatting culture you want.

What is important is : do not try to format your values in the query ; format them when you display them only.

I hope this helps.


尝试更改''Order By'子句由BENCHMARKNO订购,开始。这将首先通过benchmarkno然后通过startdate对您的记录进行排序。
Try changing the ''Order By'' clause to "Order By BENCHMARKNO, STARTDATE". This will sort your records first by benchmarkno and then by startdate.


这篇关于如何在Sql server 2008中将String转换为Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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