Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 comparing a column to Todays date Rate Topic Display Mode Topic Options
Author
 Message
 Posted Monday, September 09, 2013 11:01 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Monday, November 04, 2013 12:03 PM Points: 152, Visits: 474
 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 LogDATEfrom Xlog WHERE LogDATE >= Convert(datetime, Convert(int, GetDate()))
Post #1492874
 Posted Monday, September 09, 2013 11:44 AM
 Ten Centuries Group: General Forum Members Last Login: Today @ 1:46 PM Points: 1,105, Visits: 1,215
 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 LogDATEfrom 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()))
Post #1492888
 Posted Monday, September 09, 2013 12:02 PM
 SSCertifiable Group: General Forum Members Last Login: Today @ 1:08 PM Points: 7,935, Visits: 8,352
 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'S iomadh doigh a th’ air cu a mharbhadh gun a thachdadh le ìme
Post #1492893
 Posted Monday, September 09, 2013 1:33 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Monday, November 04, 2013 12:03 PM Points: 152, Visits: 474
 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 4Incorrect syntax near the keyword 'then'.
Post #1492906
 Posted Monday, September 09, 2013 2:13 PM
 SSChampion Group: General Forum Members Last Login: 2 days ago @ 2:23 PM Points: 10,854, Visits: 10,012
 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 4Incorrect 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 Moden's 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)
Post #1492917
 Posted Monday, September 09, 2013 2:35 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Monday, November 04, 2013 12:03 PM Points: 152, Visits: 474
 that worked Thanks,
Post #1492921
 Posted Monday, September 09, 2013 3:58 PM
 SSCertifiable Group: General Forum Members Last Login: Today @ 1:08 PM Points: 7,935, Visits: 8,352
 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'S iomadh doigh a th’ air cu a mharbhadh gun a thachdadh le ìme
Post #1492940
 Posted Wednesday, September 11, 2013 5:20 AM
 SSCommitted Group: General Forum Members Last Login: 2 days ago @ 8:00 PM Points: 1,725, Visits: 2,017
 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 4Incorrect 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/
Post #1493581

 Permissions