C# 在SQL Server中获取特定字符串
在SQLServer2008中,我希望获得以下字符串格式的年份。参数将是以下字符串之一(C# 在SQL Server中获取特定字符串,c#,sql,sql-server-2008,C#,Sql,Sql Server 2008,在SQLServer2008中,我希望获得以下字符串格式的年份。参数将是以下字符串之一(DateTime.MinValue和DateTime.MaxValue来自C#): “1/1/0001 12:00:00 AM”和“12/31/9999 11:59:59 PM” 以下是我预期的答案: 0001 from 1st string. 9999 from 2nd string. 如果您知道输入字符串的格式,只需执行以下操作: SELECT ... SUBSTRING([Input], PATIND
DateTime.MinValue
和DateTime.MaxValue
来自C#):
“1/1/0001 12:00:00 AM”和“12/31/9999 11:59:59 PM”
以下是我预期的答案:
0001 from 1st string.
9999 from 2nd string.
如果您知道输入字符串的格式,只需执行以下操作:
SELECT ... SUBSTRING([Input], PATINDEX('%[0-9][0-9][0-9][0-9]%', [Input]), 4)
FROM ...
或
这也可能是另一种方法
declare @test nvarchar(50)
SET @test='1/1/9999 12:00:00 AM'
declare @testdate date
set @testdate=CONVERT(date,@test,101)
select DatePart(year,@testdate)
查看此功能试用日期格式功能
declare @test nvarchar(50)
SET @test='1/1/9999 12:00:00 AM'
declare @testdate date
set @testdate=CONVERT(date,@test,101)
select DatePart(year,@testdate)