DateTime breaking my SQL

WHERE (dbo.ED_ATTENDANCE.AttendDateTime >= CONVERT(DATETIME, '2015-04-01 00:00:00', 102))

Hi guys is there anything wrong with the above WHERE clause? How the data outputs is displayed as 09/04/2015  13:07:00 normally, so don't see why SQL Management Studio has put in the Convert. Its this line thats timing me out? Any thoughts I could try

April 22nd, 2015 9:45am

Hi ProfessorFudger,

Could you Please clarify 'breaking my SQL'. What is the error message?

What is the datatype of AttendDateTime?

As such there shouldn't be anything wrong with usage of the above where clause

Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 9:52am

The optional style element of the CONVERT function is for converting to one of the string datatypes. It doesn't apply when converting to datetime.

But... That doesn't really matter in this case because the CONVERT function isn't even needed... The following will work just fine.

WHERE dbo.ED_ATTENDANCE.AttendDateTime >= '2015-04		
April 22nd, 2015 9:57am

It times me out when I add the above into my SQL, and runs in seconds when I take it back out. The field is a DateTime field which outputs as 09/04/2015  13:07:00

Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 9:57am

This could be because there is no index on the column you are filtering the date on?

Look at the Query Estimation Plan to see if on this filter it is costing a lot of resource.

April 22nd, 2015 10:07am

The optional style element of the CONVERT function is for converting to one of the string datatypes. It doesn't apply when converting to datetime.

But... That doesn't really matter in this case because the CONVERT function isn't even needed... The following will work just fine.

WHERE dbo.ED_ATTENDANCE.AttendDateTime >= '2015-04		
Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 10:37am

The optional style element of the CONVERT function is for converting to one of the string datatypes. It doesn't apply when converting to datetime.

But... That doesn't really matter in this case because the CONVERT function isn't even needed... The following will work just fine.

WHERE dbo.ED_ATTENDANCE.AttendDateTime >= '2015-04		
April 22nd, 2015 10:44am

Instead of using '2015-04-01' try using '01-Apr-
Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 10:53am

>Sorry buddy Timeout again

So you have a query that's taking a long time to execute, and have ruled out the wrapping of the datetime string in a convert function as the culprit.

You'll now need to share more information about your issue.

David

April 22nd, 2015 11:01am

The optional style element of the CONVERT function is for converting to one of the string datatypes. It doesn't apply when converting to datetime.

But... That doesn't really matter in this case because the CONVERT function isn't even needed... The following will work just fine.

WHERE dbo.ED_ATTENDANCE.AttendDateTime >= '2015-04		
Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 12:09pm

You can also use 

YYYYMMDD format which is universal and always treated the same.

April 22nd, 2015 12:11pm

You can also use 

YYYYMMDD format which is universal and always treated the

Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 5:00pm

A contractor who is an SQL expert Im told came up with this:  

If the previous solution is taking too long to complete (7 minutes) and getting timed out use this one. I have Pimped it a bit and should run under 40 seconds   IF OBJECT_ID(N'tempdb..#TempTable', N'U') IS NOT NULL       DROP TABLE #TempTable   SELECT      InternalNo,             AENo,             AttendDateTime,             DepartDateTime,             MethOfDepartureDesc,             MFPatientID INTO #TempTable               FROM dbo.ED_ATTENDANCE WHERE dbo.ED_ATTENDANCE.DepartDateTime IS NOT NULL AND AttendDateTime >= '2015 apr 01'  -- 7028 (33 seconds   SELECT  ED_ATTENDANCE.AENo AS [ED Number 1],       ED_ATTENDANCE_1.AENo AS [ED Number 2],       ED_ATTENDANCE.AttendDateTime AS [Attendance Time/Date 1],       ED_ATTENDANCE_1.AttendDateTime AS [Attendance Time/Date 2],       DATEDIFF(MINUTE, ED_ATTENDANCE.AttendDateTime, ED_ATTENDANCE_1.AttendDateTime) AS 'Minutes Between Attendances',       ED_ATTENDANCE.DepartDateTime AS [Departure Time/Date 1],       ED_ATTENDANCE_1.DepartDateTime AS [Departure Time/Date 2],       dbo.MF_PATIENT.FullName AS [Patients Full Name],       dbo.MF_PATIENT.NHSNo1 AS [NHS Number],       dbo.MF_PATIENT.HEYNo AS [HEY Number],       ED_ATTENDANCE.MethOfDepartureDesc AS [Departure Method 1],       ED_ATTENDANCE_1.MethOfDepartureDesc AS [Departure Method 2] FROM #TempTable ED_ATTENDANCE LEFT JOIN dbo.MF_PATIENT ON ED_ATTENDANCE.MFPatientID = dbo.MF_PATIENT.MFPatientID INNER JOIN #TempTable ED_ATTENDANCE_1 ON ED_ATTENDANCE.InternalNo = ED_ATTENDANCE_1.InternalNo AND DATEDIFF(MINUTE, ED_ATTENDANCE.AttendDateTime, ED_ATTENDANCE_1.AttendDateTime) >= 0.01 AND DATEDIFF(MINUTE, ED_ATTENDANCE.AttendDateTime, ED_ATTENDANCE_1.AttendDateTime) <= 10

However it seems a long winded way to get around a simple datetime issue, or as a total newbie to sql am i wrong?

April 22nd, 2015 5:38pm

My original SQL was: 

SELECTdbo.ED_ATTENDANCE.AENo AS [ED Number 1],
      ED_ATTENDANCE_1.AENo AS [ED Number 2],
      dbo.ED_ATTENDANCE.AttendDateTime AS [Attendance Time/Date 1],
      ED_ATTENDANCE_1.AttendDateTime AS [Attendance Time/Date 2],
      DATEDIFF(MINUTE, dbo.ED_ATTENDANCE.AttendDateTime, ED_ATTENDANCE_1.AttendDateTime) AS 'Minutes Between Attendances',
      dbo.ED_ATTENDANCE.DepartDateTime AS [Departure Time/Date 1],
      ED_ATTENDANCE_1.DepartDateTime AS [Departure Time/Date 2],
      dbo.MF_PATIENT.FullName AS [Patients Full Name],
      dbo.MF_PATIENT.NHSNo1 AS [NHS Number],
      dbo.MF_PATIENT.HEYNo AS [HEY Number],
      dbo.ED_ATTENDANCE.MethOfDepartureDesc AS [Departure Method 1],
      ED_ATTENDANCE_1.MethOfDepartureDesc AS [Departure Method 2]
FROMdbo.ED_ATTENDANCE
INNER JOINdbo.ED_ATTENDANCE AS ED_ATTENDANCE_1 ON dbo.ED_ATTENDANCE.InternalNo = ED_ATTENDANCE_1.InternalNo
LEFT JOINdbo.MF_PATIENT ON dbo.ED_ATTENDANCE.MFPatientID = dbo.MF_PATIENT.MFPatientID
WHERE(dbo.ED_ATTENDANCE.DepartDateTime IS NOT NULL)
      AND (ED_ATTENDANCE_1.DepartDateTime IS NOT NULL)
      AND (dbo.ED_ATTENDANCE.AttendDateTime >= '2015-04-01')
      AND (ED_ATTENDANCE_1.AttendDateTime >= '2015-04-01')
      AND (DATEDIFF(MINUTE, dbo.ED_ATTENDANCE.AttendDateTime, ED_ATTENDANCE_1.AttendDateTime) > 0.01)
      AND (DATEDIFF(MINUTE, dbo.ED_ATTENDANCE.AttendDateTime, ED_ATTENDANCE_1.AttendDateTime) < 10)
ORDERBY [Attendance Time/Date 1]

Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 5:40pm

Looking at this query, I think I can see what the author was trying to accomplish...  Just not going about it in the fastest way possible...

Couple questions...

What version of SQL Server are you using?

Am I correct in assuming that "InternalNo" repeats with each attendance of a given patient?

And no... It's not necessarily long winded... Just not as efficient as it probably cou

April 22nd, 2015 9:27pm

Minor points for improvement

What's wrong with a between instead of >= and <=?

DATEDIFF returns an INT, no point making it convert to a decimal.

From my limited time using SQL Server, I've found it isn't the best at conversions so prevent it converting when it doesn't need to.

By the looks of the query, attempting to identify how quickly subsequent patients are seen? If so, you would only need to look at the next record in line, not every record thereafter (as a way of reducing the Cartesian)

Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 10:05pm

Hi Jason, InternalNo is indeed a patient identifier which can have multiple attendances. I am using SQL Server 2008.

What I am doing in this is looking for possible duplicate attendances where a patient has been recorded twice within 10 minutes (this shouldn't and rarely happens). 

April 23rd, 2015 1:27am

I feel a right idiot now, the answer was obvious, rather tha using the AttendDateTime field I just used the AttendFinYear field which did the trick. Sorry guys I am just 9 days into learning SQL and jumping in at the deep end here. I had the fields below to pick from:

AttendDateTime
AttendDate
AttendDay
AttendHour
AttendWeekStart
AttendMonthYear
AttendMonthYearSort
AttendMonth
AttendFinQtr
AttendFinYear
Free Windows Admin Tool Kit Click here and download it now
April 23rd, 2015 3:24am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics