August 13, 2018 at 8:22 pm
Jonathan AC Roberts - Monday, August 13, 2018 3:34 AMJeff Moden - Sunday, August 12, 2018 3:53 PMJonathan AC Roberts - Sunday, August 12, 2018 2:57 AMJeff Moden - Saturday, August 11, 2018 7:37 PMJonathan AC Roberts - Saturday, August 11, 2018 5:41 PMI don't think anyone has noticed/seen my solution above. You just need to useLEFT(CounterDateTime, 23)
and it will work!
So Daniel can just use:delete from [dbo].[CounterData] WHERE LEFT(CounterDateTime, 23) < DATEADD(day, -10, GETDATE())
to get it to work.I'm a bit paranoid about relying on default formatting. It doesn't cost that much more to force a given format. The calculation on the column in the WHERE clause presents a possible performance problem, as well.
Again and still... you wouldn't need to do any of this if the column were of a correct temporal data type. Even if you couldn't change the column, a persisted/index computed column would likely make this sail.
I've not sure why Microsoft made it write to the database like this. But PerfMon will insert the data onto multiple sources: any database using an ODBC data source, there is also the choice of writing csv files, tab separated files and files with Perfmon's own binary format. So that might be a bit of the reason why the date is a bit odd. The format of the date is 'yyyy-MM-dd hh:mm:ss.000' which SQL server can implicitly convert to datetime.
But the thing that has stopped Daniel's query from working is the 24th character of this column (the string 'yyyy-MM-dd hh:mm:ss.000' is only 23 characters long).Another good reason to not store (except in an import staging table) dates and times as character based data. I wonder what the abhorrent 24th character actually is because a space in that position causes no problems.
SELECT CONVERT(DATETIME,'2018-08-12 17:49:35.123')
,CONVERT(DATETIME,'2018-08-12 17:49:35.123 ')
;From the following query I obtained its ASCII value to be zero.
select CounterDateTime,ASCII(SUBSTRING(CounterDateTime,24,1)) from [dbo].[CounterData]
So trying to convert to datetime is the same as running this statement:SELECT CONVERT(DATETIME,'2018-08-12 17:49:35.123' + CHAR(0))
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
Ugh... yeah... not possible because CHAR(0) is an ASCII "NULL" Character. You've just got to wonder how data gets into such a condition.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 14, 2018 at 2:00 am
Jeff Moden - Monday, August 13, 2018 8:22 PMJonathan AC Roberts - Monday, August 13, 2018 3:34 AMJeff Moden - Sunday, August 12, 2018 3:53 PMJonathan AC Roberts - Sunday, August 12, 2018 2:57 AMJeff Moden - Saturday, August 11, 2018 7:37 PMJonathan AC Roberts - Saturday, August 11, 2018 5:41 PMI don't think anyone has noticed/seen my solution above. You just need to useLEFT(CounterDateTime, 23)
and it will work!
So Daniel can just use:delete from [dbo].[CounterData] WHERE LEFT(CounterDateTime, 23) < DATEADD(day, -10, GETDATE())
to get it to work.I'm a bit paranoid about relying on default formatting. It doesn't cost that much more to force a given format. The calculation on the column in the WHERE clause presents a possible performance problem, as well.
Again and still... you wouldn't need to do any of this if the column were of a correct temporal data type. Even if you couldn't change the column, a persisted/index computed column would likely make this sail.
I've not sure why Microsoft made it write to the database like this. But PerfMon will insert the data onto multiple sources: any database using an ODBC data source, there is also the choice of writing csv files, tab separated files and files with Perfmon's own binary format. So that might be a bit of the reason why the date is a bit odd. The format of the date is 'yyyy-MM-dd hh:mm:ss.000' which SQL server can implicitly convert to datetime.
But the thing that has stopped Daniel's query from working is the 24th character of this column (the string 'yyyy-MM-dd hh:mm:ss.000' is only 23 characters long).Another good reason to not store (except in an import staging table) dates and times as character based data. I wonder what the abhorrent 24th character actually is because a space in that position causes no problems.
SELECT CONVERT(DATETIME,'2018-08-12 17:49:35.123')
,CONVERT(DATETIME,'2018-08-12 17:49:35.123 ')
;From the following query I obtained its ASCII value to be zero.
select CounterDateTime,ASCII(SUBSTRING(CounterDateTime,24,1)) from [dbo].[CounterData]
So trying to convert to datetime is the same as running this statement:SELECT CONVERT(DATETIME,'2018-08-12 17:49:35.123' + CHAR(0))
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.Ugh... yeah... not possible because CHAR(0) is an ASCII "NULL" Character. You've just got to wonder how data gets into such a condition.
Hi everybody
I have used "Jonathan AC Roberts" code and it works fine :).
In terms of performance i don´t identify any problem yet. I am collecting about 100K ROWS in a day and will only have at maximum 1Milion of ROWS (150MB)
It´s ridiculous for Microsoft to create a date time field like a STRING. Date and Time columns should only be DATETIME type. Now in CHAR 24? this type of format is static, It´s useless.
Thanks Jonathan
Viewing 2 posts - 31 through 31 (of 31 total)
You must be logged in to reply to this topic. Login to reply