Datetime format in SQL Server query - sql

I need to show a datetime column in the following fromat 04/11/2016 01:38:03 PM, but it is showing in the format 4/11/2016 1:38:03 PM.
Is there any specific function or syntax to get that desired format?

I suppose function FORMAT should help you:
select
FORMAT(GETDATE(), 'dd/MM/yyyy hh:mm:ss tt');
More details about format you can found in official documentation:
https://msdn.microsoft.com/en-us/library/ee634398.aspx

This query will give you output in exact same format(04/11/2016 01:38:03 PM) :
select FORMAT(GETDATE(), 'MM/dd/yyyy hh:mm:ss tt');
See the output:enter image description here

You need to try this..
select FORMAT(GETDATE(), 'mm/dd/yyyy hh:mm:ss tt');

Related

How to display a timestamp in DD-MMM-YYYY HH:MM:SS in hive

My data column has "2022-04-09 11:30:00" and i need to display it as "09-Apr-2022 11:30:00"
i am not familiar with unix_timestamp
You can use date_format() with desired formatted data.
SELECT date_format('2022-04-09 11:30:00', 'yyyy-MMM-dd hh:mm:ss')

SQL Date Format with T between Date and Time

I have an output that shows a timestamp field as something like "2020-06-29T23:58:34.000Z". How can I properly format it to something like "2020-06-29 23:55:34"?
Thanks!
Use to_char():
select to_char(timestamp_field, 'YYYY-MM-DD HH24:MI:SS')

Converting String Date to Date Time in Oracle

I have a situation where I need to convert the datetime value stored as string to Timestamp:
I am using oracle database
This actually works for me select TO_DATE('11-27-2013 21:28:41', 'MM-DD-YYYY HH24:MI:SS') from dual;
But my date value now is diffent from the above:
select TO_DATE('Sunday 6/1/2014 8:00AM', 'MM-DD-YYYY HH24:MI:SS') from dual; - failed. I have 'Sunday' inside my date.
You have to specify a correct format mask to the TO_DATE function. See a full list of format masks along with documentation for the function here: http://www.techonthenet.com/oracle/functions/to_date.php
You can correct your problem by:
SELECT TO_DATE('Sunday 6/1/2014 8:00AM', 'DAY M/D/YYYY HH:MIAM') FROM DUAL;
Try using the correct format. I think this will work:
select TO_DATE('Sunday 6/1/2014 8:00AM', 'DAY MM/DD/YYYY HH:MI AM')
Here is a SQL Fiddle.
The following seems to work fine:
SELECT TO_DATE('Sunday 6/1/2014 8:00AM', 'DAY MM/DD/YYYY HH:MIAM') FROM DUAL
Share and enjoy.

Convert date time to below format SQL Server

I tried to obtain date in this format:
'05-31-2014 01:20:25 AM'
I used below code:
Sql Fiddle here, but output date strangely changed to a different date: 30-26-2011 01:30:38 AM
select format(CAST('2011-11-26 01:30:38.000' AS datetime), 'mm-dd-yyyy hh:mm:ss tt')
Sql Fiddle here
When using the FORMAT() function, mm is minutes, MM is month, so change to:
SELECT FORMAT(CAST('2011-11-26 01:30:38.000' AS DATETIME), 'MM-dd-yyyy hh:mm:ss tt')
If there's already an appropriate format available via CONVERT(), that is preferable as it performs better than the FORMAT() function.

Which datetime format to use SQL Server

I have a date as a varchar in the form
DD/MM/YYYY HH:MM:SS AM
e.g.
16/3/2012 4:39:26 PM
I can't see a valid format option for CONVERT() in the MSDN page
Am I missing something or will I have to reformat the varchar field first?
Edit:
Corrected the format, sorry about that
Once you get your story straight, you'll want one of these:
-- if you really meant dd/mm/yyyy then:
SELECT CONVERT(DATETIME, '16/3/2012 4:39:26 PM', 103);
-- if you really meant mm/dd/yyyy then:
SELECT CONVERT(DATETIME, '3/16/2012 4:39:26 PM', 101);
But agreed with Madhivanan. Don't store dates using the wrong data type, and if you must, use an unambiguous format!
Always use proper DATETIME datatype to store dates
YYYYMMDD and YYYYMMDD HH:MM:SS are unambiguous date formats
For more information, refer this
http://beyondrelational.com/modules/2/blogs/70/posts/10898/understanding-datetime-column-part-ii.aspx
This will work.
DECLARE #dt varchar(100)='2012/3/16 4:39:26 PM'
select convert(datetime,#dt,101)