Which datetime format to use SQL Server - sql

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)

Related

Convert nvarchar to datetime - format 19/02/2019 12.00.00 AM

I want to convert 19/02/2019 12.00.00 AM format to datetime in a stored procedure SQL Sercver 2012.
I tried
select cast('19/02/2019 12.00.00 AM' as datetime)
and
select convert(datetime, '19/02/2019 12.00.00 AM', 108)
but it's not working
You need to firstly replace the "." with ":" because no SQL Server time format works with ".". Then you need to use the correct format, 108 is a time only format, 103 is what you are looking for.
select convert(datetime, replace('19/02/2019 12.00.00 AM', '.', ':'), 103)
Reference
Try this with replacing (.) with (:)
SELECT convert(datetime,REPLACE('19/02/2019 12.00.00 AM','.',':'), 103)
You should have the VARCHAR literal in proper format. As other answers suggest, you can go for replace. I would prefer to represent in the below way, as it is more clear. The below one works:
Select convert(datetime, '19/02/2019 12:00:00 AM', 103)
I am suggesting to you to follow the ISO 8601 standard for datetime literals. It will take care in representing the datetime in the same way, irrespective of locale. Read more on datetime datetype. I am putting content about ISO 8601 below for your reference.
ISO 8601 Description YYYY-MM-DDThh:mm:ss[.mmm]
YYYYMMDD[ hh:mm:ss[.mmm]] Examples:
1) 2004-05-23T14:25:10
2) 2004-05-23T14:25:10.487
To use the ISO 8601 format, you must specify each element in the
format, including the T, the colons (:), and the period (.) that are
shown in the format.
The brackets indicate that the fraction of second component is
optional. The time component is specified in the 24-hour format.
The T indicates the start of the time part of the datetime value.
The advantage in using the ISO 8601 format is that it is an
international standard with unambiguous specification. Also, this
format isn't affected by the SET DATEFORMAT or SET LANGUAGE setting.

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.

How to convert oracle to_timestamp to sql?

I want to convert Oracle
to_timestamp(coloum_name,'DD-MM-YYYY') to sql
required output : 24-APR-17 12.00.00.000000000 PM
I know this is old, but it has an very searchable title, and there's no accepted answer.
The TO_TIMESTAMP function converts text representations of dates to a standard date/time format. From your question, it sounds like you have dates stored as characters in the format 'DD-MM-YYYY', but you want SQL Server DATETIME2(7) (based on the number of decimals in the seconds) as your output. It also seems you want the default time to be noon, rather than midnight, since your sample output shows 12:00 PM, not AM.
Using CONVERT with style 103 will change the European styled date to a DATETIME2(7), as shown below. But then you'll need to do a DATEADD to move from midnight (which will be the default value) to noon, which is twelve hours later.
DECLARE #DateSample NVARCHAR(10) = '17-04-2017';
SELECT CONVERT( DATETIME2(7), #DateSample, 103 );
--Results
--2017-04-17 00:00:00.0000000
SELECT DATEADD( HOUR, 12, CONVERT( DATETIME2(7), #DateSample, 103 ));
--Results
--2017-04-17 12:00:00.0000000
The SQL Server default is 24 hour time, so if you absolutely must switch to AM/PM designators, you'll have to convert it back to a string, which seems to be the opposite of what you're trying to do.
This is a way to convert a date/timestamp into varchar2 in Oracle with the format you want
select to_char(yourColumn, 'DD-MON-YY HH.MI.SS.FF9 PM')
from yourTable
SELECT FORMAT(SYSDATETIME(), 'dd-MMM-yyyy h.mm.ss.fffffff tt')

SQL Server Convert ISO 8601 not working as documented

Per MSDN convert should properly parse ISO 8601 dates with timezone using 127 as the style parameter.
The optional time zone indicator, Z, is used to make it easier to map XML datetime values that have time zone information to SQL Server datetime values that have no time zone. Z is the indicator for time zone UTC-0. Other time zones are indicated with HH:MM offset in the + or - direction. For example: 2006-12-12T23:45:12-08:00.
All of the following are valid ISO 8601 dates but return Conversion failed when converting date and/or time from character string.
select convert(datetime, N'2014-02-07T13:51:00+07:00', 127)
select convert(datetime, N'2014-02-07T13:51:00+07', 127)
select convert(datetime, N'2006-12-12T23:45:12-08:00', 127)
Anyone have a solution or workaround for this issue?
Workaround?: Use datetimeoffset:
select convert(datetimeoffset, N'2014-02-07T13:51:00+07:00', 127) --<-- This one works...
select convert(datetimeoffset, N'2014-02-07T13:51:00+07', 127)
select convert(datetimeoffset, N'2006-12-12T23:45:12-08:00') --<-- and this one works...
use datetimeoffset or datetime2 instead of datetime

How to cast varchar to datetime in sql

I need to cast a string to a datetime so i can compare it later.
The varchar that i have is like this format:
29/11/2013 12:00:00 a.m.
And i need it to cast to:
2013-11-29 00:00:00.000
Im using MSSQL Server 2012
Thx for all
Please, have a look a CAST and CONVERT for more information.
Here are some examples:
-- converting from DD-MM-YYYY
select CONVERT(datetime, '29/11/2013 12:00:00 AM', 103)
-- converting from MM-DD-YYYY
select CONVERT(datetime, '11/29/2013 12:00:00 AM', 101)
IF Your date is of type varchar in the database then if you need to retrieve
then these cases arises::
case1:if your data is in the format of "dd-MM-yyyy"
then you need to use query as follows
Query::
select * from [yourTableName] where convert(datetime,YourDateColumn,103) between '2016-02-12'(yyyy-MM-dd) AND '2016-03-12'
case2:if your data is in the format of "dd-MM-yyyy HH:mm:ss"
then you need to use query as follows
Query::
select * from [yourTableName] where convert(datetime,YourDateColumn,105) between '2016-02-12'(yyyy-MM-dd) AND '2016-03-12'