Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Query Time Expand / Collapse
Author
Message
Posted Wednesday, May 04, 2005 2:22 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, April 05, 2007 12:54 AM
Points: 25, Visits: 1
I want to qury only time in HH:MM:SS AM/PM fromat from a datetime Field Or SmallDatetime Filed. Can anybody help me?
Post #179699
Posted Wednesday, May 04, 2005 3:14 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, August 09, 2011 12:22 AM
Points: 152, Visits: 116

Try using the 'convert' function i.e -

select convert(char(10),getdate(),8)

Post #179711
Posted Wednesday, May 04, 2005 3:21 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, April 05, 2007 12:54 AM
Points: 25, Visits: 1

Hi,

"select convert(char(10),getdate(),8)"

Good enough, but I need Am/Pm associated  and do not want to use Left or right function
Post #179713
Posted Wednesday, May 04, 2005 3:39 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 11:15 PM
Points: 5,950, Visits: 226
This is merely a presentational issue that you really should handle at the client.

--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/
Post #179721
Posted Thursday, May 05, 2005 8:11 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, May 24, 2008 9:03 AM
Points: 4, Visits: 5

Here is a UDF I was playing with just last night. I'm trying to get time data in a HH:MM AM/PM format from the SQL server.

USE your_db

GO

IF EXISTS (SELECT * FROM dbo.SYSOBJECTS WHERE ID = OBJECT_ID(N'[dbo].[GetAMPM]')
           AND XType IN (N'FN', N'IF', N'TF')) DROP FUNCTION [dbo].[GetAMPM]
GO

CREATE FUNCTION GetAMPM

(@thedate datetime)

RETURNS varchar(20)
AS
BEGIN

Declare
@thenewdate  varchar(20),
@thehour int,
@theminute varchar(20),
@ampm  varchar(20)

SET @thehour = DATEPART(hh, @thedate)

SET @thehour = (case when DATEPART(hh, @thedate)>12 then DATEPART(hh, @thedate)-12
  when DATEPART(hh, @thedate) = 12 then DATEPART(hh, @thedate)
  Else DATEPART(hh, @thedate) End)

SET @theminute = (case when DATEPART(mi, @thedate)= 0 then '00'
   when DATEPART(mi, @thedate) < 10 then '0' + cast(DATEPART(mi, @thedate) as varchar)
   Else cast(DATEPART(mi, @thedate) as varchar) End)


SET @ampm = (case when DATEPART(hh, @thedate) >= 12 then 'PM' Else 'AM' End)

SET @thenewdate = cast(@thehour as varchar) + ':' + @theminute + ' ' + @ampm

RETURN (@thenewdate)

END




Post #180186
Posted Thursday, May 05, 2005 1:55 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: 2 days ago @ 12:33 PM
Points: 12, Visits: 56

Vijay,

it is not clear to me why you "...do not want to use left or right function". Are you looking for some new features in Yukon?

Anyway, here are statements that might be helpfull:

declare @date datetime, @smalldate smalldatetime
SELECT @date=getdate(),@smalldate=getdate()

SELECT right('0'+ltrim(substring(CONVERT(varchar,@date,22),10,11)),11)
SELECT right('0'+ltrim(substring(CONVERT(varchar,@smalldate,22),10,11)),11)

09:14:11 AM
09:14:00 AM

--This one does not use left or right.

SELECT reverse(substring(rtrim(substring(reverse(CONVERT(varchar,GETDATE(),22)),1,11))+'0',1,11))

09:14:11 AM


 

 




Post #180357
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse