Comparision of a datetime variable against a column of the type nvarchar

  • Hello All,

    (Sorry, if I have posted this question already in another topic)

    I have a table like this:

    MinionRpt

    (

    Id [int], FullName [nvarchar](100), ..., EstDate [nvarchar](100)

    )

    If you noticed, the column [EstDate] (Estimated Date) is nvarchar(100). It need not always contain a date.

    Now I gotta query like this:

    select * from MinionRpt where DateDiff (d, EstDate, @StartDate) <= 0 and DateDiff(d, EstDate, @EndDate) >= 0

    The problem is when we have invalid formats or gibberish for EstDate in some records, the query would throw an error.

    I have no control over the existing data or any future data or even the schema design.

    Can some one tell me, how I should go about it?

    Thanks in advance,

    Venkat R Prasad.

  • First of all, you need to gather your courage and tell your DBA that this design is complete crap and it must be changed. EstDate should be a DATETIME column and should never contain gibberish (you can put that in another column: GARBAGE VARCHAR(100)).

    Of course, you won't do that, so you have two options:

    1. Create a computed column for EstDate2 that is something like:

    CASE WHEN [data is garbage] THEN GETDATE() ELSE EstDate END

    2. Replace EstDate in your query with something like:

    CASE WHEN [data is garbage] THEN @StartDate ELSE EstDate END

    The [data is garbage] is some kind of test of the format to see whether it can be converted to a valid DATETIME value. Since you did not provide the format for the date, I can't tell you what that might be.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Thank you Dwain, got that. My bad, I should've tried with:

    CASE WHERE (BLADIDAA) THEN (TITUMTITUM) ELSE (DIDADIDAA) END

    before I posted here.

    🙂

  • Hi Dwain,

    My query seemed to work on some server which runs SQL server 2000. When I execute the same on SQL Server 2008, I get the error: Conversion failed when converting date and/or time from character string.

    My table is like this:

    CRM_Request

    (

    Id [int] <PK, identity>,

    TRNumber [nvarchar](50),

    .....,

    ExpectedCompletionDate [nvarchar](50),

    UploadDate [nvarchar](50)

    )

    My actual query is this:

    /*@DateSpec = 1 ---> fetch [ExpectedCompletionDate]; 2 ---> fetch [UploadDate] */

    SELECT TRRpt.Id TRId, TRRpt.TRNumber, DateSpecified

    FROM

    (

    select a.Id, a.TRNumber,

    (

    CASE WHEN @DateSpc = 1 THEN a.ExpectedCompletionDate

    ELSE LTRIM(RTRIM(a.UploadDate))END

    ) AS DateSpecified,

    from CRM_Request a

    where

    (

    (@DateSpc = 1 and IsDate(a.ExpectedCompletionDate) = 1)

    or

    (@DateSpc = 2 and IsDate(a.UploadDate) = 1)

    )

    ) TRRpt

    WHERE

    DateDiff(d, TRRpt.DateSpecified, @StartDate) <= 0 and

    DateDiff(d, TRRpt.DateSpecified, @EndDate) >= 0

    If I remove the "where clause" the query throws results with the dates like

    8/31/2011 5:27:44 PM

    09/26/2011

    08/28/2012

    8/7/2007

    06-09-2009

    08/13/10

    08/18/10

    9/9/2010

    As you can see, it has all possible datetime formats for strings.

    Can you plz tell me what seems to be the problem?

    Thanks in advance,

    Venkat R Prasad.

  • ISDATE is sensitive to DATEFORMAT (Google "SQL DATEFORMAT"), so that could be your problem.

    Personally I don't think I'd use it. Perhaps constructing a test (maybe a FUNCTION) for the validation might be more effective because it gives you better control.

    This forum post may give you some additional ideas: http://www.sqlservercentral.com/Forums/Topic1178655-338-2.aspx#bm1183837


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Thank you again Dwain. I already wrote a function which takes in a string and returns datetime. (returns 1/1/1900, if it's an invalid date format or gibberish.) That solved it. 🙂

Viewing 6 posts - 1 through 5 (of 5 total)

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