SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sql Query Regarding Date & Time Format
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Sathish.H
Starting Member

India
2 Posts

Posted - 06/18/2012 :  02:25:48  Show Profile  Reply with Quote
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)

Singapore
16745 Posts

Posted - 06/18/2012 :  02:38:43  Show Profile  Reply with Quote
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
Time is always against us

Go to Top of Page

Sathish.H
Starting Member

India
2 Posts

Posted - 06/18/2012 :  05:53:15  Show Profile  Reply with Quote
thank u sir :-)

Sathish.H
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 06/18/2012 :  12:19:19  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000