sql server中新建查询里显示从字符串转换到datetime出错

这个是系统的时间的格式问题:

例如:

GO

SET NOCOUNT ON

DECLARE @startdate datetime,@enddate datetime

SET @startdate=’1/7/2008 12:12 AM’

SET @enddate=’11/10/2009 12:OO AM’

SELECT DATEDIFF(year,@startdate,@enddate)

SELECT DATEDIFF(month,@startdate,@enddate)

SELECT DATEDIFF(day,@startdate,@enddate)

SELECT DATEDIFF(minute,@startdate,’l/8/2007 12:17 AM’)

SELECT DATEDIFF(minute,@startdate,GETDATE())

SET NOCOUNT OFF

G0


就会出错,但是你调试一下

go
SET NOCOUNT ON
DECLARE @startdate datetime, @enddate datetime
SET @startdate= '2008/7/1 12:12 AM'
SET @enddate= '2009/10/11 12:00 AM'
SELECT DATEDIFF(year ,@startdate, @enddate)
SELECT DATEDIFF(month ,@startdate, @enddate)
SELECT DATEDIFF(day ,@startdate, @enddate)
SELECT DATEDIFF(minute, @startdate, '2007/8/1 12:17 AM')
SELECT DATEDIFF(minute, @startdate, GETDATE())
SET NOCOUNT OFF
go

这样的就是正确的,具体的语句语法没错只是系统的时间格式问题。

你可能感兴趣的:(数据库)