SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
renu.iitkgp
renu.iitkgp
Valued Member
Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)

Group: General Forum Members
Points: 73 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.
dwain.c
dwain.c
SSCrazy Eights
SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)

Group: General Forum Members
Points: 9505 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
renu.iitkgp
renu.iitkgp
Valued Member
Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)

Group: General Forum Members
Points: 73 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.
:-)
renu.iitkgp
renu.iitkgp
Valued Member
Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)

Group: General Forum Members
Points: 73 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.
dwain.c
dwain.c
SSCrazy Eights
SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)

Group: General Forum Members
Points: 9505 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
renu.iitkgp
renu.iitkgp
Valued Member
Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)

Group: General Forum Members
Points: 73 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. :-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search