Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在SQL Server中获取特定字符串_C#_Sql_Sql Server 2008 - Fatal编程技术网

C# 在SQL Server中获取特定字符串

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

在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], 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)