Showing posts with label ltgt. Show all posts
Showing posts with label ltgt. Show all posts

Thursday, March 22, 2012

Comparing date only?

This is probably a really simple one:
Given a table with a DATETIME column, how can I do an = or <> comparison
with just the date part?
ie.
SELECT * FROM MyTable WHERE SOMEFUNC(MyTable.theDateTime) <>
SOMEFUNC(@._In_theDataTime)
, where SOMEFUNC will just return the date part (ignoring the time part) of
theDateTime.
Thanks.....You don't want to apply expressions to both sides of the WHERE clause,
otherwise you will negate any indexes. How about (assuming your oddly named
variable is a DATETIME or, better yet, SMALLDATETIME):
WHERE MyTable.TheDateTIme >= (@._In_theDataTime)
AND MyTable.TheDateTIme < (@._In_theDataTime + 1)
"Robin Tucker" <idontwanttobespammedanymore@.reallyidont.com> wrote in
message news:dbo636$hgi$1$8302bc10@.news.demon.co.uk...
> This is probably a really simple one:
> Given a table with a DATETIME column, how can I do an = or <> comparison
> with just the date part?
> ie.
> SELECT * FROM MyTable WHERE SOMEFUNC(MyTable.theDateTime) <>
> SOMEFUNC(@._In_theDataTime)
> , where SOMEFUNC will just return the date part (ignoring the time part)
> of theDateTime.
>
> Thanks.....
>|||Okay, thanks (and yes, my stored proc names are odd, as I use:
@._In_somevar, @._Out_somevar, @._InOut_somevar). I find it easier to
distinguish between local vars and parameters just by prefixing parameters
with `_'. It's a personal thing (no guidelines here) - also, quite often I
write Data when I mean to write Date and visa versa - apologies as I'm
slightly dyslexic. Anyway:
WHERE MyTable.TheDateTime >= (@._In_theDateTime)
AND MyTable.TheDateTime < (@._In_theDateTime + 1)
This won't work though, because @._In_theDateTime might be less than
MyTable.TheDateTime but still on the same date (ie. the former is 3pm, the
latter 1pm). I was thinking I would be able to say "DATEPART(x) =
DATEPART(y)" or something. I don't have millions of rows (average around
20 - 40k rows in this table), so I don't mind too much about not using the
indexes (at least for now).
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:eyawDVfjFHA.3336@.tk2msftngp13.phx.gbl...
> You don't want to apply expressions to both sides of the WHERE clause,
> otherwise you will negate any indexes. How about (assuming your oddly
> named variable is a DATETIME or, better yet, SMALLDATETIME):
> WHERE MyTable.TheDateTIme >= (@._In_theDataTime)
> AND MyTable.TheDateTIme < (@._In_theDataTime + 1)
>
>
> "Robin Tucker" <idontwanttobespammedanymore@.reallyidont.com> wrote in
> message news:dbo636$hgi$1$8302bc10@.news.demon.co.uk...
>|||> This won't work though, because @._In_theDateTime might be less than
> MyTable.TheDateTime but still on the same date (ie. the former is 3pm, the
> latter 1pm).
So why not pass in JUST THE DATE?
Or, in the stored procedure, say:
SET @._in_theDateTime = 0 + DATEDIFF(DAY, 0, @._in_TheDateTime)
-- this will set the time portion to midnight without any messy
casts/conversions.|||See CONVERT function in SQL Server Books Online. It offers you various
styles for date formatting. One of them (112) gets rid of time portion.
--
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Robin Tucker" <idontwanttobespammedanymore@.reallyidont.com> wrote in
message news:dbo636$hgi$1$8302bc10@.news.demon.co.uk...
This is probably a really simple one:
Given a table with a DATETIME column, how can I do an = or <> comparison
with just the date part?
ie.
SELECT * FROM MyTable WHERE SOMEFUNC(MyTable.theDateTime) <>
SOMEFUNC(@._In_theDataTime)
, where SOMEFUNC will just return the date part (ignoring the time part) of
theDateTime.
Thanks.....|||Okay I get it now - yes this will work. Thanks.
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:%23vvTFhfjFHA.3608@.TK2MSFTNGP12.phx.gbl...
> So why not pass in JUST THE DATE?
> Or, in the stored procedure, say:
> SET @._in_theDateTime = 0 + DATEDIFF(DAY, 0, @._in_TheDateTime)
> -- this will set the time portion to midnight without any messy
> casts/conversions.
>sqlsql

Thursday, March 8, 2012

Compare Data in two differant tables

Here is my query
Select Prospect.XTeleLink as exp1, TeleScrp.XteleLink as exp2
from Prospect, XTeleLink
Where exp2 <> exp1 and TeleScrp.XResultCode = 9
What I want is to find XTeleLink numbers where the XResultCode = 9 in the
TeleScrp table, but there is NOT at record in my Prospect table that has
that same XTeleLink
I have some missing data, there should always be data in the prospect table,
and I am trying to find out what is missing!
Any help in adavnce, thanx!
DennisSomething like this?
SELECT A.XTeleLink
FROM TeleScrp
WHERE XResultCode = 9
AND NOT EXISTS (SELECT * FROM Prospect B WHERE A.XTeleLink = B.XTeleLink)
Keith
"Dennis Burgess" <dmburgess@.surdyke.remvove.com> wrote in message
news:uKTvcliCFHA.3976@.tk2msftngp13.phx.gbl...
> Here is my query
> Select Prospect.XTeleLink as exp1, TeleScrp.XteleLink as exp2
> from Prospect, XTeleLink
> Where exp2 <> exp1 and TeleScrp.XResultCode = 9
> What I want is to find XTeleLink numbers where the XResultCode = 9 in the
> TeleScrp table, but there is NOT at record in my Prospect table that has
> that same XTeleLink
> I have some missing data, there should always be data in the prospect
table,
> and I am trying to find out what is missing!
> Any help in adavnce, thanx!
> Dennis
>|||Try,
select
a.xtelelink
from
telescrp as a
left join
prospect as b
on a.xtelelink = b.xtelelink
where
b.xtelelink is null
and a.xresultcode = 9
-- or
select
a.xtelelink
from
telescrp as a
where
a.xresultcode = 9
and not exists (select * from prospect as b where a.xtelelink = b.xtelelink)
AMB
"Dennis Burgess" wrote:

> Here is my query
> Select Prospect.XTeleLink as exp1, TeleScrp.XteleLink as exp2
> from Prospect, XTeleLink
> Where exp2 <> exp1 and TeleScrp.XResultCode = 9
> What I want is to find XTeleLink numbers where the XResultCode = 9 in the
> TeleScrp table, but there is NOT at record in my Prospect table that has
> that same XTeleLink
> I have some missing data, there should always be data in the prospect tabl
e,
> and I am trying to find out what is missing!
> Any help in adavnce, thanx!
> Dennis
>
>|||Untested due to lack of DDL...
SELECT P.XTeleLink, TS.XTeleLink
FROM XTeleLink TS
LEFT OUTER JOIN Prospect P ON TS.XTeleLink = P.XTeleLink
WHERE TS.XResultCode = 9
AND P.XTeleLink IS NULL
Paul
"Dennis Burgess" wrote:

> Here is my query
> Select Prospect.XTeleLink as exp1, TeleScrp.XteleLink as exp2
> from Prospect, XTeleLink
> Where exp2 <> exp1 and TeleScrp.XResultCode = 9
> What I want is to find XTeleLink numbers where the XResultCode = 9 in the
> TeleScrp table, but there is NOT at record in my Prospect table that has
> that same XTeleLink
> I have some missing data, there should always be data in the prospect tabl
e,
> and I am trying to find out what is missing!
> Any help in adavnce, thanx!
> Dennis
>
>|||Excellent, Now,
How do you also look for a field from the second Prospect Table?
Dennis
"Keith Kratochvil" <sqlguy.back2u@.comcast.net> wrote in message
news:OM$LUtiCFHA.328@.tk2msftngp13.phx.gbl...
> Something like this?
> SELECT A.XTeleLink
> FROM TeleScrp
> WHERE XResultCode = 9
> AND NOT EXISTS (SELECT * FROM Prospect B WHERE A.XTeleLink = B.XTeleLink)
> --
> Keith
>
> "Dennis Burgess" <dmburgess@.surdyke.remvove.com> wrote in message
> news:uKTvcliCFHA.3976@.tk2msftngp13.phx.gbl...
> table,
>|||Something like this?
SELECT A.XTeleLink
FROM TeleScrp
WHERE XResultCode = 9
AND NOT EXISTS (SELECT * FROM Prospect B WHERE A.XTeleLink = B.XTeleLink
/*here is the new part*/
AND A.SomeColumn = B.SomeColumn)
Keith
"Dennis Burgess" <dmburgess@.surdyke.remvove.com> wrote in message
news:uAX5d%23iCFHA.2512@.TK2MSFTNGP15.phx.gbl...
> Excellent, Now,
> How do you also look for a field from the second Prospect Table?
> Dennis
>
> "Keith Kratochvil" <sqlguy.back2u@.comcast.net> wrote in message
> news:OM$LUtiCFHA.328@.tk2msftngp13.phx.gbl...
B.XTeleLink)
the
has
>