Solved

Date Conversion Error

Posted on 2012-03-28
7
269 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
7 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks You very Much  ,It works
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now