Knowledge gained after reading inside SqlServer2005-TSQL programming

First, you should know the difference among the DateAdd,DateDiff,DatePart functions.
Second, you should know the key words of SQL SERVER new edition. Such as : DateName(). You can get the day's name of week back from this function. Such as : Monday,Tuesday,Wednesday, thursday,Friday.
 Generally, we get the day of week by using DatePart. Such as : select datepart(weekday,'2009-8-4') , as is we know, SQL Server will set the DateFirst based on your language settings. Usually, the value of the first day of week is '7'. It means that the sunday is setted as the first day of week. However, we can change the first day of week manually. Such as follows:
Set DateFirst 1----------It means that I specify the Monday as the first day of week. If we execute the SQL as follows:
set DateFirst 1
select datepart(weekday,'2009-8-4')
-- you will get '2' back from this function.
Actually, you can always get the correct days order of week with follows: Such as (Monday is 1,Tuesday is 2, Wednesday is 3,etc)
Select DatePart(weekday,Cast('2009-8-4' as DateTime)+@@DateFirst). But, we usually use the following SQL during our wor k.
Select DatePart(weekday,Cast('2009-8-4') as DateTime)+@@DateFirst-n)----n is the value(DateFirst) which we are going to set to instead of (set DateFirst n)

你可能感兴趣的:(sqlserver2005)