Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Problem

Status
Not open for further replies.

msalvador

Programmer
Nov 13, 2001
33
0
0
IT
Hi i've a table with a field datetime type, i've to extract from this only the time for a comparison

F.E.

'01/02/2002 11:00:15 PM'

the result must be 11:00:15 PM
 
You could use DatePart()


DATEPART
Returns an integer representing the specified datepart of the specified date.

Syntax
DATEPART ( datepart , date )

Arguments
datepart

Is the parameter that specifies the part of the date to return. The table lists dateparts and abbreviations recognized by Microsoft® SQL Server™.

Datepart Abbreviations
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw
hour hh
minute mi, n
second ss, s
millisecond ms


Hope this helps....
 
You could also do something like this:


if (select convert (char(2),getdate(),108)) > 12
select convert (char(9),getdate(),108) + 'PM'
else
select convert (char(9),getdate(),108) + 'AM'

Rick.
 
I recommend using a 24 hour time for comparisons rather than a 12 hour time with AM or PM. The 24 hour time can be easily derived using the convert function and style 108.

Select convert(varchar(11),getdate(),108)

If you need the AM/PM form of the time you can use a query like the following.

Select
Right(Convert(Varchar(19), getdate(), 20), 8) +
Case
When datepart(hh, getdate())>11
Then ' PM' Else ' AM' End

NOTE: In Each case the date becomes a character data type so comparisons will only work if all times are converted. Terry L. Broadbent
Programming and Computing Resources
 
SELECT CONVERT(varchar(10),YourDateField,8) + ' ' + RIGHT(CONVERT(varchar(19),YourDateField),2) JHall
 
thank's my solution is

AND convert(varchar,TR_Basket_Header1.TS_End_Time,108) >= convert(varchar,@CurPerTS_Start_Date,108)
AND convert(varchar,TR_Basket_Header1.TS_End_Time,108) <= convert(varchar,@CurPerTS_End_Date,108)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top