I have a call in SQL Server 2012 that I need to have working on Server 2005 and 2008.
我有一个SQL Server 2012的电话,我需要在Server 2005和2008上工作。
FORMAT(Date, 'yyyyMM') = 201501
is what I am currently using and need to use DATEPART older versions of SQL Server but cannot see a way of doing it easily.
格式(日期,' yyymm ') = 201501是我目前正在使用的,并且需要使用DATEPART较老版本的SQL Server,但是不能很容易地看到这样做的方法。
Any help would be very much appreciated.
如有任何帮助,我们将不胜感激。
4 个解决方案
#1
3
Earlier versions of SQL Server don't have the FORMAT
, so you'd need to so something like
SQL Server的早期版本没有这种格式,因此需要类似的格式
YEAR(Date) = 2015 AND MONTH(Date) = 1
or something like that to check the two conditions
或者类似的东西来检验这两个条件
#2
1
If you're looking to use DATEPART, this should work:
如果你想使用DATEPART,这个应该可以:
CAST(DATEPART(YEAR, CAST('2015-01-05' AS DATETIME)) AS VARCHAR(4))
+RIGHT('00' + CAST(DATEPART(MM, CAST('2015-01-05' AS DATETIME)) AS VARCHAR(2)), 2)
Alternatively, you could use YEAR or MONTH like the other posts suggest.
或者,你也可以像其他帖子建议的那样使用年份或月份。
#3
1
Should work with SQL Server 2005 and 2008
是否应该使用SQL Server 2005和2008 ?
DECLARE @Date DATETIME = GETDATE();
SELECT CAST(YEAR(@Date) AS VARCHAR(4))
+ RIGHT('00' + CAST(MONTH(@Date) AS VARCHAR(2)),2)
RESULT: 201606
If you are going to use it in where clause you can do something like...
如果你打算在where子句中使用它,你可以做一些事情,比如……
WHERE YEAR(DateColumn) = 2016
AND MONTH(DateColumn) = 6
#4
1
SELECT CONVERT(NVARCHAR(6),GETDATE(),112)
#1
3
Earlier versions of SQL Server don't have the FORMAT
, so you'd need to so something like
SQL Server的早期版本没有这种格式,因此需要类似的格式
YEAR(Date) = 2015 AND MONTH(Date) = 1
or something like that to check the two conditions
或者类似的东西来检验这两个条件
#2
1
If you're looking to use DATEPART, this should work:
如果你想使用DATEPART,这个应该可以:
CAST(DATEPART(YEAR, CAST('2015-01-05' AS DATETIME)) AS VARCHAR(4))
+RIGHT('00' + CAST(DATEPART(MM, CAST('2015-01-05' AS DATETIME)) AS VARCHAR(2)), 2)
Alternatively, you could use YEAR or MONTH like the other posts suggest.
或者,你也可以像其他帖子建议的那样使用年份或月份。
#3
1
Should work with SQL Server 2005 and 2008
是否应该使用SQL Server 2005和2008 ?
DECLARE @Date DATETIME = GETDATE();
SELECT CAST(YEAR(@Date) AS VARCHAR(4))
+ RIGHT('00' + CAST(MONTH(@Date) AS VARCHAR(2)),2)
RESULT: 201606
If you are going to use it in where clause you can do something like...
如果你打算在where子句中使用它,你可以做一些事情,比如……
WHERE YEAR(DateColumn) = 2016
AND MONTH(DateColumn) = 6
#4
1
SELECT CONVERT(NVARCHAR(6),GETDATE(),112)