Results 1 to 4 of 4

Thread: Problem with Datepart

  1. #1

    Thread Starter
    Fanatic Member vuyiswamb's Avatar
    Join Date
    Jan 2007
    Location
    South Africa
    Posts
    830

    Problem with Datepart

    Good Day all

    i have the Following Query
    Code:
    DECLARE @CurrentTime DATETIME
    SET @CurrentTime = CURRENT_TIMESTAMP
    select tr.Descr [Room], tb.Purpose [Purpose], tb.Description [Description],
    convert(varchar,datepart(hour,tb.starttime))+':'+convert(varchar,datepart(minute,tb.starttime)) [Start Time],
    convert(varchar,datepart(hour,tb.endtime))+':'+convert(varchar,datepart(minute,tb.endtime)) [End Time],
    tu.name [Requested by] from tbl_booking tb inner join tbl_resource tr
    on tb.resources = tr.id
    inner join tbl_user tu on tu.id = tb.RequestedByUser
    where (day(startdate) = day(@CurrentTime))and(month(startdate)=month(@CurrentTime))and(year(startdate)=year(@CurrentTime))and(tb.status=1)
    order by [Room],[Start Time]
    and in the [Start Time]and [End Time] it gives me time that is not Complete

    it Gives this

    14:0

    instead of

    14:00
    Thanks

  2. #2
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Problem with Datepart

    You could pad the output with extra values.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  3. #3
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Problem with Datepart

    Here's an example with GetDate()

    SQL Code:
    1. --Tested On Microsoft SQL Server  2000 - 8.00.2040
    2. select Left('0'+ convert(varchar,datepart(hour,getdate())),2)
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Problem with Datepart

    There is no need to use the DatePart function.

    The Convert function can format the datetime fields into hh:mm.

    Code:
    select tr.Descr [Room], tb.Purpose [Purpose], tb.Description [Description],
    convert(varchar(5), tb.starttime, 108) [Start Time],
    convert(varchar(5), tb.endtime, 108) [End Time],
    tu.name [Requested by] 
    from tbl_booking tb inner join tbl_resource tr

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width