Link to home
Start Free TrialLog in
Avatar of zachvaldez
zachvaldezFlag for United States of America

asked on

Display Date and Time

I would like to display both date and time as
04/27/2017 01:00 PM . What's the syntax in SQL
In inserting the record it should write this way to in the table
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Use GETDATE() to get the current date and time.
SELECT GETDATE()

Open in new window

To insert or update..
INSERT INTO your_table (goo, foo, boo, some_date_column) 
SELECT 'yada', 'yada', 'yada', GETDATE()

UPDATE your_table
SET some_date_column = GETDATE()

Open in new window

Dates are NOT stored in any particular cosmetic formatting, so to DISPLAY the date as you have in this question choose from any one of SQL expert PortletPaul's Handy Dandy Date Conversion Cheat Sheet.
Avatar of zachvaldez

ASKER

I did this
CONVERT(varchar,getdate(),101) + RIGHT(CONVERT(VARCHAR(19),GETDATE(),100),7), and it work for some time but
now it is getting error...
Simply GetDate() work!
SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi zachvaldez,

Use the below code and you will get the format like 04/29/2017  2:21AM

select convert(varchar(10),getdate(), 101) + right(convert(varchar(32),getdate(),100),8)

Regards,
Tapan Pattanaik
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Guys!
No problem. Will you also choose the answer(s) and close the question?