Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

comparing a column to Todays date Expand / Collapse
Author
Message
Posted Monday, September 9, 2013 11:01 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 2:56 PM
Points: 160, Visits: 502
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()))

Post #1492874
Posted Monday, September 9, 2013 11:44 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, September 26, 2014 10:18 AM
Points: 1,407, Visits: 1,645
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()))
Post #1492888
Posted Monday, September 9, 2013 12:02 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:15 AM
Points: 7,742, Visits: 9,491
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
Post #1492893
Posted Monday, September 9, 2013 1:33 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 2:56 PM
Points: 160, Visits: 502
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'.
Post #1492906
Posted Monday, September 9, 2013 2:13 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:14 PM
Points: 13,007, Visits: 12,425
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 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 9, 2013 2:35 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 2:56 PM
Points: 160, Visits: 502

that worked

Thanks,

Post #1492921
Posted Monday, September 9, 2013 3:58 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:15 AM
Points: 7,742, Visits: 9,491
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
Post #1492940
Posted Wednesday, September 11, 2013 5:20 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 12:09 AM
Points: 1,916, Visits: 2,337
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/
Post #1493581
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse