Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 trying to select Left and right together

Author  Topic 

masterdineen
Aged Yak Warrior

550 Posts

Posted - 2011-10-13 : 06:33:38
Hello everyone.

i have the below statement to select left and right of getdate()

declare @TimeStamp varchar(10)
select @TimeStamp = ( select right (getdate(), 7))
select left (@TimeStamp, 5)

but when i try and create a function with the above i get an error.

function script i am using

-------------------------------------------------------------------
create function [dbo].[TimeStamp] ()

returns varchar(10)
as

begin

declare @TimeStamp varchar(10)
select @TimeStamp = ( select right (getdate(), 7))

return

select left (@TimeStamp, 5)

end
---------------------------------------------------------------------


MCTS / MCITP certified

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-10-13 : 06:45:00
You are not allowed to use non-deterministic functions inside other functions...i other words: you will have to pass getdate() as a parameter to the function to make it work.

- Lumbago
My blog-> http://thefirstsql.com/2011/07/08/how-to-find-gaps-in-identity-columns-at-the-speed-of-light/
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-13 : 06:48:19
You actually can use getdate() inside a function from SQL 2008 onwards.

What version of SQL Server are you using ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

masterdineen
Aged Yak Warrior

550 Posts

Posted - 2011-10-13 : 06:49:53
what about a plan select statement.

can you combine left and right in one statement like you would with datepart, datediff.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-13 : 06:50:56
i guess you must be not using SQL 2008 else you would have used the time data type


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-13 : 06:51:29
quote:
Originally posted by masterdineen

what about a plan select statement.

can you combine left and right in one statement like you would with datepart, datediff.



Yes. you can


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-13 : 06:52:27
if you just want to get the current time in string HH:MM:SS


select convert(varchar(8), getdate(), 108)



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

masterdineen
Aged Yak Warrior

550 Posts

Posted - 2011-10-13 : 06:53:27
o my god, of course. sorry to waste your time.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-13 : 08:30:46
no problem


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -