Unable to sort dates in Access with SQL backend - sql

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])

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

is it possible to store m - m / Y formatted datetime with SQL Server

I need to store month - month / year formatted data in an SQL Server database. 01 - 12 / 2014 means, for instance, January 2014 to December 2014. Should I use varchar datatype or is it possible to do this with datetime datatype?
You cannot do this with a single date column in SQL Server. I would encourage you to store this as two date columns, such as: BeginDate and EndDate. That would be the best way to a period of time using native SQL types.
If you have to store this as a varchar(), then put the field in sensible order. Use a format like: 'YYYYMM-YYYYMM'. This allows each component to be parsed out easily and to be sorted by the first value. However, let me repeat my first advice: the best way to store a range of time is to use two columns, one for the start date and one for the end date.

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

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.

SSAS - dsv displays datetime instead of date

I added named query to my data source view, converting datetime to date. When I run the query it displays datetime anyway. Do you know how to fix this? I need date format to match with the Time dimension.
I'm using SQL Server Data Tools 2012.
The SQL DATE datatype accepts time values, so that is how it is shown in the Query preview. If the datatype of your Time dimension key is also DATE, then you should be OK to continue. Have you tried?
FWIW I would use a BIGINT key for a Date Dimension, containing a date represented as YYYYMMDD. This allows for extra rows e.g. with a -1 Key for unspecified dates (e.g. null in Fact data).

Update table Error Using Convert Function In SQL Server 2005

I have a table with two columns, all of them are datetime value
Such as, Column A with value ‘07/09/2012 14:13:34’
Now, I want to update column A to yyyymmdd by statement
Update Change_Date
SET A = CONVERT(VARCHAR(8),A,112)
It shows succsessful message but with no effect (no update value to 20120907) in my table Change_Date.
Any help will be greated, thank you!
A datetime fields saves a date time. How you see that date time is a result of the tool you're using to inspect the data, whether it is Management Studio, or your own software that's printing something from the database.
I strongly recommend keeping it as a datetime field. This will allow you to do date-related operations, such as subtractions and comparisons. If you want to change how your users see the date, then format your date at the presentation layer.
What's happening in the code you've posted is that you're setting the value of A to the same date that it already is. The fact that you're setting that value by means of a string in another format has no relation, SQL server will always have to parse your string input into a date that it can understand. This is why you're not getting an error message. The operation is working, only it's not changing anything.
You can select the date column in specified format or make a view which selects the column value in yyyymmdd format:
SELECT CONVERT(VARCHAR(8), A, 112) FROM Change_Date
It's because the datatype of the column is DATE or DATETIME and it has specific format. If you want to update the column with specific format, make another column and make its datatype VARCHAR. I believe 112 is yyyymmdd format.
I strongly suggest that you keep it AS IS. Database is the storage of data and not for viewing purposes. It is easy to perform task for dates if your data type is DATETIME or DATE. If for instance you want to retrieve the dates with specific format, that's the time you convert your date.
Hope this makes sense.