December 17, 2003 at 12:16 pm
I am trying to get the time value in a 'hh:mm:ss am/pm' format from the getdate() function, can anyone please tell me how to achieve that? Many thanks!
December 17, 2003 at 12:25 pm
Select Convert(Varchar(8), GetDate(), 8)
Once you understand the BITs, all the pieces come together
December 17, 2003 at 12:29 pm
Thanks Thomas for the reply, the below statement outputs the time in 24h, but I needed it to display in hh:mm:ss AM/PM format.
--------------------------------------------
Select Convert(Varchar(8), GetDate(), 8)
[/quote]
December 17, 2003 at 12:40 pm
Select SubString(Convert(Varchar(25), GetDate(), 9), 13, 8) + Right(Convert(Varchar(25), GetDate(), 9), 1) + 'M'
Once you understand the BITs, all the pieces come together
December 17, 2003 at 1:39 pm
SELECT RIGHT(STUFF(CONVERT(char(26),GETDATE(),9),21,4,' '),10)
--Jonathan
--Jonathan
December 18, 2003 at 3:09 am
How about something like:
print Convert( Char(30), GetDate(), 108)
print Convert( Char(30), GetDate(), 109)
See BOL for a complete list with format numbers
December 18, 2003 at 4:46 am
Johnathan's method works very well, but a slight amendment is required otherwise it cuts the first character off the front. The command should read :
SELECT RIGHT(STUFF(CONVERT(char(26),GETDATE(),9),21,4,' '),11)
Thanks Jonathan. That's a concise answer to a frequent problem.
December 18, 2003 at 7:56 am
I have a need to use many date formats. The following SP lists the conversion codes and the length of the output.
CREATE procedure pDisplayDateConversionStyles as
print 'datetime to Varchar conversion codes'
print 'syntax: convert(varchar(length),getdate(),code)'
print ''
declare @i tinyint
declare @Now datetime
set @Now = getdate()
set @i = 0
while @i < 15
begin
print 'Length: ' + cast(len(convert(varchar(35),@Now,@i)) as varchar(3)) + 'Conversion code: ' + str(@i,3,0) + ' Returns: ' + convert(varchar(35),@Now,@i)
set @i = @i + 1
end
print 'Length: ' + cast(len(convert(varchar(35),@Now,20)) as varchar(3)) + 'Conversion code: ' + str(20,3,0) + ' Returns: ' + convert(varchar(35),@Now,20)
print 'Length: ' + cast(len(convert(varchar(35),@Now,21)) as varchar(3)) + 'Conversion code: ' + str(21,3,0) + ' Returns: ' + convert(varchar(35),@Now,21)
set @i = 100
while @i < 115
begin
print 'Length: ' + cast(len(convert(varchar(35),@Now,@i)) as varchar(3)) + 'Conversion code: ' + str(@i,3,0) + ' Returns: ' + convert(varchar(35),@Now,@i)
set @i = @i + 1
end
print 'Length: ' + cast(len(convert(varchar(35),@Now,120)) as varchar(3)) + 'Conversion code: ' + str(120,3,0) + ' Returns: ' + convert(varchar(35),@Now,120)
print 'Length: ' + cast(len(convert(varchar(35),@Now,121)) as varchar(3)) + 'Conversion code: ' + str(121,3,0) + ' Returns: ' + convert(varchar(35),@Now,121)
print 'Length: ' + cast(len(convert(varchar(35),@Now,126)) as varchar(3)) + 'Conversion code: ' + str(126,3,0) + ' Returns: ' + convert(varchar(35),@Now,126)
print 'Length: ' + cast(len(convert(varchar(35),@Now,130)) as varchar(3)) + 'Conversion code: ' + str(130,3,0) + ' Returns: ' + convert(varchar(35),@Now,130)
print 'Length: ' + cast(len(convert(varchar(35),@Now,131)) as varchar(3)) + 'Conversion code: ' + str(131,3,0) + ' Returns: ' + convert(varchar(35),@Now,131)
December 18, 2003 at 10:33 am
select convert(char(8),getdate(),112) as yyyymmdd
select convert(char(8),getdate(),108) as [hh:mm:ss]
select convert(char(8),getdate(),1) as [mm/dd/yy]
outputs
yyyymmdd
--------
20031218
(1 row(s) affected)
hh:mm:ss
--------
11:28:57
(1 row(s) affected)
mm/dd/yy
----------
12/18/03
(1 row(s) affected)
The options are almost endless. See BOL->"Cast and Convert"
December 18, 2003 at 11:38 am
You still can you Johnathan's method:
SELECT RIGHT(STUFF(CONVERT(char(26),GETDATE(),9),21,4,' '),11)
Minh Vu
December 18, 2003 at 1:30 pm
Thanks MORRIJL for your SP. It comes in handy.
December 19, 2003 at 2:36 am
Can be used this way also
quote:
Create Function DateOnly(@dt DateTime)returns Datetime
as
begin
return Cast(convert(varchar(8),@dt,1) AS datetime)
end
CREATE Function TimeOnly(@dt DateTime)
returns datetime
as
begin
return Cast(convert(varchar(8),@dt,8) AS datetime)
end
Select dbo.Dateonly(getdate()),dbo.TimeOnly(getdate())
Rohit
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply