Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Retrieve current date

Author  Topic 

jamie_pattison
Yak Posting Veteran

65 Posts

Posted - 2008-08-14 : 09:53:43
I have some SQL code

CONVERT(VARCHAR(10), ReportDate,105)

.....

DATEDIFF (day, ReportDate,DD-MM-YYYY)


Im trying to find the difference between ReportDate and todays date (DD-MM-YYYY) but no joy.

How may this be accomplished?

Thank you

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-14 : 09:57:57
SELECT DATEDIFF(day,ReportDate,GETDATE()) from table

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jamie_pattison
Yak Posting Veteran

65 Posts

Posted - 2008-08-14 : 10:05:41
I receive a syntax error - Why do you have From Table?

The date should be todays date and not pulled from any table?

Thanks
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-08-14 : 10:07:27
where are you getting ReportDate from if not a table?

Em
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-14 : 10:10:26
quote:
Originally posted by jamie_pattison

I receive a syntax error - Why do you have From Table?

The date should be todays date and not pulled from any table?

Thanks


What is the error?
Is ReportDate a column of a table?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jamie_pattison
Yak Posting Veteran

65 Posts

Posted - 2008-08-14 : 10:11:54
ReportDate is a field in a "table". So im trying to figure out what the difference is between the date from ReportDate and todays date.....

Error is Incorrect syntax near DateDiff.
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-08-14 : 10:13:31
then what's wrong with the query? you do know that "From Table" means from YOUR table (whatever it's called)?

Em
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-14 : 10:15:40
quote:
Originally posted by jamie_pattison

ReportDate is a field in a "table". So im trying to figure out what the difference is between the date from ReportDate and todays date.....

Error is Incorrect syntax near DateDiff.


Can you post the full code you used?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jamie_pattison
Yak Posting Veteran

65 Posts

Posted - 2008-08-14 : 10:23:23
I gathered it means from "my table" but i thought i could just type in the dateDiff function to get the results and use From later on in the query.

My code is

SELECT

CONVERT(VARCHAR(10), TBL.[ReportDate],105) As ReportDate,

CONVERT(VARCHAR(10), TBL.[User],105) As UserDate,

DATEDIFF (day, TBL.[ReportDate], GETDATE())

FROM
Database.Internal.Procedures

First guess is that its having problems converting the date 105 (formatted in dd-mm-yyyy) to GETDATE which i think is mm-dd-yyyy 00:00:00.000

Thanks
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-08-14 : 10:26:42
why the TBL prefix? how is reportdate stored in the database? is it a datetime?

Em
Go to Top of Page

jamie_pattison
Yak Posting Veteran

65 Posts

Posted - 2008-08-14 : 10:46:50
TBL was a typo and yes its a datetime.....
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-14 : 10:51:18
Do you get any errors?

SELECT
[ReportDate] As ReportDate,
[User] As UserDate,
DATEDIFF (day, [ReportDate], GETDATE())
FROM
Database.Internal.Procedures


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -