Solved

Date Conversion Error

Posted on 2012-03-28
7
275 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
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

911 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

22 Experts available now in Live!

Get 1:1 Help Now