SQL常用语句(不定期更新)包含 日期格式,取列

select getdate()

select dateadd(day,-10,getdate())

select datepart(weekday,getdate())

select @@datefirst
select datepart(dw,getdate())

set datefirst 1--设置周一为1

select convert(datetime,convert(char(8),getdate(),120)+'1')--本月第一天
select dateadd(day,-1,convert(char(8),dateadd(month,1,getdate()),120)+'1')--本月最后一天


select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08

select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')
20040912110608

convert(varchar(12),getdate(),23)
2008-04-01

select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12

select CONVERT(varchar(12) , getdate(), 112 )
20040912

select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12

其它不常用的日期格式转换方法:

select CONVERT(varchar(12) , getdate(), 101 )
09/12/2004

select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004

select CONVERT(varchar(12) , getdate(), 104 )
12.09.2004

select CONVERT(varchar(12) , getdate(), 105 )
12-09-2004

select CONVERT(varchar(12) , getdate(), 106 )
12 09 2004

select CONVERT(varchar(12) , getdate(), 107 )
09 12, 2004

select CONVERT(varchar(12) , getdate(), 108 )
11:06:08

select CONVERT(varchar(12) , getdate(), 109 )
09 12 2004 1

select CONVERT(varchar(12) , getdate(), 110 )
09-12-2004

select CONVERT(varchar(12) , getdate(), 113 )
12 09 2004 1

select CONVERT(varchar(12) , getdate(), 114 )
11:06:08.177

取列

begin tran 
declare @sql varchar(4000),@ff varchar(200)
select @sql=''
declare c1 cursor for 
--select name from syscolumns where id=772965880
--select a.name from syscolumns a,sysobjects b where b.name='KCCKD1' and a.id=b.id and b.type='U'
select name from syscolumns where id = object_id('表名') 
open c1
fetch c1 into @ff 
while @@fetch_status = 0 
begin
select @sql=@sql+','+ @ff
fetch c1 into @ff
end 
close c1
deallocate c1
print '1'
print @sql
commit tran

原文地址:https://www.cnblogs.com/txws1119/p/6003830.html