SQL select statement is finding something that should not exist in a view
up vote
0
down vote
favorite
I have this sql statement for making a view
create view RecordYearsTwo
as
select
Record.RecordID, RecordValue.Value
from
Record
join
RecordValue on Record.RecordID = RecordValue.RecordID
where
len(RecordValue.Value) = 4
and RecordValue.Value like '[16-20][0-9][0-9][0-9]%'
and RecordValue.Value like '%[16-20][0-9][0-9][0-9]'
and RecordValue.Value != '26 Mar 1850';
When I then run
select *
from Record
join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
where cast(RecordYearsTwo.Value as int) >= 1800
I get this error
Conversion failed when converting the nvarchar value '26 Mar 1850' to data type int.
My understanding is that '26 Mar 1850' shouldn't even exist in my view because the length of everything in my view should be 4 and I specifically said should not equal '26 Mar 1850'
Any ideas?
sql sql-server
add a comment |
up vote
0
down vote
favorite
I have this sql statement for making a view
create view RecordYearsTwo
as
select
Record.RecordID, RecordValue.Value
from
Record
join
RecordValue on Record.RecordID = RecordValue.RecordID
where
len(RecordValue.Value) = 4
and RecordValue.Value like '[16-20][0-9][0-9][0-9]%'
and RecordValue.Value like '%[16-20][0-9][0-9][0-9]'
and RecordValue.Value != '26 Mar 1850';
When I then run
select *
from Record
join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
where cast(RecordYearsTwo.Value as int) >= 1800
I get this error
Conversion failed when converting the nvarchar value '26 Mar 1850' to data type int.
My understanding is that '26 Mar 1850' shouldn't even exist in my view because the length of everything in my view should be 4 and I specifically said should not equal '26 Mar 1850'
Any ideas?
sql sql-server
Are you sure you dropped the view, then re-created it? Side note: It would be better to be storing the date in a SARGable format, preferrably ISO:YYYY-MM-DD
(so,'1850-03-26
, assuming you can't store it as a date object). This would prevent you from even needing to do the conversion, among other things.
– Clockwork-Muse
Nov 21 at 18:45
1
SQL Server could decide to merge the where clauses together.
– Salman A
Nov 21 at 20:29
1
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. In this case, the question is lacking the table definitions. For example, ifRecordValue.Value
is of typeint
, that might cause this error. Please provide a Minimal, Complete, and Verifiable example.
– Richardissimo
Nov 21 at 20:44
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have this sql statement for making a view
create view RecordYearsTwo
as
select
Record.RecordID, RecordValue.Value
from
Record
join
RecordValue on Record.RecordID = RecordValue.RecordID
where
len(RecordValue.Value) = 4
and RecordValue.Value like '[16-20][0-9][0-9][0-9]%'
and RecordValue.Value like '%[16-20][0-9][0-9][0-9]'
and RecordValue.Value != '26 Mar 1850';
When I then run
select *
from Record
join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
where cast(RecordYearsTwo.Value as int) >= 1800
I get this error
Conversion failed when converting the nvarchar value '26 Mar 1850' to data type int.
My understanding is that '26 Mar 1850' shouldn't even exist in my view because the length of everything in my view should be 4 and I specifically said should not equal '26 Mar 1850'
Any ideas?
sql sql-server
I have this sql statement for making a view
create view RecordYearsTwo
as
select
Record.RecordID, RecordValue.Value
from
Record
join
RecordValue on Record.RecordID = RecordValue.RecordID
where
len(RecordValue.Value) = 4
and RecordValue.Value like '[16-20][0-9][0-9][0-9]%'
and RecordValue.Value like '%[16-20][0-9][0-9][0-9]'
and RecordValue.Value != '26 Mar 1850';
When I then run
select *
from Record
join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
where cast(RecordYearsTwo.Value as int) >= 1800
I get this error
Conversion failed when converting the nvarchar value '26 Mar 1850' to data type int.
My understanding is that '26 Mar 1850' shouldn't even exist in my view because the length of everything in my view should be 4 and I specifically said should not equal '26 Mar 1850'
Any ideas?
sql sql-server
sql sql-server
edited Nov 22 at 0:28
Eric Brandt
2,0901521
2,0901521
asked Nov 21 at 18:41
Jared Smith
312
312
Are you sure you dropped the view, then re-created it? Side note: It would be better to be storing the date in a SARGable format, preferrably ISO:YYYY-MM-DD
(so,'1850-03-26
, assuming you can't store it as a date object). This would prevent you from even needing to do the conversion, among other things.
– Clockwork-Muse
Nov 21 at 18:45
1
SQL Server could decide to merge the where clauses together.
– Salman A
Nov 21 at 20:29
1
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. In this case, the question is lacking the table definitions. For example, ifRecordValue.Value
is of typeint
, that might cause this error. Please provide a Minimal, Complete, and Verifiable example.
– Richardissimo
Nov 21 at 20:44
add a comment |
Are you sure you dropped the view, then re-created it? Side note: It would be better to be storing the date in a SARGable format, preferrably ISO:YYYY-MM-DD
(so,'1850-03-26
, assuming you can't store it as a date object). This would prevent you from even needing to do the conversion, among other things.
– Clockwork-Muse
Nov 21 at 18:45
1
SQL Server could decide to merge the where clauses together.
– Salman A
Nov 21 at 20:29
1
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. In this case, the question is lacking the table definitions. For example, ifRecordValue.Value
is of typeint
, that might cause this error. Please provide a Minimal, Complete, and Verifiable example.
– Richardissimo
Nov 21 at 20:44
Are you sure you dropped the view, then re-created it? Side note: It would be better to be storing the date in a SARGable format, preferrably ISO:
YYYY-MM-DD
(so, '1850-03-26
, assuming you can't store it as a date object). This would prevent you from even needing to do the conversion, among other things.– Clockwork-Muse
Nov 21 at 18:45
Are you sure you dropped the view, then re-created it? Side note: It would be better to be storing the date in a SARGable format, preferrably ISO:
YYYY-MM-DD
(so, '1850-03-26
, assuming you can't store it as a date object). This would prevent you from even needing to do the conversion, among other things.– Clockwork-Muse
Nov 21 at 18:45
1
1
SQL Server could decide to merge the where clauses together.
– Salman A
Nov 21 at 20:29
SQL Server could decide to merge the where clauses together.
– Salman A
Nov 21 at 20:29
1
1
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. In this case, the question is lacking the table definitions. For example, if
RecordValue.Value
is of type int
, that might cause this error. Please provide a Minimal, Complete, and Verifiable example.– Richardissimo
Nov 21 at 20:44
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. In this case, the question is lacking the table definitions. For example, if
RecordValue.Value
is of type int
, that might cause this error. Please provide a Minimal, Complete, and Verifiable example.– Richardissimo
Nov 21 at 20:44
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
The criteria for that datestamp isn't needed.
Because even the first criteria wouldn't accept it (to long).
And those LIKE criteria don't need the % if only 4 characters are expected.
create view RecordYearsTwo as
select rec.RecordID, val.Value
from Record rec
join RecordValue val on val.RecordID = rec.RecordID
where len(val.Value) = 4
and (val.Value like '1[6-9][0-9][0-9]' or val.Value like '20[0-9][0-9]')
And to avoid the error you could use TRY_CAST instead.
select *
from Record
join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
where try_cast(RecordYearsTwo.Value as int) >= 1800
add a comment |
up vote
0
down vote
With multiple expressions in the where clause, you can't guarantee what order they get executed in. Try:
create view RecordYearsTwo as
select r.RecordID, v.Value
from Record r
join (
select *
from RecordValue
where len(Value) = 4
) v on r.RecordID = v.RecordID
where v.Value like '[16-20][0-9][0-9][0-9]%'
and v.Value like '%[16-20][0-9][0-9][0-9]'
add a comment |
up vote
0
down vote
First, your view conditions do not make sense. I think you want:
create view RecordYearsTwo as
select r.RecordID, rv.Value
from Record r join
RecordValue rv
on r.RecordID = rv.RecordID
where len(rv.Value) = 4
try_convert(int, rv.Value) >= 1600 and
try_convert(int, rv.Value) < 2100;
Your logic doesn't make sense. I find this amusing: 'v.Value like '[16-20][0-9][0-9][0-9]%'
. That like
pattern says to get any '1'
, any character between '6'
and '2'
(which is none), and any '0'
. I understand what you mean, but SQL Server does not.
Then, the view does not get executed first. You have no idea what the order of execution is, so for your query, you want try_convert()
again:
select *
from Record join r
RecordYearsTwo ry2
on r.RecordID = ry2.RecordID
where try_convert(int, ry2.Value) >= 1800
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
The criteria for that datestamp isn't needed.
Because even the first criteria wouldn't accept it (to long).
And those LIKE criteria don't need the % if only 4 characters are expected.
create view RecordYearsTwo as
select rec.RecordID, val.Value
from Record rec
join RecordValue val on val.RecordID = rec.RecordID
where len(val.Value) = 4
and (val.Value like '1[6-9][0-9][0-9]' or val.Value like '20[0-9][0-9]')
And to avoid the error you could use TRY_CAST instead.
select *
from Record
join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
where try_cast(RecordYearsTwo.Value as int) >= 1800
add a comment |
up vote
1
down vote
The criteria for that datestamp isn't needed.
Because even the first criteria wouldn't accept it (to long).
And those LIKE criteria don't need the % if only 4 characters are expected.
create view RecordYearsTwo as
select rec.RecordID, val.Value
from Record rec
join RecordValue val on val.RecordID = rec.RecordID
where len(val.Value) = 4
and (val.Value like '1[6-9][0-9][0-9]' or val.Value like '20[0-9][0-9]')
And to avoid the error you could use TRY_CAST instead.
select *
from Record
join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
where try_cast(RecordYearsTwo.Value as int) >= 1800
add a comment |
up vote
1
down vote
up vote
1
down vote
The criteria for that datestamp isn't needed.
Because even the first criteria wouldn't accept it (to long).
And those LIKE criteria don't need the % if only 4 characters are expected.
create view RecordYearsTwo as
select rec.RecordID, val.Value
from Record rec
join RecordValue val on val.RecordID = rec.RecordID
where len(val.Value) = 4
and (val.Value like '1[6-9][0-9][0-9]' or val.Value like '20[0-9][0-9]')
And to avoid the error you could use TRY_CAST instead.
select *
from Record
join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
where try_cast(RecordYearsTwo.Value as int) >= 1800
The criteria for that datestamp isn't needed.
Because even the first criteria wouldn't accept it (to long).
And those LIKE criteria don't need the % if only 4 characters are expected.
create view RecordYearsTwo as
select rec.RecordID, val.Value
from Record rec
join RecordValue val on val.RecordID = rec.RecordID
where len(val.Value) = 4
and (val.Value like '1[6-9][0-9][0-9]' or val.Value like '20[0-9][0-9]')
And to avoid the error you could use TRY_CAST instead.
select *
from Record
join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
where try_cast(RecordYearsTwo.Value as int) >= 1800
edited Nov 21 at 21:02
answered Nov 21 at 20:47
LukStorms
11.1k31532
11.1k31532
add a comment |
add a comment |
up vote
0
down vote
With multiple expressions in the where clause, you can't guarantee what order they get executed in. Try:
create view RecordYearsTwo as
select r.RecordID, v.Value
from Record r
join (
select *
from RecordValue
where len(Value) = 4
) v on r.RecordID = v.RecordID
where v.Value like '[16-20][0-9][0-9][0-9]%'
and v.Value like '%[16-20][0-9][0-9][0-9]'
add a comment |
up vote
0
down vote
With multiple expressions in the where clause, you can't guarantee what order they get executed in. Try:
create view RecordYearsTwo as
select r.RecordID, v.Value
from Record r
join (
select *
from RecordValue
where len(Value) = 4
) v on r.RecordID = v.RecordID
where v.Value like '[16-20][0-9][0-9][0-9]%'
and v.Value like '%[16-20][0-9][0-9][0-9]'
add a comment |
up vote
0
down vote
up vote
0
down vote
With multiple expressions in the where clause, you can't guarantee what order they get executed in. Try:
create view RecordYearsTwo as
select r.RecordID, v.Value
from Record r
join (
select *
from RecordValue
where len(Value) = 4
) v on r.RecordID = v.RecordID
where v.Value like '[16-20][0-9][0-9][0-9]%'
and v.Value like '%[16-20][0-9][0-9][0-9]'
With multiple expressions in the where clause, you can't guarantee what order they get executed in. Try:
create view RecordYearsTwo as
select r.RecordID, v.Value
from Record r
join (
select *
from RecordValue
where len(Value) = 4
) v on r.RecordID = v.RecordID
where v.Value like '[16-20][0-9][0-9][0-9]%'
and v.Value like '%[16-20][0-9][0-9][0-9]'
answered Nov 21 at 18:47
RedFilter
133k30241253
133k30241253
add a comment |
add a comment |
up vote
0
down vote
First, your view conditions do not make sense. I think you want:
create view RecordYearsTwo as
select r.RecordID, rv.Value
from Record r join
RecordValue rv
on r.RecordID = rv.RecordID
where len(rv.Value) = 4
try_convert(int, rv.Value) >= 1600 and
try_convert(int, rv.Value) < 2100;
Your logic doesn't make sense. I find this amusing: 'v.Value like '[16-20][0-9][0-9][0-9]%'
. That like
pattern says to get any '1'
, any character between '6'
and '2'
(which is none), and any '0'
. I understand what you mean, but SQL Server does not.
Then, the view does not get executed first. You have no idea what the order of execution is, so for your query, you want try_convert()
again:
select *
from Record join r
RecordYearsTwo ry2
on r.RecordID = ry2.RecordID
where try_convert(int, ry2.Value) >= 1800
add a comment |
up vote
0
down vote
First, your view conditions do not make sense. I think you want:
create view RecordYearsTwo as
select r.RecordID, rv.Value
from Record r join
RecordValue rv
on r.RecordID = rv.RecordID
where len(rv.Value) = 4
try_convert(int, rv.Value) >= 1600 and
try_convert(int, rv.Value) < 2100;
Your logic doesn't make sense. I find this amusing: 'v.Value like '[16-20][0-9][0-9][0-9]%'
. That like
pattern says to get any '1'
, any character between '6'
and '2'
(which is none), and any '0'
. I understand what you mean, but SQL Server does not.
Then, the view does not get executed first. You have no idea what the order of execution is, so for your query, you want try_convert()
again:
select *
from Record join r
RecordYearsTwo ry2
on r.RecordID = ry2.RecordID
where try_convert(int, ry2.Value) >= 1800
add a comment |
up vote
0
down vote
up vote
0
down vote
First, your view conditions do not make sense. I think you want:
create view RecordYearsTwo as
select r.RecordID, rv.Value
from Record r join
RecordValue rv
on r.RecordID = rv.RecordID
where len(rv.Value) = 4
try_convert(int, rv.Value) >= 1600 and
try_convert(int, rv.Value) < 2100;
Your logic doesn't make sense. I find this amusing: 'v.Value like '[16-20][0-9][0-9][0-9]%'
. That like
pattern says to get any '1'
, any character between '6'
and '2'
(which is none), and any '0'
. I understand what you mean, but SQL Server does not.
Then, the view does not get executed first. You have no idea what the order of execution is, so for your query, you want try_convert()
again:
select *
from Record join r
RecordYearsTwo ry2
on r.RecordID = ry2.RecordID
where try_convert(int, ry2.Value) >= 1800
First, your view conditions do not make sense. I think you want:
create view RecordYearsTwo as
select r.RecordID, rv.Value
from Record r join
RecordValue rv
on r.RecordID = rv.RecordID
where len(rv.Value) = 4
try_convert(int, rv.Value) >= 1600 and
try_convert(int, rv.Value) < 2100;
Your logic doesn't make sense. I find this amusing: 'v.Value like '[16-20][0-9][0-9][0-9]%'
. That like
pattern says to get any '1'
, any character between '6'
and '2'
(which is none), and any '0'
. I understand what you mean, but SQL Server does not.
Then, the view does not get executed first. You have no idea what the order of execution is, so for your query, you want try_convert()
again:
select *
from Record join r
RecordYearsTwo ry2
on r.RecordID = ry2.RecordID
where try_convert(int, ry2.Value) >= 1800
answered Nov 21 at 22:54
Gordon Linoff
751k34286394
751k34286394
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418620%2fsql-select-statement-is-finding-something-that-should-not-exist-in-a-view%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you sure you dropped the view, then re-created it? Side note: It would be better to be storing the date in a SARGable format, preferrably ISO:
YYYY-MM-DD
(so,'1850-03-26
, assuming you can't store it as a date object). This would prevent you from even needing to do the conversion, among other things.– Clockwork-Muse
Nov 21 at 18:45
1
SQL Server could decide to merge the where clauses together.
– Salman A
Nov 21 at 20:29
1
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. In this case, the question is lacking the table definitions. For example, if
RecordValue.Value
is of typeint
, that might cause this error. Please provide a Minimal, Complete, and Verifiable example.– Richardissimo
Nov 21 at 20:44