Solved

convert function of SQL Server 2000 giving wrong results.

Posted on 2009-04-01
5
450 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
This is an introductory video for CloudBerry Managed Backup. You will learn how to sign up with the service and get started in a few minutes.

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

21 Experts available now in Live!

Get 1:1 Help Now