SQL Server Performance

Comparing Dates

Discussion in 'SQL Server 2005 General Developer Questions' started by deepakleo20031, Aug 10, 2007.

  1. deepakleo20031 New Member

    I am using SP and passing a date field (@From) from the Frontend.
    Now i want to search all those records whose @From value is greater than or equalto @CreatedDate(field in the database)
    Here @CreatedDate is datetime field in the database
    And @From is the parameter that am passing in SP and its a varchar(50) datatype
    Pls help me
    Any help is greatly appreciated.
  2. MichaelB Member

    select * from mytable where @from >= cast(createddate as datetime)
  3. ndinakar Member

    If your field in the DB is datetime type why is your front end passing a varchar type? Shouldnt it be datetime too? Keeping datatypes consistent will maintain data integrity and avoid unnecessary screw ups converting back and forth.
    SELECT * FROM YourTable WHERE CreatedDate <= Convert(Datetime, @From)
  4. dineshasanka Moderator

    select * from mytable where datediff(d,@from,cast(createddate as datetime) ) >= 0
  5. Adriaan New Member

    You can use a string representation of a date to compare, really no need to cast or anything.
    One big issue is that you need to force either the US mm/dd/yyyy or the 'universal' yyyy-mm-dd format on the string.
    Also note that the date delimiter must be "-" or "/", and the time delimiter must be ":". Especially if you use a "." as date or time delimiter, you will often get a run-time error.
    Check out Frank Kalis's very complete article on datetime issues here on the main site.
  6. FrankKalis Moderator

  7. satya Moderator

    I believe thats not, very valueable for all DATETIME shenanigans [:D]
  8. Madhivanan Moderator

    While 1=1
    print 'Always use proper DATETIME datatype'
  9. Madhivanan Moderator

  10. FrankKalis Moderator

    that might turn out a performance killer, since an index on the date column could not be used.

Share This Page