h1

SQL SERVER – Get Time in AM/PM Format

12/22/2008

Today i come with one situation where I need to get Time in AM/PM format.

I tried with different way and come up with the simple solution, that i am going to share with all of you.


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

SQL DateTime

Syntax to Get SQL DateTime With Am/Pm Format

SQL Date time in AM/PM Format

Get SQL Date time in AM/PM Format

I have read very interesting article on SQL Authority.Com . You can find the many ways to get date in different formats.

About these ads

10 comments

  1. Note that if you format dates using sql, all your DATEs become VARCHARs which can’t be used to make use of date related functions until re-converted back to DATETIME

    If you use front end application, you should do formation there

    Madhivanan


  2. That worked fine. Thanks!


  3. GUD


  4. Really its very useful thank you so much


  5. Its Good.


  6. Hey Thanks….

    Worked well for me….Got me what i needed …

    God Bless


  7. hi,
    when i inserted a new rows, i cant get the date time format like 08/08/201212:49PM.
    if i wrote this query in sql (SELECT CONVERT(VARCHAR(10), GETDATE(), 101) + RIGHT(CONVERT(VARCHAR, GETDATE(), 100),7) got output like 08/08/201212:49PM..
    but i inserted means
    (insert into j(createtime)values(CONVERT(VARCHAR(10), GETDATE(), 101) + RIGHT(CONVERT(VARCHAR, GETDATE(), 100),7) )
    ) the output is Conversion failed when converting date and/or time from character string.

    Please anyone can help me


  8. Hi Meena,

    I have found that, it is working with SQL 2005 and SQL 2008. Please find my scripts:

    DECLARE @test TABLE(T DATETIME)

    INSERT INTO @test(t)
    VALUES (CONVERT(VARCHAR(10), GETDATE(), 101) + RIGHT(CONVERT(VARCHAR, GETDATE(), 100),7))

    SELECT * FROM @test

    Can you please check what is the datatype of column “CreateTime” on your table “j”?

    Thanks,
    Tejas
    SQLYoga.com


  9. It worked .. Thank you..


  10. Thanks, just what I needed.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: