最近写了一个字符串变日期的函数,我想取当前日期,用getdate()sql中报错,我加了dbo.getdate()后vs2008中报错,说没有dbo.getdate() 能解决吗?
我有一种想法,写一个dbo.getdate()诸位帮我写写哈,我也写一个对比一下
create function getdate
returns datetime
as .......没法写了,写不了了
获得当前日期,难吗?????????
--创建一个函数,将字符串转化为日期,如果无法转化,那么就设为当前时间
alter FUNCTION NvarcharToDateTime(@datetime nvarchar(200))
RETURNS Datetime
AS
BEGIN
declare @resdatetime/*result datetime*/ datetime,@strnow/*to store the string of the time now*/ nvarchar(50)
set @strnow=cast(CONVERT(varchar(10),dbo.getDate()/*attention ,must dbo ,else it will give a error infomation*/,120) as nvarchar(200))
set @resdatetime=cast(@datetime as datetime)
if @@error <>1
begin
select @resdatetime=cast(@strnow as datetime)
end
return @resdatetime
END
我有一种想法,写一个dbo.getdate()诸位帮我写写哈,我也写一个对比一下
create function getdate
returns datetime
as .......没法写了,写不了了
获得当前日期,难吗?????????
--创建一个函数,将字符串转化为日期,如果无法转化,那么就设为当前时间
alter FUNCTION NvarcharToDateTime(@datetime nvarchar(200))
RETURNS Datetime
AS
BEGIN
declare @resdatetime/*result datetime*/ datetime,@strnow/*to store the string of the time now*/ nvarchar(50)
set @strnow=cast(CONVERT(varchar(10),dbo.getDate()/*attention ,must dbo ,else it will give a error infomation*/,120) as nvarchar(200))
set @resdatetime=cast(@datetime as datetime)
if @@error <>1
begin
select @resdatetime=cast(@strnow as datetime)
end
return @resdatetime
END
我有一种想法,写一个dbo.getdate()诸位帮我写写哈,我也写一个对比一下
create function getdate
returns datetime
as .......没法写了,写不了了
获得当前日期,难吗?????????
--创建一个函数,将字符串转化为日期,如果无法转化,那么就设为当前时间
alter FUNCTION NvarcharToDateTime(@datetime nvarchar(200))
RETURNS Datetime
AS
BEGIN
declare @resdatetime/*result datetime*/ datetime,@strnow/*to store the string of the time now*/ nvarchar(50)
set @strnow=cast(CONVERT(varchar(10),dbo.getDate()/*attention ,must dbo ,else it will give a error infomation*/,120) as nvarchar(200))
set @resdatetime=cast(@datetime as datetime)
if @@error <>1
begin
select @resdatetime=cast(@strnow as datetime)
end
return @resdatetimeEND
sql里
select getdate()就可以了吧
alter FUNCTION NvarcharToDateTime(@datetime nvarchar(200))
RETURNS Datetime
AS
BEGIN
declare @resdatetime datetime
if not exists (select CONVERT(datetime,@datetime,120))
rollback;
else
begin
select @resdatetime= CONVERT(datetime,@datetime,120)
end
return @resdatetime
END
在SQL SERVER 2005就可以..所以SQL SERVER 2000的方法是.使用一个视图.来查询GETDATE().然后在函数里SELECT它.
CREATE VIEW v_GetDate
AS
SELECT GETDATE() AS [now];
GOCREATE FUNCTION dbo.fn_name()
RETURNS DATETIME
BEGIN
DECLARE @dt DATETIME; SELECT @dt = [now] FROM v_GetDate; RETURN @dt;
END
GO
RETURNS Datetime
AS
BEGIN
declare @resdatetime datetime
if exists (select CONVERT(datetime,@datetime,120))
begin
select @resdatetime= CONVERT(datetime,@datetime,120)
end
return @resdatetime
ENDselect dbo.NvarcharToDateTime ('2009-10-9')
/*-----------------------
2009-10-09 00:00:00.000(1 行受影响)*/
drop view aa
gocreate view aa
as
select getdate() as [now]
gocreate function fn_tt()
returns datetime
begin
declare @date datetime
select @date=[now] from aa
return @date
end
drop view aa
gocreate view aa
as
select getdate() as [now]
go
if object_id('ccc','fn') is not null
drop function ccc
go
create function ccc()
returns datetime
begin
declare @date datetime
select @date=[now] from aa
return @date
endselect dbo.ccc() as 当前时间
/*
当前时间
1 2009-07-12 22:38:56.653
*/