473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Datetime comparison problem

Hi every one,
I need to compare to datetime values , done in
stored procedure.The input datetime parameter is in
dd/mm/yyyy hh:mm:ss AM/PM format ,the datetime values to be compared
are also stored with the same format.
does datetime works fine for AM/PM format also? because some test
conditions are failing , which i thought correct..Help me out!
Thanks in advance..
Nov 1 '08 #1
5 4711
Sreenivas (th******************@gmail.com) writes:
I need to compare to datetime values , done in
stored procedure.The input datetime parameter is in
dd/mm/yyyy hh:mm:ss AM/PM format ,the datetime values to be compared
are also stored with the same format.
does datetime works fine for AM/PM format also? because some test
conditions are failing , which i thought correct..Help me out!
datetime is a binary data type and does not have a format, least of all
one with AM/PM. If you have data as strings in your application, you
should use parameterised statements, so that the strings are converted
in the client with respect to regional settings.

Show us the code your having problem with, and we should be able to tell
what you are doing wrong.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Nov 1 '08 #2
On Nov 1, 6:34 pm, Erland Sommarskog <esq...@sommarskog.sewrote:
Sreenivas (thatiparthysreeni...@gmail.com) writes:
I need to compare to datetime values , done in
stored procedure.The input datetime parameter is in
dd/mm/yyyy hh:mm:ss AM/PM format ,the datetime values to be compared
are also stored with the same format.
does datetime works fine for AM/PM format also? because some test
conditions are failing , which i thought correct..Help me out!

datetime is a binary data type and does not have a format, least of all
one with AM/PM. If you have data as strings in your application, you
should use parameterised statements, so that the strings are converted
in the client with respect to regional settings.

Show us the code your having problem with, and we should be able to tell
what you are doing wrong.

--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se

Links for SQL Server Books Online:
SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000:http://www.microsoft.com/sql/prodinf...ons/books.mspx
======================================
from client ,I have used mm/dd/yyyy hh:mm:ss AM/PM format,same format
is sent to stored procedure,

create procedure sp_checkAvailability(@ip_fromTime
datetime,@ip_toTime datetime)
as
begin
if exists(
select * from conferenceHall c where
(@ip_fromTime< c.fromTime AND @ip_toTime <=c.fromTime)
AND
(@ip_fromTime>=c.toTime AND @ip_toTime>c.toTime )
)
print ' Ok'
else
print 'NotOk'
question is , is this comparison works for all types AM/PM format of
@ip_toTime ,@ip_fromTime values
(@ip_fromTime>=c.toTime AND @ip_toTime>c.toTime )
because c.fromTime is stored in binary format,
but i dont know how sql server handles @ip_fromTime datetime
values ,since it is input parameter , not yet stored right!

thanks,
Srinivas Reddy Thatiparthy,
Nov 2 '08 #3
Sreenivas (th******************@gmail.com) writes:
from client ,I have used mm/dd/yyyy hh:mm:ss AM/PM format,same format
is sent to stored procedure,
But how did you call the stored procedure? If you let the API convert
the string, you should be alright, provided that this date format
agree with your regional settings. (Then again, in the headers of your
post, I see an IP address that I can locate to Karanataka in India,
why I would expect your regional settings be set to dd/mm/yyyy.)

If you sent an EXEC string (which you shouldn't), conversion happens on
the SQL Server side. The AM/PM part is likely to be understood no matter
the settings, but xx/yy/zzzz could be taken as MM/DD/YYYY or DD/MM/YYYY
depending on the language and date format settings in SQL Server.
create procedure sp_checkAvailability(@ip_fromTime
datetime,@ip_toTime datetime)
as
begin
if exists(
select * from conferenceHall c where
(@ip_fromTime< c.fromTime AND @ip_toTime <=c.fromTime)
AND
(@ip_fromTime>=c.toTime AND @ip_toTime>c.toTime )
)
print ' Ok'
else
print 'NotOk'
question is , is this comparison works for all types AM/PM format of
@ip_toTime ,@ip_fromTime values
(@ip_fromTime>=c.toTime AND @ip_toTime>c.toTime )
because c.fromTime is stored in binary format,
but i dont know how sql server handles @ip_fromTime datetime
values ,since it is input parameter , not yet stored right!
I don't know what your code intends to achieve, but it looks funny. Assuming
that @ip_fromTime <= @ip_toTime and conference.fromTime <
conferenceHall.toTime, this will always print 'OK'. Given these
assumptions, it's logically impossible that for the same row that
@ip_fromTime can at the same be less than c.fromTime and greater than
toTime.

If the purpose is to see whether there is any rows that do not overlap
with the interval in the parameters, try:

IF NOT EXISTS (SELECT * FROM conferenceHall c
WHERE @ip_fromTime <= c.toTime
AND @ip_toTime >= c.fromTime)
PRINT 'Hall available'
ELSE
PRINT 'Hall booked'

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Nov 2 '08 #4
On Nov 2, 3:56*pm, Erland Sommarskog <esq...@sommarskog.sewrote:
Sreenivas (thatiparthysreeni...@gmail.com) writes:
from client ,I have used *mm/dd/yyyy hh:mm:ss AM/PM format,same format
is sent to stored procedure,

But how did you call the stored procedure? If you let the API convert
the string, you should be alright, provided that this date format
agree with your regional settings. (Then again, in the headers of your
post, I see an IP address that I can locate to Karanataka in India,
why I would expect your regional settings be set to dd/mm/yyyy.)

If you sent an EXEC string (which you shouldn't), conversion happens on
the SQL Server side. The AM/PM part is likely to be understood no matter
the settings, but xx/yy/zzzz could be taken as MM/DD/YYYY or DD/MM/YYYY
depending on the language and date format settings in SQL Server.
create procedure sp_checkAvailability(@ip_fromTime
datetime,@ip_toTime *datetime)
as
begin
if exists(
select * from conferenceHall c *where
(@ip_fromTime< c.fromTime AND @ip_toTime <=c.fromTime)
AND
(@ip_fromTime>=c.toTime AND @ip_toTime>c.toTime )
*)
print ' Ok'
else
print 'NotOk'
question is , is this comparison works for all types AM/PM format of
@ip_toTime ,@ip_fromTime *values
(@ip_fromTime>=c.toTime AND @ip_toTime>c.toTime )
because c.fromTime is stored in binary format,
but i dont know how sql server handles @ip_fromTime datetime
values ,since it is input parameter , not yet stored right!

I don't know what your code intends to achieve, but it looks funny. Assuming
that @ip_fromTime <= @ip_toTime and conference.fromTime <
conferenceHall.toTime, this will always print 'OK'. Given these
assumptions, it's logically impossible that for the same row that
@ip_fromTime can at the same be less than c.fromTime and greater than
toTime.

If the purpose is to see whether there is any rows that do not overlap
with the interval in the parameters, try:

* IF NOT EXISTS (SELECT * FROM conferenceHall c
* * * * * * * * *WHERE *@ip_fromTime <= c.toTime
* * * * * * * * * *AND *@ip_toTime >= c.fromTime)
* * *PRINT 'Hall available'
* ELSE
* * *PRINT 'Hall booked'

--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se

Links for SQL Server Books Online:
SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000:http://www.microsoft.com/sql/prodinf...ons/books.mspx
Sorry ! i mistyped it ..
select * from conferenceHall c where
(@ip_fromTime< c.fromTime AND @ip_toTime <=c.fromTime)
It's not AND ..it's OR
OR
(@ip_fromTime>=c.toTime AND @ip_toTime>c.toTime )
)
Nov 3 '08 #5
On Nov 2, 3:56*pm, Erland Sommarskog <esq...@sommarskog.sewrote:
Sreenivas (thatiparthysreeni...@gmail.com) writes:
from client ,I have used *mm/dd/yyyy hh:mm:ss AM/PM format,same format
is sent to stored procedure,

But how did you call the stored procedure? If you let the API convert
the string, you should be alright, provided that this date format
agree with your regional settings. (Then again, in the headers of your
post, I see an IP address that I can locate to Karanataka in India,
why I would expect your regional settings be set to dd/mm/yyyy.)

If you sent an EXEC string (which you shouldn't), conversion happens on
the SQL Server side. The AM/PM part is likely to be understood no matter
the settings, but xx/yy/zzzz could be taken as MM/DD/YYYY or DD/MM/YYYY
depending on the language and date format settings in SQL Server.
create procedure sp_checkAvailability(@ip_fromTime
datetime,@ip_toTime *datetime)
as
begin
if exists(
select * from conferenceHall c *where
(@ip_fromTime< c.fromTime AND @ip_toTime <=c.fromTime)
AND
(@ip_fromTime>=c.toTime AND @ip_toTime>c.toTime )
*)
print ' Ok'
else
print 'NotOk'
question is , is this comparison works for all types AM/PM format of
@ip_toTime ,@ip_fromTime *values
(@ip_fromTime>=c.toTime AND @ip_toTime>c.toTime )
because c.fromTime is stored in binary format,
but i dont know how sql server handles @ip_fromTime datetime
values ,since it is input parameter , not yet stored right!

I don't know what your code intends to achieve, but it looks funny. Assuming
that @ip_fromTime <= @ip_toTime and conference.fromTime <
conferenceHall.toTime, this will always print 'OK'. Given these
assumptions, it's logically impossible that for the same row that
@ip_fromTime can at the same be less than c.fromTime and greater than
toTime.

If the purpose is to see whether there is any rows that do not overlap
with the interval in the parameters, try:

* IF NOT EXISTS (SELECT * FROM conferenceHall c
* * * * * * * * *WHERE *@ip_fromTime <= c.toTime
* * * * * * * * * *AND *@ip_toTime >= c.fromTime)
* * *PRINT 'Hall available'
* ELSE
* * *PRINT 'Hall booked'

--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se

Links for SQL Server Books Online:
SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000:http://www.microsoft.com/sql/prodinf...ons/books.mspx
Thanks..
Nov 3 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Donnal Walter | last post by:
I was very surprised to discover that >>> import datetime >>> x = datetime.date(2004, 9, 14) >>> y = datetime.datetime(2004, 9, 14, 6, 43, 15) >>> print x == y True How can these two...
1
by: colinhumber | last post by:
I have a datetime variable coming from my ASP.NET application that has a time portion. I give my users the option to perform an equals, greater than, less than, or between comparison. The trouble...
4
by: Viktor Jevdokimov | last post by:
Hello, I've got an XML file saved from DataSet. One table has datetime column (datatype - xs:dateTime). XML file fragment for example: <Root>...
4
by: elziko | last post by:
Hi I have two date times that I belive to be equal. However, when I do a comparison I find that date1 is less than date2... Console.WriteLine(date1.ToString("dd/MM/yyyy HH:mm:fffffff") + " - " +...
2
by: rhaazy | last post by:
I need to know how I can format a string in C# to get the current date/ time, so that I can do a comparison against a date time column in MS SQL Server 2005. The date/time column in the database...
1
by: jimmybaba | last post by:
hi all i am newly graduate and started my first job.......i am geting problem to convert datetime into date only , i am trying to get values from sqlserver table with comparison of date. i am using...
5
by: Mike | last post by:
Hi, I use MS SQL Express and VS 2005 c#, win application. I would like to select value rom DateTimePicker and list all values for selected date within GridView. I have method as follows: ...
3
by: Anil Gupte | last post by:
I am using the followig code: SqlConnection con = new SqlConnection(strcon); con.Open(); string strSelect = "Select PaidUntil from Users where username='" + un + "'"; SqlCommand cmd = new...
5
by: indika | last post by:
Hi, I'm a newbie to python but have some experience in programming. I came across this requirement of using datetime.date objects associated with some another object. eg. a dictionary containing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.