September 29, 2011 at 8:48 am
Hi I am trying to extract just the hours and minutes form getdate. The best I have come up with is
declare @date datetime
set @date = getdate()
select @date
select DATEPART (HH,@date), DATEPART(MI,@date)
however that outputs 2 separate columns. Any ideas how I can do this?
Thanks
September 29, 2011 at 8:54 am
select CAST(DATEPART (HH,@date) AS VARCHAR(2))+':'+ CAST(DATEPART(MI,@date) AS VARCHAR(2))
September 29, 2011 at 8:56 am
and another way, since you posted in SQL2208, is to consdier the tIME datatype to get what yo are after as well:
--returned 10:53:47.4970000 and 10:53 for me at that moment
select
CONVERT(time,getdate()),
CONVERT(varchar(5),CONVERT(time,getdate()))
Lowell
September 29, 2011 at 8:57 am
Sorry, I missed a boundry value.
IF LEN(CAST(DATEPART(MI,@date) AS VARCHAR(2))) < 2
select CAST(DATEPART (HH,@date) AS VARCHAR(2))+':0'+ CAST(DATEPART(MI,@date) AS VARCHAR(2))
ELSE
select CAST(DATEPART (HH,@date) AS VARCHAR(2))+':'+ CAST(DATEPART(MI,@date) AS VARCHAR(2))
September 29, 2011 at 9:28 am
Thats great thanks guys
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply