SQL Server date calculation
A. the first day of a month
Select dateadd (mm, datediff (mm, 0, getdate (), 0)
B. Monday of the week
Select dateadd (wk, datediff (wk, 0, getdate (), 0)
C. The first day of a year
Select dateadd (YY, datediff (YY, 0, getdate (), 0)
D. The first day of the quarter
Select dateadd (QQ, datediff (QQ, 0, getdate (), 0)
E. Last day of last month
Select dateadd (MS,-3, dateadd (mm, datediff (mm, 0, getdate (), 0 ))
F. Last day of last year
Select dateadd (MS,-3, dateadd (YY, datediff (YY, 0, getdate (), 0 ))
G. Last day of the month
Select dateadd (MS,-3, dateadd (mm, datediff (M, 0, getdate () + 1, 0 ))
H. The first Monday of this month
Select dateadd (wk, datediff (wk, 0,
Dateadd (DD, 6-datepart (day, getdate (), getdate ())
), 0)
I. The last day of the year
Select dateadd (MS,-3, dateadd (YY, datediff (YY, 0, getdate () + 1, 0 )).
Obtain the table structure [Replace 'sysobjects' with 'tablename']
select case isnull (I. name, '')
when ''then''
else'' * '
end as ispk,
object_name (. ID) as t_name,
. name as c_name,
isnull (substring (M. text, 1,254), '') as pbc_init,
T. name as f_datatype,
case isnull (typeproperty (T. name, 'Scale'), '')
when'' then cast (. prec as varchar)
else cast (. prec as varchar) + ',' + Cast (. scale as varchar)
end as f_scale,
. isnullable as f_isnullable
from syscolumns as a
join policypes as t
On (. xtype = T. xusertype and. id = object_id ('sysobjects')
left join (sysindexes as I
join syscolumns as A1
on (I. id = a1.id and a1.id = object_id ('sysobjects') and (I. status & 0x800) = 0x800 and a1.colid <= I. keycnt)
On (. id = I. ID and. name = index_col ('sysobjects', I. indid, a1.colid)
left join syscomments as m
On (M. id =. cdefault and objectproperty (. cdefault, 'isconstraint') = 1)
order by. colid ASC