Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have a column as GDate in a table in sql server and inserted date in that by getdate()function and fetching hours,minutes,sec of date as
select datepart(d,GDate),datepart(hour,GDate),
datepart(minute,GDate),datepart(second,GDate) from tablename

Now I want to check that date was in AM or PM .
Is there any function with datepart() to check only AM or PM.

Please Let me know .


Thanking You
Mohammad. wasif
Posted
Updated 6-Apr-11 20:07pm
v2

This link [^]should help you out.
 
Share this answer
 
check the following link it has the solution,

http://blog.sqlauthority.com/2008/08/14/sql-server-get-date-time-in-any-format-udf-user-defined-functions

Thanks,
www.codecollege.net | www.InterviewsGuru.com | www.chitraguptan.com | www.thekumbakonam.com
 
Share this answer
 
v2
Hi try this,

SQL
SELECT SUBSTRING(CONVERT(varchar(20), yourdate, 22), 18, 3)  AS Expr1
FROM      table
ORDER BY expr1 ;



SUBSTRING(CONVERT(varchar(20), yourdate, 22), 18, 3)-->it gives you a am/pm.
o/p:
----
am
pm
am
pm
 
Share this answer
 
Comments
Mahendra.p25 7-Apr-11 3:53am    
Great Work
See CAST and CONVERT[^]

SQL
select RIGHT( convert(varchar(30), GDate, 100 ), 2 ) from tablename
 
Share this answer
 
One way to do this is:
SQL
select case
          when datepart( hour, getdate()) < 12 then 'It''s AM'
          else 'It''s PM'
       end
 
Share this answer
 
try this Link

Link [^]

SQL
SELECT
GETDATE() AS CurrentDate,
RIGHT(CONVERT(VARCHAR, GETDATE(), 100),7) AS CurrentTime,
CONVERT(VARCHAR(10), GETDATE(), 101) + '' + RIGHT(CONVERT(VARCHAR, GETDATE(), 100),7) AS CurrentDateTime

Another Way--------------------------------------------------------------------
declare @result varchar(10)
declare @Format int
select @Format= datepart(hh,convert(varchar,CreatedOn,108))  from CandidateDetails where CandidateID = 5
if(@Format<=12)
begin
set @result='AM'
select @result as Result
end
else
begin
set @result='PM'
select @result as Result
end
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900