如何在asp.net c#中将varchar类型列值转换为datetime并对日期进行排序 [英] how to convert varchar type column value to datetime in asp.net c# and sort the date

查看:131
本文介绍了如何在asp.net c#中将varchar类型列值转换为datetime并对日期进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表中有varchar类型列。我存储第一次访问的日期。现在我想在sql查询中将它转换为datetime并按日期对数据进行排序。



当我直接排序时,它将按天排序..



所以我们怎么做?

i've varchar type column in my table. I store date of first visit to in it. Now i want to convert it to datetime in sql query and sort the data by date.

when i sort directly, it will sort by days only..

so how can we do that?

推荐答案

不要。

从不将日期信息存储为varchar:将其存储为DateTime。这样,当用户输入时,可以使用他们的文化信息作为格式指南进行检查和转换。

如果将其存储为字符串,那么每次尝试使用它时必须决定是否:

Don't.
Never store date information as varchar: store it as DateTime. That way, when you user enters it is can be checked and converted using their culture info as a guide to teh format.
If you store it as a string, then every time you try to use it you have to decide if this:
12/03/05

2005年3月12日,或2005年12月3日,或2012年3月5日,或...

或者只是一般的错误:

Is 12th March 2005, or 3rd Dec 2005, or 5th March 2012, or...
Or just plain wrong:

32/01/2014





延迟检查是一个坏主意,最好的解决方案是现在更改你的数据库,而它仍然很小。你离开的时间越晚,你得到的问题就越多。



Late checking is a bad idea, and the very best solution is to change your db now, while it is still small. The later you leave it, the more problems you will get.


这篇关于如何在asp.net c#中将varchar类型列值转换为datetime并对日期进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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