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

x
?
Solved

convert function of SQL Server 2000 giving wrong results.

Posted on 2009-04-01
5
Medium Priority
?
458 Views
Last Modified: 2012-05-06
Hi,

I am using SQL Server 2000 and I have a table named BTH_INFO with BTH_DT datetime field and in this field I am having data with both date and time together, but when I am executing the following query

"select * from BTH_INFO where convert(nvarchar,BTH_DT,101) >= '03/30/2009' and convert(nvarchar,BTH_DT,101) <= '03/31/2009' order by ltrim(rtrim(BTH_ID))"
I am getting rows with BTH_DT 03/31/2008, 03/30/2009 and 03/31/2009.

And when I am executing the following query:

select * from BTH_INFO where convert(nvarchar,BTH_DT,101) >= '03/30/09' and convert(nvarchar,BTH_DT,101) <= '03/31/09' order by ltrim(rtrim(BTH_ID))
I am getting rows with BTH_DT 03/30/2009 only.

Please give me a solution to the above problem so that I will get the correct result.

Regards,
Pranjal
0
Comment
Question by:pranjal_ds
[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
  • 3
  • 2
5 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 24039395
Hi,

That's because converttype 101 gives you the year with 4 digits.
So if BYH_DT is '03/31/2008' your WHERE looks like this:

select * from BTH_INFO where  '03/31/2008' >= '03/30/09' and  '03/31/2008' <= '03/31/09' order by ltrim(rtrim(BTH_ID))

If you only wants 2 digits in the year use converttype 1
convert(nvarchar,BTH_DT,1)

/peter
0
 

Author Comment

by:pranjal_ds
ID: 24041320
Hi,

Actually the database was in SQL Server 2000 and now I am accessing it in Microsoft SQL Server 2005 Management Studio Express. Will that cause such errors?
I used convert(nvarchar,BTH_DT,1) still it is showing me  rows with BTH_DT 03/31/08, 03/30/09 and 03/31/09, when actually I should get rows of 03/31/09 & 03/30/09 only.

Please advice and give solution.

-Pranjal
0
 
LVL 22

Expert Comment

by:pivar
ID: 24042367
No, that shouldn't matter.

I'm sorry of course it does, it's string comparison and the string '03/31/08' is bigger than '03/30/09'.

Try this, or can change the dateformat on your datecriteria?
set dateformat mdy

select *
  from BTH_INFO
    where BTH_DT BETWEEN convert(datetime,'03/30/09') and convert(datetime,'03/31/09')
order by ltrim(rtrim(BTH_ID))

If you want to keep the string comparison the date must be in YMD format.

By the way if you have an index on BHT_DT it will not be used if you have convert in the where clause.
0
 
LVL 22

Accepted Solution

by:
pivar earned 1500 total points
ID: 24042432
My last post got somewhat incomprehensible.

First. It doesn't matter if you are using 2005 or 2000.

Then the line
"Try this, or can change the dateformat on your datecriteria?"

What I meant was,
Try this select. But I think it's better if it's possible for you to change the datecriteria to standard dateformat '20090330'
0
 

Author Closing Comment

by:pranjal_ds
ID: 31565306
Thank you for guiding me. I was able to get the correct result by including the end time 23:59:00 in To date in the select query.

Regards,
Pranjal
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 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