トップページに戻る
次のSQLServerのサンプルへ
前のSQLServerのサンプルへ
1-4 日付関数
select GetDate() as "GetDate",
DateAdd(year,1,GetDate()) as "1年後"
go
GetDate 1年後
----------------------- -----------------------
2011-12-31 11:11:40.890 2012-12-31 11:11:40.890
MSDN --- GetDate
select GetDate() as "GetDate",
DateDiff(day,cast('2011-12-01' as date),GetDate()) as "日数の差"
go
GetDate 日数の差
----------------------- --------
2011-12-07 11:11:21.047 6
MSDN --- DateDiff
select GetDate() as "GetDate",
Year(GetDate()) as "年",
Month(GetDate()) as "月",
Day(GetDate()) as "日"
go
GetDate 年 月 日
----------------------- ---- -- --
2011-12-31 11:10:43.263 2011 12 31
MSDN --- Year
MSDN --- Month
MSDN --- Day
select GetDate() as "GetDate",
DatePart(Year,GetDate()) as "年",
DatePart(Month,GetDate()) as "月",
DatePart(Day,GetDate()) as "日",
DatePart(hour,GetDate()) as "時",
DatePart(minute,GetDate()) as "分",
DatePart(second,GetDate()) as "秒"
go
GetDate 年 月 日 時 分 秒
----------------------- ---- -- -- -- -- --
2011-12-31 11:08:32.163 2011 12 31 11 8 32
MSDN --- DatePart
select GetDate() as "GetDate",
DateName(weekday, GETDATE()) as "DateName関数"
go
GetDate DateName関数
----------------------- ------------
2011-12-31 11:07:30.533 土曜日
MSDN --- DateName
select GetDate() as "GetDate",
Convert(varchar(10),GetDate(),111) as "変換後"
go
GetDate 変換後
----------------------- ----------
2011-12-31 11:06:57.740 2011/12/31
MSDN --- CastおよびConvert