What format is this date and how can I convert it to a regular datetime? - sql

Amazon for sellers provides order reports. I'm trying to import one of these order reports into a Sql Server database:
Their date fields look like this:
2014-04-30T12:17:28-07:00
2014-04-30T12:24:43-07:00
2014-04-30T12:25:34-07:00
2014-04-30T12:46:02-07:00
2014-07-27T13:10:02-07:00
2014-07-27T13:12:09-07:00
2014-07-27T13:20:42-07:00
2014-07-27T13:23:25-07:00
2014-07-27T13:29:10-07:00
2014-07-27T13:36:16-07:00
2014-07-27T13:51:41-07:00
I cannot figure out which data type to assign this date.
How do I convert this field to be a regular datetime? The solution could be SQL or SSIS or a combination.

Try this.....
SQL Server
SELECT CONVERT(DATETIME,
CONVERT(DATETIME2, '2014-04-30T12:17:28-07:00')
)
RESULT: 2014-04-30 12:17:28.000 --<-- SQL SERVER DATETIME
SSIS
Convert your input column to SSIS Data type DT_DBTIMESTAMP2
Add a derived column task and use the following expression
(DT_DBTIMESTAMP)Input_Column_Name
essentially you are doing the same thing but result will be the same.

Related

Casting nvarchar to date with a formatting in SQL-Server

I'm using SQL Server Management Studio. There is one table in my database containing dates which are stored as nvarchar(255). I want to migrate the data of this table to a new table which I call Converted_Dates and store this data as date. Also, I want them all to be formatted like this YYYY-MM-DD
Currently the Dates table looks like this:
**Dates**
15/6/2011
16/6/2011
2013-03-30
2013-04-16
...
I want the new table to look like this:
**Converted_Dates**
2011-06-15
2011-06-16
2013-03-30
2013-04-16
...
I execute this query but formatting of dates remains the same, only the data type changes from nvarchar(255) to date.
USE [reporting_database]
GO
INSERT INTO [dbo].[Converted_Dates]
SELECT
cast(Dates as date)
FROM [dbo].[Dates]
GO
Any advice on how to cast the data from the old table to a new one in a preferred format?
The value of date and datetime data type is not stored with format in sql server. If you want to see the date in a different format you can manipulate the way that date and datetime data types are displayed when converted to a varchar (or nvarchar,nchar,char) data type using some built in functions.
You should store your dates as date data type, and if you can format them at the application level, do so there. If you must format them in sql, then use convert() styles.
select convert(char(10),getdate(),120)
returns: 2017-05-01
In sql server 2012+ you can use format()
select format(getdate(),'yyyy-MM-dd')
returns: 2017-05-01
But format() is much slower, take a look here: format() is nice and all, but… - Aaron Bertrand

Unable to sort dates in Access with SQL backend

I have an application in Access 2013 with a SQL backend. Its sorts dates like :
01/01/2015
01/02/2013
01/02/2014
.
The ApplicationDate is stored as a Datetime value in SQL. How do I get it to sort in the right order.
It sounds like your data is stored in SQL Server using data type Datetime2.
This cannot be read as a date value in Access, thus the ODBC driver returns it as text which will be sorted as you show.
So either change the data type of the field to Datetime or, in your query, sort on the field converted to date:
Order By CDate([YourDateField])
or, if some records have Null values:
Order By CVDate([YourDateField])

SSIS: What are rules to import date fields from text files to SQL table

I have created a package in |SSIS 2012. The package basically connect to a text files in a predefined location and load data from the text files to the SQL database table. I have one DOB field which is DATE TYPE in the destination SQL table and it was returning error: conversion failed and date out-of-range.
I did not have any luck converting DOB column using the CAST or CONVERT funtion:
CONVERT(date, DOB, 103)
What am I doing wrong?
You should use CAST() or CONVERT() functions, when you want convert varchar value to datetime
https://msdn.microsoft.com/ru-ru/library/ms187928.aspx
I had some bad data in my input files for the DOB column. For example, DOB: 100-01-01, 29999-05-24; I tried to convert these dates using CONVERT function and it was returning error.
So, I was just selecting records only with valid DOB dates using the query as below and after that I could convert the DOB without any error in this case:
SELECT * FROM MyTableTest WHERE ISDATE(dob) <> 1

Change datetime custom format in sql server

I have created a column of datetime type in SQL Server.
The output of this column is:
1994-01-10
But I want to show only year, like this: 1994 (not long datetime)
How can I change this format type of datetime?
My table is tbl_borrowing, my column is VitiBotimit
Thank you
How about this:
SELECT YEAR(VitiBotimit)
FROM dbo.borrowing
DATETIME in SQL Server has no format - it's an 8-byte binary value - and therefore you cannot change the format.
You only get a format (of your choice) when you select the value from the table. If you only need the year - select it that way.

Change Dateformat for nvarchar by sql script

I have to change the datetime format for a given string in a SQL query.
The database is SQL Server 2005, the column is of type nvarchar(1024).
Atm the data is like 2014-05-21, given the date from today as example.
I should write a script in which I can convert from 2014-05-21 to 21.05.2014.
What will be the fastest / easiest way to do this?
You can give a try as below :-
SELECT CONVERT(VARCHAR(10), GETDATE(), 104) AS [DD.MM.YYYY]
For more information :-
http://www.sql-server-helper.com/tips/date-formats.aspx