Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The Query
SQL
"Select * from ( SELECT (convert(varchar, [InTime], 103))as Date FROM CarEnteries
Union SELECT (convert(varchar, [OutTime], 103)) as Date FROM CarEnteries)
a order by (convert(DateTime, [Date], 103)) asc"


Is working absolutly fine, when i am taking union of two date feilds in a single table but...
when i need range of date from the result i am getting error plxz help...
query is as follow


SQL
Select * from ( SELECT (convert(varchar, [InTime], 103))as Date FROM CarEnteries
Union SELECT (convert(varchar, [OutTime], 103)) as Date FROM CarEnteries)
Where (convert(DateTime, [Date], 103)) BETWEEN '08-05-2012' and '01-07-2012'
a order by (convert(DateTime, [Date], 103)) asc



Getting error message as
"Incorrect syntax near the keyword 'Where'."


[edit]Code blocks sorted out - OriginalGriff[/edit]
Posted
Updated 17-Jun-12 20:08pm
v2

Take the "a" out:
SQL
Select * from ( SELECT (convert(varchar, [InTime], 103))as Date FROM CarEnteries
Union SELECT (convert(varchar, [OutTime], 103)) as Date FROM CarEnteries)
Where (convert(DateTime, [Date], 103)) BETWEEN '08-05-2012' and '01-07-2012'
a order by (convert(DateTime, [Date], 103)) asc

becomes
SQL
Select * from ( SELECT (convert(varchar, [InTime], 103))as Date FROM CarEnteries
Union SELECT (convert(varchar, [OutTime], 103)) as Date FROM CarEnteries)
Where (convert(DateTime, [Date], 103)) BETWEEN '08-05-2012' and '01-07-2012'
order by (convert(DateTime, [Date], 103)) asc



BTW: never, ever store dates as strings. Store them as DateTime instead, then you don't have to faff with converting them every time you need to do any math. It also makes life a lot easier, because you move the internationalization to as close to input and output as possible - so the data in your DB is in a completely independent format.
 
Share this answer
 
There is a syntax error with your query.
There is an extra barcket at the end of carEnteries.

However, note that your query will still not work (after removing this bracket).

When you do a UNION, the columns returned in all the constituents of the UNION queries should return the same set of columns.
 
Share this answer
 
if ur [date] field datatype is datetime then use this query it will work

SQL
Select * from ( SELECT (convert(varchar, [InTime], 103))as Date FROM CarEnteries
Union SELECT (convert(varchar, [OutTime], 103)) as Date FROM CarEnteries)
Where [Date] BETWEEN '08-05-2012' and '01-07-2012'
order by [Date] asc
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900