Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Date Conversion Error

Posted on 2012-03-28
7
Medium Priority
?
290 Views
Last Modified: 2012-03-28
Hi

When i Execute the Below Query its id Displaying the  2012 year dates also

SELECT  CONVERT(VARCHAR(8), s.CREATED_TIME,3) as day from  Table1 s
             WHERE       CONVERT(VARCHAR(8), s.created_time, 3)
 between CONVERT(VARCHAR(8), '13/09/11', 3)  and  CONVERT(VARCHAR(8), '20/09/11', 3) order by created_time
 
Created_time datatype id datetime
in the Database its is storing as the  these format:2011-09-13 12:11:00.000
2011-09-14 14:22:50.950
2011-09-14 14:36:14.267


Out Put
13/09/11
13/09/11
14/09/11
14/09/11
14/09/11
14/09/11
14/09/11
14/09/11
16/09/11
16/09/11
16/09/11
16/09/11
16/09/11
16/09/11
16/09/11
16/09/11
16/09/11
20/09/11
20/09/11
20/09/11
20/09/11
20/09/11
20/09/11
20/09/11
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
17/01/12
18/01/12
18/01/12
18/01/12
18/01/12
18/01/12
18/01/12
18/01/12
18/01/12
18/01/12
18/01/12
18/01/12
18/01/12
19/01/12
19/01/12
19/01/12
19/01/12
19/01/12
19/01/12
19/01/12
19/01/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12
17/02/12


Please suggest !!

Thanks
Raj
0
Comment
Question by:nrajasekhar7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 37775857
You're converting to a string and comparing.

Try this:

SELECT  CONVERT(VARCHAR(8), s.CREATED_TIME,3) as day from  Table1 s
             WHERE       created_time
 between cast(CONVERT(VARCHAR(8), '13/09/11', 3)  as datetime) and  cast(CONVERT(VARCHAR(8), '20/09/11', 3) as datetime) order by created_time

I'm assuming created_time is a datetime field.
0
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 2000 total points
ID: 37775859
Or...

SELECT  CONVERT(VARCHAR(8), s.CREATED_TIME,3) as day from  Table1 s
             WHERE       created_time
 between cast('13 September 2011' as datetime) and  cast('20 September 2011 as datetime) order by created_time
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 37775860
To avoid incompatibilities, I always use 'yyyyMMdd' format. You can try this:
WHERE       CONVERT(VARCHAR(8), s.created_time, 112)
 between '20110913' and '20110920'

You don't even need to convert your field to 112. SQL does an implicit conversion.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37775868
SELECT  CONVERT(VARCHAR(8), s.CREATED_TIME,3) as day from  Table1 s
WHERE  created_time between CONVERT(datetime, '13/09/11', 103) and  CONVERT(datetime, '20/09/11', 103) order by created_time

Open in new window

0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 37775874
SQL does do implicit conversions, but explicit conversions are better in my opinion as at least you control the conversion and you don't rely on something to do it for you. We all know how much a nightmare date handling is because of the differing formats regardless of how good SQL's date handling system is.
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 37775892
That's why I use 112. DateField='20120301' is the same thing as DateField=CONVERT(datetime,'20120301'). And you have the exact same control over it without the explicit conversion and with it.
As a general rule, you should always use explicit conversions. But in this particular case, there is no difference, unless your date field isn't a date either.
And the yyyyMMdd format is the only one I found that is, so far, fully universal.
0
 

Author Closing Comment

by:nrajasekhar7
ID: 37780003
Thanks You very Much  ,It works
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Ready to get certified? Check out some courses that help you prepare for third-party exams.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

636 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