Solved

Syntax error converting datetime from character string

Posted on 2008-06-16
7
203 Views
Last Modified: 2010-03-20
I am getting the error message with the following code:

Case when 'firstinvoicedate' = dateadd(month,-14,getdate()) then 'new work' else 'rolling work' end as 'Work Status'.



0
Comment
Question by:benissitt
  • 2
  • 2
7 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 21794582
If you put single quotes around 'firstinvoicedate', then you are comparing the string 'firstinvoicedate', and not the column firstinvoicedate.
Is that what you are trying to do?

else

Case when firstinvoicedate = dateadd(month,-14,getdate()) then 'new work' else 'rolling work' end as 'Work Status'.
0
 

Author Comment

by:benissitt
ID: 21794796
I'll show you the entire query to clear things up for you
SELECT yourquery.*,FirstInvoiceDate FROM

	(select 

jobheader.jobno,

costing.dbo.revenue.docketdate,

companyname,

jobdesc,

jobheader.status,

costing.dbo.revenue.value,

orderheader.completedate,

salesperson,

Case	when 'firstinvoicedate' = dateadd(month,14,getdate()) then 'old work' else 'mew work' end as ' '

from jobheader

inner join orderheader on jobheader.orderno = orderheader.orderno

inner join customers.dbo.company on orderheader.customer = customers.dbo.company.coid

inner join costing.dbo.revenue on jobheader.jobno = costing.dbo.revenue.jobno

INNER join customers.dbo.lookupdesignation on customers.dbo.company.designation = customers.dbo.lookupdesignation.ldcode

where costing.dbo.revenue.docketdate is not null ) yourquery

INNER JOIN

	(SELECT CompanyName,Min(docketdate) AS FirstInvoiceDate

	FROM

		(select 

jobheader.jobno,

costing.dbo.revenue.docketdate,

companyname,

jobdesc,

jobheader.status,

costing.dbo.revenue.value,

orderheader.completedate,

salesperson,

Case when firstinvoicedate = dateadd(month,-14,getdate()) then 'new work' else 'rolling work' end as 'Work Status'

from jobheader

inner join orderheader on jobheader.orderno = orderheader.orderno

inner join customers.dbo.company on orderheader.customer = customers.dbo.company.coid

inner join costing.dbo.revenue on jobheader.jobno = costing.dbo.revenue.jobno

INNER join customers.dbo.lookupdesignation on customers.dbo.company.designation = customers.dbo.lookupdesignation.ldcode

where costing.dbo.revenue.docketdate is not null ) yourquery

	GROUP BY CompanyName) latest

ON

yourquery.CompanyName=latest.CompanyName

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 21794832
>I'll show you the entire query to clear things up for you
My comment is already pretty clear.

>SELECT CompanyName,Min(docketdate) AS FirstInvoiceDate
Here you have an expression aliased as FirstInvoice Date, with the table aliased as latest, so your expression should be...

Case when latest.firstinvoicedate = dateadd(month,-14,getdate()) then 'new work' else 'rolling work' end as 'Work Status'
0
 

Author Comment

by:benissitt
ID: 21794883
I tried that before but I get the following error messages:

Server: Msg 107, Level 16, State 2, Line 1
The column prefix 'latest' does not match with a table name or alias name used in the query.
Server: Msg 107, Level 16, State 1, Line 1
The column prefix 'latest' does not match with a table name or alias name used in the query.
0
 
LVL 51

Assisted Solution

by:Mark Wills
Mark Wills earned 250 total points
ID: 21796082
No, you cannot access that way.

It is a subquery that you are joining to the "latest" subquery (which is almost identical the the other subquery). Seems fairly convaluted... Each time I go to read it, I get a headache :)

What happens when you make the first one just like the second one ? assume that firstinvoicedate is a column somewhere - although it is not prefixed with a table or alias like the other columns...



0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sql Query Lookup based on range 2 41
MS SQL export CSV & schedule It 9 53
SQL Server 2008 R2 - Sums/Grouping 7 51
SQL Server Update Query Streamline 4 14
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now