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
 General SQL Server Forums
 New to SQL Server Programming
 Sql Query Regarding Date & Time Format

Author  Topic 

Sathish.H
Starting Member

2 Posts

Posted - 2012-06-18 : 02:25:48
The Query
"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


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'."


Sathish.H

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-06-18 : 02:38:43
because you have converted the date to string. And you are doing a string comparision in your where clause.
You should leave it as datetime data type. Also specify your date string in YYYY-MM-DD format


Select *
from
(
SELECT [InTime] as Date FROM CarEnteries
Union SELECT [OutTime] as Date FROM CarEnteries
)a
WHERE [Date] BETWEEN '2012-05-08' and '2012-07-01'
order by [Date] asc



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Sathish.H
Starting Member

2 Posts

Posted - 2012-06-18 : 05:53:15
thank u sir :-)

Sathish.H
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-18 : 12:19:19
and keep in mind that you will still be able to apply format functions in your front end application and get date values in format you want. you dont need to alter datatypes at sql end for that

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -