Solved

Date Conversion Error

Posted on 2012-03-28
7
287 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 500 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

738 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