Concatenation Error

  • Can anyone tell me what's wrong with this query:

    Select (sum(Datediff(HOUR,clkIn, clkOut))+ ':' + sum(Datediff(MINUTE,clkIn, clkOut))) as "Total Hours" from History WHERE CONVERT(CHAR(10),clkIn,1) = '05/20/10'

    I get the following error:

    Conversion failed when converting the varchar value ':' to data type int.

    I'm trying to calculate total hours and minutes an employee was clocked in. I would like the field to display in this format: "00:00"

    Thanks!!

  • Select convert(varchar(2),(sum(Datediff(HOUR, clkin, clkout))))+ ':'

    + convert(varchar(2),sum(Datediff(MINUTE, clkin, clkout))) as "Total Hours"

    You're trying to add a character ':' to integers.

    You have to convert (or cast) the integer results to characters in order to concatenate properly.

    ______________________________________________________________________________________________
    Forum posting etiquette.[/url] Get your answers faster.

  • calvo (5/20/2010)


    Select convert(varchar(2),(sum(Datediff(HOUR, clkin, clkout))))+ ':'

    + convert(varchar(2),sum(Datediff(MINUTE, clkin, clkout))) as "Total Hours"

    You're trying to add a character ':' to integers.

    You have to convert (or cast) the integer results to characters in order to concatenate properly.

    THANKS!! It worked!

  • Jdoggz (5/20/2010)


    Can anyone tell me what's wrong with this query:

    Select (sum(Datediff(HOUR,clkIn, clkOut))+ ':' + sum(Datediff(MINUTE,clkIn, clkOut))) as "Total Hours" from History WHERE CONVERT(CHAR(10),clkIn,1) = '05/20/10'

    I get the following error:

    Conversion failed when converting the varchar value ':' to data type int.

    I'm trying to calculate total hours and minutes an employee was clocked in. I would like the field to display in this format: "00:00"

    Thanks!!

    Just a suggestion.... even if that code did work, it's going to do a scan even if a full covering index is available because the WHERE clause isn't sargeable. Anytime you put a formula around a table column in the WHERE clause (there are the occasion exceptions but this isn't one of them), you almost guarantee a table scan. Of course, that can make for some very slow code compared to when an index seek kicks in.

    The way to write the WHERE clause for this code is...

    DECLARE @StartDate DATETIME,

    @EndDate DATETIME

    SELECT @StartDate = '20100520', --"ISO" date for 5/20/10

    @EndDate = DATEADD(dd,1,@StartDate) --5/21/10 in this case

    SELECT whatever

    FROM dbo.History

    WHERE clkIn >= @StartDate

    AND clkIn < @EndDate

    That will still catch ALL of the dates and times for the given StartDate even if the new DateTime2 is used. Do NOT (ever) use BETWEEN for this because, obviously, you would be including midnight times for the next day.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply