当日期coloumn具有数据类型varchar时,如何触发查询日期时间? [英] how to fire a query for date time when the date coloumn has datatype varchar?

查看:117
本文介绍了当日期coloumn具有数据类型varchar时,如何触发查询日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据特定日期保存在数据库中的数据现在我想要检索去年/月的数据,但问题是我的日期列是varchar类型,所以如何检索数据

I have data saved in database according to spcific dates now i want to retrive the data of last year/ month but the problem is my date column is of type varchar so how to retrive the data

推荐答案

非常不幸。首先,日期应该是日期类型而不是任何其他类型。话虽如此,请检查一下: SQL Server的功能有助于将日期和时间值转换为字符串文字和其他日期和时间格式。 [ ^ ]
There is very unfortunate. In the first place, a date should be of date type and not any other type. Having said that, check this out: SQL Server Functions that helps to convert date and time values to and from string literals and other date and time formats.[^]


简单:不要将日期时间值存储为字符串 - 它总是会出现问题。

问题是,当用户输入日期时,他们会以当地日期格式输入日期:

Simple: don't store date time values as strings - it always gives problems.
The trouble is that when users enter dates, they enter them in their local date format:
01-02-12



可能是2012年2月1日,2012年1月2日,2001年2月12日,甚至只是错误!

唯一你知道用户输入日期的格式是什么时候进入他们 - 所以如果你当时没有做任何验证和转换那么你就完全失去了机会,一旦它进入你的数据库y你不知道它们是哪一个。



因此,更改数据库:将日期/时间值存储为DATETIME列,验证并更正现有数据,并从那里开始工作。否则,你总是会遇到问题,这只是它们的开始!


which could be 1st Feb 2012, 2nd Jan 2012, 12th Feb 2001, or even just plain wrong!
The only time you have any idea what format the user is entering dates in is when he enters them - so if you don't do any validation and conversion at that time then you have completely lost the opportunity and once it enters your DB you have no idea which of them it is.

So change your DB: store date / time values as DATETIME columns, validate and correct your existing data, and work from there. Otherwise, you are always going to get problems and this is just the start of them!


实际上我已经用字段bla bla和日期保存数据,数据类型为varchar,现在我想访问基于日期的数据库...

但我不知道怎么做?
actually i have saved the data with field bla bla and date with datatype varchar and now i want to access the database on the basis of date ...
but i don t know how??


这篇关于当日期coloumn具有数据类型varchar时,如何触发查询日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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