Convert date time to below format SQL Server - sql

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.

Related

Datediff SQL date conversion

I am trying to use datediff on a date/time column. The date/time column is in dd/mm/yyyy hh:mm:ss format and from what I have read online Datediff works with yyyy/mm/dd hh:mm:ss format.
I have tried to convert bit I am facing problems
DATEDIFF(minute, convert(varchar(c.duration, 20)), '1970/01/01 00:00:00') as "Resolution Target"
Any help would be appreciated
Depends on database you are using ,if you are using Oracle 'yyyy-mm-dd',if it is SQL Server 'yyyy/mm/dd' can be used as datediff function.
DATEDIFF(minute, convert(varchar(c.duration), 20) , '1970/01/01 00:00:00') as "Resolution Target"

How to convert timestamp to datetime

How to convert datetime to timestamp ?
Timestamp like 'yyyy-mm-dd hh:mm:ss.mss'
I try to format this
format(time_field , 'dd.mm.yyyy hh:mm:ss')
but in the end it turns out '1.43.2019 01:43:03'
use MM for month when formatting
SELECT FORMAT(GETDATE(), 'dd.MM.yyyy HH:mm:ss')
use the built in convert function, with a suitable option.
SELECT CONVERT(varchar, getdate(), 21);
https://www.w3schools.com/sql/func_sqlserver_convert.asp
You don't actually need to use format() for this, because the format you want is a "built-in" format for SQL Server's convert() function:
select convert(varchar(255), timefield, 120)
I'm not a big fan on convert() and its arcane formatting options. However, options 120 and 121 are pretty much the ones that I know and use.

Datetime format in SQL Server query

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');

DateAdd converting query from sql to oracle(sql developer)

I'm trying to migrate my sql query to oracle but it seems That i cannot convert my query due to DateAdd function.
STRINGVARIABLE = '1361439468476'
output is : Feb 20,2013
convert (char(12), (dateadd(s, convert(bigint, STRINGVARIABLE) / 1000, convert(datetime, '1-1-1970 00:00:00'))), 107)
DATEADD doesn't exist in Oracle. There are various ways to manipulate dates, but this is fairly straightforward:
select date '1970-01-01' + (to_number('1361439468476') / (1000*60*60*24))
from dual;
DATE'1970
---------
21-FEB-13
... which is actually 21/02/2013 09:37:48, so not sure why you have it as 20-FEB-13.
If you want to keep the millisecond precision you can use a TIMESTAMP instead:
select timestamp '1970-01-01 00:00:00'
+ numtodsinterval(to_number('1361439468476')/1000, 'SECOND')
from dual;
TIMESTAMP'1970-01-0100:00:00'+NUMTODSINTERVAL(TO_NUMBER('1361439468476')/10
---------------------------------------------------------------------------
21-FEB-13 09.37.48.476000000
I'm not sure what the 107 in your query is doing though, or convert, perhaps they are formatting the result as a string?
OK, I see what convert(..., 107) is doing; the equivalent is:
select to_char(date '1970-01-01'
+ (to_number('1361439468476') / (1000*60*60*24)), 'Mon DD, YYYY') as dt
from dual;
DT
------------
Feb 21, 2013
... using to_char() with a Mon DD, YYYY format model.

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)