场景是,数据库处于维护阶段。该数据库不是由我们的开发人员开发的。它是'xyz'公司在sql server 2000中开发的现有数据库。这是我现在工作的实时数据库。我想编写一个存储过程,它将检索从 date1 到 date 2 的记录。所以查询是:
Select * from MyTableName
Where colDate>= '3-May-2010' and colDate<= '5-Oct-2010' and colName='xyzName'
而我的理解是,我必须获取包括上限日期和下限日期的数据。但不知何故,我得到了从“2010 年 5 月 3 日”(这很好,但是)到“2010 年 10 月 10 日”的记录,
正如我在表设计中观察到的那样,对于 ColDate,开发人员使用“varchar”来存储日期。我知道他们这是错误的补救措施。因此,在我的存储过程中,我还使用了 varchar 参数作为 @FromDate1 和 @ToDate 来获取 SP 的输入。这给了我我已经解释过的结果。我尝试将参数类型设为“Datetime”,但在保存/更改存储过程时显示错误“@FromDate1 具有无效数据类型”,与“@ToDate”相同。
情况是,我根本无法改变表格设计。我必须在这里做什么?我知道我们可以在sql server 2008中使用用户定义的表,但是有版本sql server 2000。它不支持相同的功能。
请指导我应对这种情况。
**Edited**
I am trying to write like this SP:
CREATE PROCEDURE USP_Data
(@Location varchar(100),
@FromDate DATETIME,
@ToDate DATETIME) AS
SELECT *
FROM dbo.TableName
Where CAST(Dt AS DATETIME) >=@fromDate and CAST(Dt AS DATETIME)<=@ToDate and Location=@Location
GO
但出现错误:将表达式转换为数据类型日期时间时出现算术溢出错误。在 SQL Server 2000 中
那应该是什么?我是不是哪里错了? 我也
(202 row(s) affected) is changes every time in circular manner means first time sayin
(122 row(s) affected)
run again saying
(80 row(s) affected)
if again
(202 row(s) affected)
if again
(122 row(s) affected)
无法理解发生了什么事?
Scenario is, database is in the maintenance phase. this database is not developed by ours developer. it is an existing database developed by the 'xyz' company in sql server 2000. This is real time database, where i am working now. I wanted to write the stored procedure which will retrieve me the records From date1 to date 2.so query is :
Select * from MyTableName
Where colDate>= '3-May-2010' and colDate<= '5-Oct-2010' and colName='xyzName'
whereas my understanding I must get data including upper bound date as well as lower bound date. but somehow I am getting records from '3-May-2010' (which is fine but) to '10-Oct-2010'
As i observe in table design , for ColDate, developer had used 'varchar' to store the date. i know this is wrong remedy by them. so in my stored procedure I have also used varchar parameters as @FromDate1 and @ToDate to get inputs for SP. this is giving me result which i have explained. i tried to take the parameter type as 'Datetime' but it is showing error while saving/altering the stored procedure that "@FromDate1 has invalid datatype", same for "@ToDate".
situation is that, I can not change the table design at all. what i have to do here ? i know we can use user defined table in sql server 2008 , but there is version sql server 2000. which does not support the same.
Please guide me for this scenario.
**Edited**
I am trying to write like this SP:
CREATE PROCEDURE USP_Data
(@Location varchar(100),
@FromDate DATETIME,
@ToDate DATETIME) AS
SELECT *
FROM dbo.TableName
Where CAST(Dt AS DATETIME) >=@fromDate and CAST(Dt AS DATETIME)<=@ToDate and Location=@Location
GO
but getting Error: Arithmetic overflow error converting expression to data type datetime. in sql server 2000
What should be that ? is i am wrong some where ? also
(202 row(s) affected) is changes every time in circular manner means first time sayin
(122 row(s) affected)
run again saying
(80 row(s) affected)
if again
(202 row(s) affected)
if again
(122 row(s) affected)
I can not understand what is going on ?
发布评论
评论(5)
您可以 CAST() 或在此表上放置一个视图并将所有日期转换为 DateTime 值吗?我不记得 2000 是否支持索引视图,但是根据您的 varchar 值(dd-mmm-yyyy),尝试使用这些值选择日期范围(可能,但解析很痛苦)将是一场噩梦。
Can you CAST() or put a view on this table and convert all the dates to DateTime values? I can't remember if 2000 supported index views, but based on your varchar values (dd-mmm-yyyy), it's going to be a nightmare to try and select date rages (possible, by parsing but a pain) with those values.
您需要将 varchar 列中的“日期”转换为实际的
datetime
值,然后进行比较。此外,我建议您的日期使用 YYYYMMDD 格式。如果您传递参数,您还希望强制它们为日期时间。
当然,最好的解决方案是简单地将表中的数据类型更改为日期时间并更正相关程序或创建几个计算将 varchar 值转换为
datetime
的列,或者简单地添加一些静态datetime
列并从 varchar 列进行一次性更新。更新
您收到的错误是因为某些 varchar 值无法转换为
datetime
(这就是为什么将它们作为 varchar 是一个坏主意)。要么必须更正这些值,要么必须排除这些值。要查找非日期值,您可以使用IsDate
函数。您可以编写查询来排除这些值的另一种方法是:
You need to cast the "dates" in the varchar column to actual
datetime
values and then do your comparison. In addition, I would recommend using the format YYYYMMDD for your dates.If you are passing parameters you would also want to force them to be datetime
Of course, the best solution is to simply change the datatype in the table to
datetime
and correct the dependent programs or create a couple of computed columns which cast the varchar values todatetime
or simply add a few staticdatetime
columns and do a one-time update from the varchar columns.Update
The error you are getting is because some of the varchar values cannot be converted into
datetime
(which is why having them as varchar is a bad idea). Either those values must be corrected or you have to exclude those values. To find values that are not dates, you can use theIsDate
function.Another way you can write your query to exclude those values would be:
听起来您需要找到坏数据。我也必须处理应该存储在 varchar 列中的日期时间数据。追踪错误数据可能很困难。首先,我打开表格并观察它,寻找格式错误的日期,但我找不到任何日期,所以我认为日期很少,并且使用更具编程性的方法会很快。我最终使用了分而治之的方法:我将数据分成两半,并尝试将每一半转换为日期时间。如果任何一半导致错误,那么我将其进一步分成两半并检查这两半。这是一个递归过程。我本可以为此编写 T-SQL,但我是手动完成的。大概花了半个小时。
例如,假设 MyTableName 有一个名为 MyID 的主键,其连续值从 1 开始到 1000000 结束。然后我将运行两个查询来检查哪一半(或哪一半)包含无效数据:
如果第一个查询导致错误,则我将再运行两个查询:
假设在本次迭代中,只有第二个查询导致错误。然后,我将再运行两个查询:
最终,在 log-base-2-of-n 迭代或更少的时间内,您将找到第一个错误的数据值。当然,您不知道表中有多少错误值,因此可能需要一些时间才能找到所有错误值。就我而言,我有大约 10 个错误值,其中一些是“none”或“NA”,我将其更新为 NULL,其中一些只是格式错误的日期,我将其更新为格式正确的日期。修复了错误的数据后,我终于能够回到分析数据的最初任务了。
It sounds like you need to locate the bad data. I, too, have had to deal with what should have been datetime data stored in a varchar column. It can be difficult to track down the errant data. First, I opened the table and eyeballed it, looking for misformatted dates, but I couldn't find any, so I figured there were very few and that it would be quick to use a more programmatic approach. I ultimately used the divide-and-conquer approach: I divided the data in half and tried to cast each half to datetime. If either half caused an error, then i further divided it in half and checked those halves. It's a recursive process. I could have written T-SQL for this, but I did it kind of manually. It took maybe half an hour.
As an example, suppose MyTableName has a PRIMARY KEY named MyID with consecutive values starting at 1 and ending at 1000000. Then I would run two queries to check which half (or halves) contain invalid data:
If the first query caused an error, then I would run two more queries:
Suppose that in this iteration, only the second query caused an error. Then, I would run two more queries:
Eventually -- in log-base-2-of-n iterations or less -- you will find the first errant data value. Of course, you don't know how many errant values are in your table, so it could take time to find all of them. In my case, I had about 10 errant values, some of which were 'none' or 'NA' and which I UPDATEd to be NULL, and some of which were just misformatted dates, which I UPDATEd to be properly formatted dates. Having fixed the bad data, I was finally able to get back to the original task of analyzing the data.
查找不良数据的另一种方法是使用 SQL Server 2000 有限的正则表达式功能:
您必须自己完成查询。
Another way to find the bad data is to use SQL Server 2000's limited regular-expression functionality:
You'll have to complete the query yourself.