MS SQL Datetime Format

Hi I need format getdate() to dd/mm/yyyy hh:mm:ss AMPM

How can I do this?
GRChandrashekarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee SavidgeCommented:
Does this work for you?

select convert(nvarchar(100), getdate(), 131)
0
Lee SavidgeCommented:
Do you need the day/month elements zero padded? Do you need the spaces as per your request? Do you specifically need to remove the milleseconds?
0
Lee SavidgeCommented:
Actually ignore my first comment entirely, of course it doesn't work. One minute.
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Lee SavidgeCommented:
This works, but it feels dirty.

It gets your date format into exactly the right format for you according to your question

select right('0' + cast(datepart(d, getdate()) as nvarchar(2)), 2) + '/' +
       right('0' + cast(datepart(m, getdate()) as nvarchar(2)), 2) + '/' +
       cast(year(getdate()) as nvarchar(4)) + ' ' +
       convert(nvarchar(8), getdate(), 114) + ' ' +
       right(convert(nvarchar(30), getdate(), 109), 2)

Open in new window

0
GRChandrashekarAuthor Commented:
@lsavidge

This is fine. but it includes milliseconds also  which I dont want
0
Lee SavidgeCommented:
The last one doesn't. It just includes seconds.
0
GRChandrashekarAuthor Commented:
But it is 24 hour clock though it shows AMPM !
0
GRChandrashekarAuthor Commented:
First one is absolutely fine except for milliseconds
0
Lee SavidgeCommented:
The first one gives the Hijri formatted date which isn't correct. For example todays date shows as:

 9/11/1434  1:24:20:200PM

The year is definitely 1434 in the gregorian calendar.

I'll sort the script a little more.
0
GRChandrashekarAuthor Commented:
ID: 39490018
oh ya didnt notice that
0
Lee SavidgeCommented:
select right('0' + cast(datepart(d, getdate()) as nvarchar(2)), 2) + '/' +
       right('0' + cast(datepart(m, getdate()) as nvarchar(2)), 2) + '/' +
       cast(year(getdate()) as nvarchar(4)) + ' ' +
       convert(nvarchar(7), cast(getdate() as time), 100)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GRChandrashekarAuthor Commented:
Sorry. Closed it. Seconds is missing in this !
0
Lee SavidgeCommented:
With seconds!!

select right('0' + cast(datepart(d, getdate()) as nvarchar(2)), 2) + '/' +
       right('0' + cast(datepart(m, getdate()) as nvarchar(2)), 2) + '/' +
       cast(year(getdate()) as nvarchar(4)) + ' ' +
       right('0' + ltrim(stuff(right(convert(char(26), current_timestamp, 109), 14),9, 4, ' ')),11)
0
Lee SavidgeCommented:
I need a wash!
0
GRChandrashekarAuthor Commented:
Thank You
0
Lee SavidgeCommented:
We got there :)

Formatting dates and times is a pain when the format can't be easily returned using the standard convert function.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.