inverting datetime!

  • Hi

    i have this line in my store procedure:

    select * from MainVideo where LEFT (datetime,11)=LEFT (@VarDateTime,11)

    but the problem is : datetimeis like this : 2012-03-12

    but @VarDateTime is like this : 13/03/2012

    so i can not compare these two

    would you please help me?

  • select * from MainVideo where CONVERT(date,datetime) = Convert(date,@VarDateTime,101)

    The additional 101paramater is the style, in this case US .

    Look at http://msdn.microsoft.com/en-us/library/ms187928(v=sql.105).aspx for additional styles/

  • stevro (4/15/2012)


    select * from MainVideo where CONVERT(date,datetime) = Convert(date,@VarDateTime,101)

    The additional 101paramater is the style, in this case US .

    Look at http://msdn.microsoft.com/en-us/library/ms187928(v=sql.105).aspx for additional styles/

    Correction I see it is style 103(British).

    It should be:

    select * from MainVideo where CONVERT(date,datetime) = Convert(date,@VarDateTime,103)

  • On SQL Server 2008 - I would recommend using CAST({value} AS DATE) instead. This has been optimized so it can still use index seeks where possible and will still use the DATE data type instead of character which would be slower.

    Select mv.* -- don't use * in your actual code

    From MainVideo mv

    Where CAST(mv.[date] AS DATE) = @VarDate

    I would also not set the variable to a datetime data type - define it as DATE so you don't have an extra cast in the code.

    Jeffrey Williams
    Problems are opportunities brilliantly disguised as insurmountable obstacles.

    How to post questions to get better answers faster
    Managing Transaction Logs

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

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