Click here to Skip to main content
15,902,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hai ALL I have Query to get the date and time like this

mm/dd/yyyy hh:mm:ss AM

example

12/23/2011 05:07:45 PM

The Query is like this
RTRIM(CONVERT(CHAR(20),device_last_contact,101)) + &''; +LTRIM(STUFF(right(CONVERT(varchar(30),
device_last_contact, 9), 14), 6, 7, '')) as [Last Contact]



Could anyone change this query using sql format like these numbers 109,101

I have a query which gives the answer as
select convert(varchar, starttime, 009) as [Last Contact] from Table Name

Dec 4 2011 12:53:40 PM

But I need as
12/4/2011 12:53:40 PM


Please Help through SQL.
Posted

This link[^] provides all the various options.
 
Share this answer
 
Comments
sacraj 4-Apr-11 11:37am    
Thank U for U
Abhinav S 4-Apr-11 12:14pm    
You are welcome.
Hi,

Although there is no single format code which gives the output as you required, but you may try the following combination to get your date time format:-

SQL
declare @starttime as datetime

set @starttime = '12/4/2011 12:53:40 PM'

select convert(varchar, @starttime, 101) +' '+ convert(varchar, @starttime, 108)+ ' '+ right(convert(varchar, @starttime, 109),2)



-- Output
12/04/2011 12:53:40 PM
 
Share this answer
 
Comments
sacraj 4-Apr-11 11:37am    
It is nice of U
Is this good for you?

SQL
select  getdate(),
        convert(varchar(10),getdate(), 101)                         -- MM/DD/YYYY
        +
        ' '
        +
        LEFT(RIGHT( convert(varchar(100),getdate(), 109), 14),8)    -- HH:MM:SS
        +
        RIGHT(convert(varchar(100),getdate(), 100), 2)              -- AM/PM
 
Share this answer
 
v2
Comments
sacraj 4-Apr-11 11:36am    
Thank you for the help that U have done It was very useful for .

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