ORDNO is string which include the date, like 'XX021224XXXXXXX'.
I use SUBSTRING() to get the date part from this string.
Main Topics
Browse All TopicsThe following sql works on my machine useing for depelopment, but doesn't on the other environment.
and couses the datetime error.
Is there any setting for datetime in SQLserver?
Or is there any mistake in the following sql.
Any little advice is appriciated.
select MEDCD from ORDSYOHODET_TEST
where PATID = '31499601'
and substring(ORDNO,3,6) <> '000000'
and convert(datetime, substring(ORDNO,3,6)) = '2002/12/22'
group by MEDCD
union
select d.MEDCD MEDCD
from ORDSYOHODET_TEST d
where d.PATID = '31499601' and substring(d.ORDNO,3,6)<> '000000'
and d.NUMDAYS > DATEDIFF( Day, convert(datetime, substring(d.ORDNO,3,6)), '2002/12/22')
group by MEDCD
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Do you have any date in ORDNO less than 12/12/1752
I dont think so..
but only in such conditions, you will get this kind of error
'The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.'
Coz' datediff will return an integer which is out of the range for that integer datatype
I am not sure how valid is this website.. but it is talking abt the same issue that you are asking.. he has given a solution too.. validate the solution before you try out the things.
http://www.ielearning.com/
You can also refer this site..
http://www.sqlmag.com/Arti
in which you can set the format for the datetime values.
Thank you for the information.
I'm sorry to ask you again.
Because of my poor English skill,
but I don't get the exact meaning of what the page says.
The first page you listed is saying that
the error occurs when the database server is using
a non-US date format, right?
Though, our system is using a US date format(yy/mm/dd)...
Hi mini1111
1. It is more than likely that the error is caused by different date settings in the SQL database. Try to use convert(datetime,'2002/12/
2. The convert(datetime, substring(d.ORDNO,3,6)) is probably causing problems too. You are not passing the third parameter to the convert function, wich specifies the format of the datestring. In your case this should look like convert(datetime, substring(d.ORDNO,3,6),12)
Look at the Transact-SQL help on the CONVERT function.
A word of advice too.
Avoid using functions on fields like this: substring(ORDNO,3,6). This will make the index on the field useless and on large tables queries like this will run very slow.
Best regards,
bragn.
Thank you for all the information.
I think I could figure out the solution.
There existed a difference between two environment.
The one had been added the index on the PATID field.
Because of this index, the optimization occured and this
made the sql to cause the error.
I don't think this is the exact solution,
but at least the index did bad things of some kind.
So if the problem is resolved, please do not leave it open like your other one at http://www.experts-exchang
Anthony
Business Accounts
Answer for Membership
by: acperkinsPosted on 2002-12-23 at 20:30:02ID: 7627453
What is the value of ORDNO?
Take a look at SET DATEFORMAT format
Anthony