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
Solved

convert function of SQL Server 2000 giving wrong results.

Posted on 2009-04-01
5
452 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
  • 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 500 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 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