SQL中的时间的问题
一。DateTime类型的去掉时间部分--转化为字符 来显示
CONVERT(varchar(10),
dbo.tbl_EmpContract.ConEndDate, 111) AS ConEndDate, 这种 格式是 2006/01/02 导到Excel中 就是正常的显示格式 2006-1-2
二。 convert(varchar(10),单据日期,120) 格式 2006-1-2
三。 SET DATEFORMAT YMD 将默认的设成某一格式,如年月日
四。 startdate 2005/1/1 和 enddate 2006/1/1 不要日 来比较 只要 年月
convert(char(7),startdate ,120)
convert(datetime,year(startdate)+'-'+month(startdate)+'-01')
convert(datetime,year(enddate)+'-'+month(enddate)+'-01')
datepart(yy,startdate)=datepart(yy.enddate) and datepart(mm,startdate))=datepart(mm.enddate)
五。 查询 SKTime 日期 2006-1-1(包括0点到23-59-59) 至 2006-1-10(包括0点到23-59-59) 日的记录
1.
select * from tablename where convert(varchar(10),SKTime,120)>='2006-01-01' and convert(varchar(10),SKTime,120)<'2006-01-11'
2.
select * from Table where SKTime between '2006-1-1' and '2006-1-11'
以上查询:
SKTime 是datetime 或 smalldatetime 类型才行
3.
select * from Table where SKTime >='2006-1-1' and SKTime<'2006-1-11'
4.
select * from a
where sktime between '2006-1-1' and '2006-1-10'
5.
select * from Table where SKTime between '2006-01-01 00:00:00' and '2006-01-10 23:59:59.997'
六 对日期进行模糊查询
select * from abc where id like 'a%'
如果是日期型进行模糊查询,该怎么写,比如我要对2005年10月份的所有数据进行 查询怎么写?
1.
select * from 表名 where datediff(month,日期字段,'2005-10-1')=0
2.
select * from abc where datepart(year,shijianziduan)=2005 and datepart(month,shijianziduan)=10
3.
select * from abc where date between '2005-10-1' and '2005-10-31'
4.
可以先转换成字符型,再用%呀
|
参考: Syntax CAST(expression AS data_type) Using CONVERT: CONVERT (data_type[(length)], expression [, style]) Arguments
Without Standard Input/Output**
Important By default, SQL Server interprets two-digit years based on a cutoff year of 2049. That is, the two-digit year 49 is interpreted as 2049 and the two-digit year 50 is interpreted as 1950. Many client applications, such as those based on OLE Automation objects, use a cutoff year of 2030. SQL Server provides a configuration option (two digit year cutoff) that changes the cutoff year used by SQL Server and allows the consistent treatment of dates. The safest course, however, is to specify four-digit years. |
浙公网安备 33010602011771号