September 9, 2013 at 11:01 am
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()))
September 9, 2013 at 11:44 am
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()))
September 9, 2013 at 12:02 pm
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
September 9, 2013 at 1:33 pm
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'.
September 9, 2013 at 2:13 pm
SQLTestUser (9/9/2013)
Convert(datetime, DATEDIFF(day, 0, GETDATE())) workedIF 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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
September 9, 2013 at 2:35 pm
that worked
Thanks,
September 9, 2013 at 3:58 pm
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
September 11, 2013 at 5:20 am
SQLTestUser (9/9/2013)
Convert(datetime, DATEDIFF(day, 0, GETDATE())) workedIF 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/
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply