Solved

convert function of SQL Server 2000 giving wrong results.

Posted on 2009-04-01
5
449 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

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

9 Experts available now in Live!

Get 1:1 Help Now