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


comparing a column to Todays date


comparing a column to Todays date

Author
Message
SQLTestUser
SQLTestUser
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1926 Visits: 565
how do we compare a column name "LogDATE" to todays date and if its exists we print out a statement saying the record exists.

Ideally we would check the max row and check that the max row has todays date and if it does we print out a statement as " record exist no further action"

thanks

I tried this but it will give me 9/9 and 9/8 i just want 9/9 or just todays date

SELECT LogDATE
from Xlog
WHERE LogDATE >= Convert(datetime, Convert(int, GetDate()))
batgirl
batgirl
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3243 Visits: 1820
SQLTestUser (9/9/2013)
how do we compare a column name "LogDATE" to todays date and if its exists we print out a statement saying the record exists.

Ideally we would check the max row and check that the max row has todays date and if it does we print out a statement as " record exist no further action"

thanks

I tried this but it will give me 9/9 and 9/8 i just want 9/9 or just todays date

SELECT LogDATE
from Xlog
WHERE LogDATE >= Convert(datetime, Convert(int, GetDate()))



Convert(datetime, Convert(int, GetDate())) computes to tomorrow's date -

Try this:

Convert(datetime, DATEDIFF(day, 0, GETDATE()))
Tom Thomson
Tom Thomson
SSC Guru
SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)

Group: General Forum Members
Points: 51052 Visits: 13160
The conversion to INT may be part of the problem. But Convert(datetime, Convert(int, GetDate())) should deliver midnight tonight, not midnight this morning, so it's surprising you are getting any rows at all if tomorrow's row doesn't exist.

Your description of what you want to do would require something like
IF EXISTS(
select 1 from Xlog where LogDATE >= cast(getDate() as date)
)
then PRINT 'Record already exists.' ;



Or you can select the LogDATE using slightly different code from what you have tried:
select top 1 LogDATE from Xlog
where LogDATE >= cast(getDate() as date)
order by LogDATE desc ;


If there are any rows for today or later that will deliver you just one LogDATE (the latest in the table), even if you have 10 rows in the table with LogDate today or later. Change desc to asc to get the earliest LogDATE in the table that is today or later (if there is any such row).

Tom

SQLTestUser
SQLTestUser
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1926 Visits: 565
Convert(datetime, DATEDIFF(day, 0, GETDATE())) worked

IF EXISTS(
select 1 from Xlog where LogDATE >= cast(getDate() as date) -- should this be datetime if i have Sql server 2005?
)
then PRINT 'Record already exists.' ;

results in an error as
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'then'.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)

Group: General Forum Members
Points: 149939 Visits: 18581
SQLTestUser (9/9/2013)
Convert(datetime, DATEDIFF(day, 0, GETDATE())) worked

IF EXISTS(
select 1 from Xlog where LogDATE >= cast(getDate() as date) -- should this be datetime if i have Sql server 2005?
)
then PRINT 'Record already exists.' ;

results in an error as
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'then'.


Drop "THEN". That word is not valid in sql syntax.


IF EXISTS(
select 1 from Xlog where LogDATE >= cast(getDate() as date) -- should this be datetime if i have Sql server 2005?
)
PRINT 'Record already exists.' ;



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
SQLTestUser
SQLTestUser
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1926 Visits: 565
that worked

Thanks,
Tom Thomson
Tom Thomson
SSC Guru
SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)

Group: General Forum Members
Points: 51052 Visits: 13160
SQLTestUser (9/9/2013)
-- should this be datetime if i have Sql server 2005?

No, if you are on 2005 you need something more complicated - as batgirl suggested,
convert(datetime, datediff(0, DD, getdate())) will work in SQL Server 2005 instead of cast(getdate() as date).
But if you were on 2005, why would you ask in the 2008 forum?

Sorry about that intrusive THEN that shouldn't have been there; had been working in a different language for a couple of hours, and somehow that then slipped from there into my sql.

Tom

kapil_kk
kapil_kk
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10697 Visits: 2778
SQLTestUser (9/9/2013)
Convert(datetime, DATEDIFF(day, 0, GETDATE())) worked

IF EXISTS(
select 1 from Xlog where LogDATE >= cast(getDate() as date) -- should this be datetime if i have Sql server 2005?
)
then PRINT 'Record already exists.' ;

results in an error as
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'then'.

Remove 'then' before PRINT, it will work

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
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