Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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
在sql server中,如何从该字符串中分别获取年和月?_Sql_Sql Server 2005_Date - Fatal编程技术网

在sql server中,如何从该字符串中分别获取年和月?

在sql server中,如何从该字符串中分别获取年和月?,sql,sql-server-2005,date,Sql,Sql Server 2005,Date,我有这个字符串格式mm/yyyy- 01/2010 如何将月份和年份与它分开。我希望以一种可以比较它们的方式将其分别设置为01和2010?检查子字符串和charindex函数的帮助检查子字符串和charindex函数的帮助这应该可以将您连接起来(假设@Date代表您的日期字符串): 此时,@Month和@Year将包含表示月份和年份的字符串。这将连接您(假设@Date代表您的日期字符串): 此时,@Month和@Year将包含表示月份和年份的字符串。示例: declare @d char(7);

我有这个字符串格式mm/yyyy-

01/2010


如何将月份和年份与它分开。我希望以一种可以比较它们的方式将其分别设置为01和2010?

检查子字符串和charindex函数的帮助检查子字符串和charindex函数的帮助这应该可以将您连接起来(假设@Date代表您的日期字符串):

此时,
@Month
@Year
将包含表示月份和年份的字符串。

这将连接您(假设@Date代表您的日期字符串):

此时,
@Month
@Year
将包含表示月份和年份的字符串。

示例:

declare @d char(7);
declare @Month varchar(2);
declare @Year varchar(4);

set @d = '01/2010';

SET @Month = LEFT(@d, 2);
SET @Year = RIGHT(@d, 4);
例如:

declare @d char(7);
declare @Month varchar(2);
declare @Year varchar(4);

set @d = '01/2010';

SET @Month = LEFT(@d, 2);
SET @Year = RIGHT(@d, 4);

我的荣幸。如果你的月份都是两位数的话,Leniel的答案也应该有效,而且简单一点。我很乐意。如果你的月份都是两位数,Leniel的答案也应该有效,而且更简单一点。