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


Selection using date held as a string


Selection using date held as a string

Author
Message
Chris Quinn-821458
Chris Quinn-821458
Old Hand
Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)

Group: General Forum Members
Points: 326 Visits: 844
I think I have discovered something that is possibly significant!

As stated previously, the data is a set of key/value pairs, with a datatype (D = Date, N= Number, S = String, B = Bit)

If the table contains only data of type D, my query works, but if there is data of any other type in the table, I get the conversion error message, even though my WHERE clause explicitly excludes everything except those where the datatype = 'D'. This suggests that it is evaluating the date comparison criteria before the datatype = 'D' clause, and the conversion is failing on non-date datatypes.

I therefore put the WHERE datatype = 'D' selection into a derived view of the table, assuming this would be evaluated before the date comparisons later in the query, but this is appears not to be the case unless I put in a kludge to force the matter

This fails:

SELECT DERIVED.*

FROM (select parameter_value PV , ,convert(datetime2,PV.parameter_value) as key_date WHERE PV.datatype = 'D') DERIVED

WHERE DERIVED.key_date BETWEEN GETDATE() and DATEADD(d, 10, GETDATE())



But this works!

SELECT DERIVED.*

FROM (select TOP 9999999999 parameter_value PV , ,convert(datetime2,PV.parameter_value) as key_date WHERE PV.datatype = 'D') DERIVED

WHERE DERIVED.key_date BETWEEN GETDATE() and DATEADD(d, 10, GETDATE())


Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39730 Visits: 38563
Chris Quinn-821458 (4/4/2013)
I think I have discovered something that is possibly significant!

As stated previously, the data is a set of key/value pairs, with a datatype (D = Date, N= Number, S = String, B = Bit)

If the table contains only data of type D, my query works, but if there is data of any other type in the table, I get the conversion error message, even though my WHERE clause explicitly excludes everything except those where the datatype = 'D'. This suggests that it is evaluating the date comparison criteria before the datatype = 'D' clause, and the conversion is failing on non-date datatypes.

I therefore put the WHERE datatype = 'D' selection into a derived view of the table, assuming this would be evaluated before the date comparisons later in the query, but this is appears not to be the case unless I put in a kludge to force the matter

This fails:

SELECT DERIVED.*

FROM (select parameter_value PV , ,convert(datetime2,PV.parameter_value) as key_date WHERE PV.datatype = 'D') DERIVED

WHERE DERIVED.key_date BETWEEN GETDATE() and DATEADD(d, 10, GETDATE())



But this works!

SELECT DERIVED.*

FROM (select TOP 9999999999 parameter_value PV , ,convert(datetime2,PV.parameter_value) as key_date WHERE PV.datatype = 'D') DERIVED

WHERE DERIVED.key_date BETWEEN GETDATE() and DATEADD(d, 10, GETDATE())



I don't have the same data as you, but I don't have any problems with the following:



create table dbo.parameter_value (
datatype char(1),
key_value varchar(64)
);

insert into dbo.parameter_value(datatype, key_value)
values
('D','2013-10-29 22:59:00Z'),
('B','0'),
('N','101'),
('N','102'),
('B','1'),
('D','2013-04-25 23:59:00Z'),
('N','105'),
('N','106'),
('D','2013-03-06 22:59:00Z'),
('D','2013-03-06 22:59:00Z'),
('N','103'),
('B','0'),
('B','1'),
('N','104'),
('N','107'),
('N','110'),
('D','2013-03-06 22:59:00Z'),
('B','0'),
('B','0'),
('D','2013-03-29 22:59:00Z'),
('D','2013-03-06 22:59:00Z'),
('B','1'),
('B','0'),
('B','1'),
('D','2013-04-27 23:59:00Z'),
('B','1'),
('D','2013-03-06 22:59:00Z'),
('D','2013-04-08 23:59:00Z'),
('N','108'),
('N','109');

go

SELECT
key_value
,convert(datetime2,PV.key_value) as DT2
,convert(datetime,CONVERT(DATETIME2, PV.key_value)) AS DT
FROM
parameter_value PV
WHERE
PV.datatype = 'D';

go

SELECT
key_value
,convert(datetime2,PV.key_value) as DT2
,convert(datetime,CONVERT(DATETIME2, PV.key_value)) AS DT
FROM
parameter_value PV
WHERE
PV.datatype = 'D'
and convert(datetime,CONVERT(DATETIME2, PV.key_value)) BETWEEN GETDATE() and DATEADD(d, 10, GETDATE())

go

drop table dbo.parameter_value;
go




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
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