Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Function and Getdate()

Posted on 2011-03-01
8
507 Views
Last Modified: 2012-05-11
I have a table that holds cutoffhours like this:
--
1900-01-01 15:00:00.000
1900-01-01 17:00:00.000--


Below SQL works and I get values like this:
2011-03-01 15:00:00.000
2011-03-01 17:00:00.000
select  convert(varchar(10), getdate(), 111) + cutoffhour
from cutoff

---Now, i want to have this in a function and I cant use getdate() in a function. I need to pass getdate() to it. I tried this but get an error to use "convert" . I know i need to use "convert" around cutoffhour ...did that but dont get the "time" part of cutoffhour field
Create function dbo.test
(
  @CurrentTime datetime
)
RETURNS DATETIME
As
Begin

select @cutoff = convert(varchar(10), @CurrentTime, 111) + cutoffhour --somehow need to use convert with cutoffhour to get 2011-03-01 15:00:00.000 (today's date plus the time in that table)
   from cutoff 

...
end

Open in new window

0
Comment
Question by:Camillia
8 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 35009427
What data type is cutoff hour?
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35009461

Create function dbo.test
RETURNS DATETIME
As
Begin

select @cutoff = convert(varchar(10), GETDATE(), 111) + cutoffhour
   from cutoff

...
end
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 35009465
No, you don't need to pass in getdate(), but cutoffhour. Also your convert creates a varchar string, not a datetime. This varchar string then may be implictly converted, as the string format is a valid datetime, but lookup convert in the t-sql helpfile, you'll see that convert(varchar(10),...) converts to varchar(10).

Within the user defined function you're not in the context of the table and cannot adress table fields. But you can make use of any standard/syste T-SQL function within your function.

Create Function dbo.cutoffstring
(
  @cutoffhour varchar(13)
)
RETURNS varchar(23)
As
Begin
   return convert(varchar(10), getdate(), 111) + @cutoffhour
End

Open in new window


Then can use this function via
Select dbo.cutoffstring(cutoffhour) from cutoff

Open in new window


Bye, Olaf.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 7

Author Comment

by:Camillia
ID: 35010193
You cannot have getdate() in a function.

cutoffhour is datetime...Brandon....
0
 
LVL 7

Author Comment

by:Camillia
ID: 35010450
this is the solution;

convert(char(10), @currentTime, 111) + CONVERT(VARCHAR(8),cutoffhour,108)

0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 35011235
Finally convert it to datetime from varchar.

CONVERT(DATETIME,CONVERT(VARCHAR(10),@currentTime,111) + ' ' + CONVERT(VARCHAR(8),currentTime,108))
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 35011267
Yes, if cutoffhour is not varchar, you cannot add it to another varchar.  I'd choose 112 style over 111 for the date portion and style 114 over 108 for the time portion. And finally do a explicit conversion via cast.

cast(datetime,convert(char(8), @currenttime, 112) + CONVERT(char(12), cutoffhour,114) as datetime)

Bye, Olaf.

PS: in SQL2008 you can use getdate() in a UDF, as usage of nondeterministi functions is relaxed there. How do I know what version of SQL Server you are using, Farzadw?

 
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 35011658
This is a full functional example:

IF OBJECT_ID (N'dbo.overridetime') IS NOT NULL
   DROP FUNCTION dbo.overridetime;
GO

CREATE FUNCTION dbo.overridetime(@currenttime datetime, @cutoffhour datetime)
RETURNS datetime
WITH EXECUTE AS CALLER
AS
BEGIN
  return convert(char(8), @currenttime, 112)+' '+convert(char(12), @cutoffhour,114)
END;
GO

IF OBJECT_ID(N'tempdb.dbo.#tmp') IS NULL
BEGIN
   create table #tmp (cutoffhour datetime);  
   insert into #tmp values ('19000101 15:00:00.000');
END;

select dbo.overridetime(getdate(),cutoffhour) as 'mydatetime' from #tmp

Open in new window


Bye, Olaf.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
T-SQL 10 33
MIcrosoft SQL 2014 Database Copy Question 16 51
incorrect syntax near the order by 10 39
Download ms sql express. 2 25
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question