如何修改日期列的值?

发布于 2024-08-06 03:13:48 字数 263 浏览 5 评论 0原文

使用 SQL Server 2000,日期列数据类型为 varchar...

在我的表中日期列值如下:

2009/01/31
2009/02/00
2009/02/01....
2009/03/31
2009/04/00
2009/04/01.... so on...,

我想显示 2009/01/31 而不是 2009/02/00,如果日期是 2009/02/00 它应该显示之前的日期。

如何查询这个条件呢?

Using SQL Server 2000, Date Column Datatype is varchar...

In my table date column values are like:

2009/01/31
2009/02/00
2009/02/01....
2009/03/31
2009/04/00
2009/04/01.... so on...,

I want to display 2009/01/31 instead of 2009/02/00, If date is 2009/02/00 it should display previous date.

How to make a query for this condition?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

她比我温柔 2024-08-13 03:13:48

试试这个如何

DECLARE @Table TABLE(
        Val VARCHAR(10)
)

INSERT INTO @Table (Val) SELECT '2009/01/31'
INSERT INTO @Table (Val) SELECT '2009/02/00'

SELECT * FROM @Table

SELECT  CAST(SUBSTRING(Val, 1, 8) + CASE WHEN SUBSTRING(Val, 9, 2) = '00' THEN '01' ELSE SUBSTRING(Val, 9, 2) END AS DATETIME) + CASE WHEN SUBSTRING(Val, 9, 2) = '00' THEN -1 ELSE 0 END
FROM    @Table

检查最后一个字符是否为00,然后设置为01并添加-1

How about try this

DECLARE @Table TABLE(
        Val VARCHAR(10)
)

INSERT INTO @Table (Val) SELECT '2009/01/31'
INSERT INTO @Table (Val) SELECT '2009/02/00'

SELECT * FROM @Table

SELECT  CAST(SUBSTRING(Val, 1, 8) + CASE WHEN SUBSTRING(Val, 9, 2) = '00' THEN '01' ELSE SUBSTRING(Val, 9, 2) END AS DATETIME) + CASE WHEN SUBSTRING(Val, 9, 2) = '00' THEN -1 ELSE 0 END
FROM    @Table

Check if the last chars is 00, then set to 01 and add -1

云柯 2024-08-13 03:13:48

好吧,我没有看到有日期以 00 作为日期部分的事实。

我想出了这种蛮力的方法。

我确信可以应用一些优化,但这应该为您提供一个起点。

select dateadd(dd, -1, convert(datetime, case when substring(Reverse(@WeirdDate), 1, 2) = '00' then reverse('1' + substring(Reverse(@WeirdDate), 2, len(@WeirdDate) - 1)) else @WeirdDate end, 101))

将@WeirdDate替换为列名,它似乎可以工作

declare @WeirdDate varchar(10)
set @WeirdDate = '2009/04/00'
select dateadd(dd, -1, convert(datetime, case when substring(Reverse(@WeirdDate), 1, 2) = '00' then reverse('1' + substring(Reverse(@WeirdDate), 2, len(@WeirdDate) - 1)) else @WeirdDate end, 101))

set @WeirdDate = '2009/04/03'
select dateadd(dd, -1, convert(datetime, case when substring(Reverse(@WeirdDate), 1, 2) = '00' then reverse('1' + substring(Reverse(@WeirdDate), 2, len(@WeirdDate) - 1)) else @WeirdDate end, 101))

set @WeirdDate = '2009/01/00'
select dateadd(dd, -1, convert(datetime, case when substring(Reverse(@WeirdDate), 1, 2) = '00' then reverse('1' + substring(Reverse(@WeirdDate), 2, len(@WeirdDate) - 1)) else @WeirdDate end, 101))

OK, I didn't see the fact that there are dates with 00 as the day part.

I whipped up this brute force way.

I'm sure there are optimizations that can be applied, but this should give you a starting point.

select dateadd(dd, -1, convert(datetime, case when substring(Reverse(@WeirdDate), 1, 2) = '00' then reverse('1' + substring(Reverse(@WeirdDate), 2, len(@WeirdDate) - 1)) else @WeirdDate end, 101))

replace the @WeirdDate with the column name, and it seems to work

declare @WeirdDate varchar(10)
set @WeirdDate = '2009/04/00'
select dateadd(dd, -1, convert(datetime, case when substring(Reverse(@WeirdDate), 1, 2) = '00' then reverse('1' + substring(Reverse(@WeirdDate), 2, len(@WeirdDate) - 1)) else @WeirdDate end, 101))

set @WeirdDate = '2009/04/03'
select dateadd(dd, -1, convert(datetime, case when substring(Reverse(@WeirdDate), 1, 2) = '00' then reverse('1' + substring(Reverse(@WeirdDate), 2, len(@WeirdDate) - 1)) else @WeirdDate end, 101))

set @WeirdDate = '2009/01/00'
select dateadd(dd, -1, convert(datetime, case when substring(Reverse(@WeirdDate), 1, 2) = '00' then reverse('1' + substring(Reverse(@WeirdDate), 2, len(@WeirdDate) - 1)) else @WeirdDate end, 101))
烙印 2024-08-13 03:13:48

显示有效日期非常简单:

SELECT  
   CASE ISDATE(datecolumn) 
     WHEN 1 THEN CAST(datecolumn AS DATETIME) 
     ELSE NULL 
   END  AS 'DateValue'
FROM
  dbo.YourTable

但是处理像“2009/02/00”这样的无效日期有点棘手......

但是由于您似乎已经有了上一个日期(2009/01/31) - 无法你不就忽略无效日期吗???

SELECT  
   CAST(datecolumn AS DATETIME) 
FROM
   dbo.YourTable
WHERE
   ISDATE(datecolumn) = 1

马克

To show the valid dates is pretty easy:

SELECT  
   CASE ISDATE(datecolumn) 
     WHEN 1 THEN CAST(datecolumn AS DATETIME) 
     ELSE NULL 
   END  AS 'DateValue'
FROM
  dbo.YourTable

But handling the invalid dates like '2009/02/00' is a bit trickier.....

But since you already seem to have the previous date (2009/01/31) - couldn't you just ignore the invalid dates???

SELECT  
   CAST(datecolumn AS DATETIME) 
FROM
   dbo.YourTable
WHERE
   ISDATE(datecolumn) = 1

Marc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文