Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Comparision of a datetime variable against a column of the type nvarchar Expand / Collapse
Author
Message
Posted Sunday, September 2, 2012 11:27 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 2, 2014 9:03 AM
Points: 15, Visits: 57
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.
Post #1353340
Posted Sunday, September 2, 2012 11:54 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, November 24, 2014 4:53 AM
Points: 3,422, Visits: 5,368
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1353344
Posted Monday, September 3, 2012 1:32 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 2, 2014 9:03 AM
Points: 15, Visits: 57
Thank you Dwain, got that. My bad, I should've tried with:
CASE WHERE (BLADIDAA) THEN (TITUMTITUM) ELSE (DIDADIDAA) END

before I posted here.
Post #1353369
Posted Tuesday, September 4, 2012 9:15 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 2, 2014 9:03 AM
Points: 15, Visits: 57
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.
Post #1353984
Posted Tuesday, September 4, 2012 6:38 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, November 24, 2014 4:53 AM
Points: 3,422, Visits: 5,368
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1354287
Posted Wednesday, September 5, 2012 6:03 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 2, 2014 9:03 AM
Points: 15, Visits: 57
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.
Post #1354456
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse