简体   繁体   English

SQL 转换日期格式和减去天数

[英]SQL Convert Date format and Subtract days

I am trying to format GETDATE() '08/10/2021' where it subtracts 9 days from it however I keep getting '2021-09-30 00:00:00.000' with trailing 0...where all I want is just the date in this format '08/10/2021'我正在尝试格式化 GETDATE() '08/10/2021' ,它从中减去 9 天,但是我不断收到带有尾随 0 的 '2021-09-30 00:00:00.000' ......我想要的只是这种格式的日期“08/10/2021”

I'm using 'select convert(nvarchar(MAX), getdate(), 103)' to return the date in the format I want but I need to be able to subtract 9 days from this.我正在使用 'select convert(nvarchar(MAX), getdate(), 103)' 以我想要的格式返回日期,但我需要能够从中减去 9 天。

The thing is when I try 'SELECT CONVERT(VarChar(20), DATEADD(DD, -9, GETDATE()), 103)' (which works) in my query it errors with:问题是当我在查询中尝试 'SELECT CONVERT(VarChar(20), DATEADD(DD, -9, GETDATE()), 103)'(有效)时,它会出错:

'The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.' “将 varchar 数据类型转换为 datetime 数据类型导致值超出范围。”

this works:这有效:

SELECT CONVERT(VarChar(10), DATEADD(DD, -9, GETDATE()), 103) SELECT CONVERT(VarChar(10), DATEADD(DD, -9, GETDATE()), 103)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM