SQL Server date datatype becomes nvarchar - sql-server

After so many years, I have become part of a project that use SQL server. This time it's 2017. I found a very weird behavior when I create a table.
create table test (sampledate date)
If I run above and check the data type of column sampledate, it shows nvarchar instead of date. This causes an error in my application.
Btw I'm using DBVisualizer to check the data type, I believe this is not because of this tool.

Have a look at this section of the Microsoft documentation. It explains that with down-level clients, the backward compatibility of the date data type is ensured by being converted to String/Varchar. So it may really comes from the DBVisualizer usage.
For your app, check the version of the client you use.

Related

Pulling Data from SQL Server pushes date backward one day. How to resolve?

I am using R's odbc package to pull data from a SQL server DB. The data is private, but my queries looks something like this
select
*
from
tbl
where
[some date] >= '2019-01-01'
but when I receive the data, some.date contains values like "2018-12-31" which doesn't make any sense. R's tbl(conn, tbl) tells me that [some date] is of date type not datetime type. But not sure if tbl() changes the type.
Under what circumstance does this happen? where the pulled date is goes back one day before.
Also, I tried changing the timezone on my machine to match that of what's in the Docker, but I can't replicate the error.
Basically, the error doesn't occur on my machine but only occurs when I run it in a docker container.
Not sure if the issue is to do with R.
I tried a few things but can't get to the bottom of them.
Just wondering if there are other avenues to explore e.g. does the SQL server have time settings etc, or bugs in R's odbc library etc.

Create datetime2 as custom datatype in SQL Server 2005

Many have come across the fact that SQL Server 2005 doesn't support datetime2. I was wondering if I can add it as a custom datatype instead.
I created a custom type with the name datetime2 so that's done.
Now I need to set the min date value, but is that even possible, since the custom type is based on the datetime type?
Short answer: No you can't.
Furthermore, DATETIME2 has additional properties, regarding fractional precision. I'd highly recommend any SQL Server DBA to migrate their server to at least 2008, to open up the rich features that are not available in 2005. I don't see any advantage to using something that is largely deprecated and over 10 years old. Especially since you're resorting to creating UDTs that will potentially create all kinds of RDBMS havoc in the future.
To get over this I changed the datatypes in the database to varchar. next I modified the stored procedure to do the conversion between the date entered in the search form and the values in the database. It's an ugly solution and I have to 'manually' compare year, month and day values, but it works in the end with a minimal effort. Customer happy, developer... on the fence :-)

Sql Server date type appears as text field in MS Access

I have a MS Access 2010 front end / SQL Server 2012 back end database, with a number of date fields in different tables. Sometimes I need to store the time, so I have used data types datetime or smalldatetime. However certain fields only need to store the date, so I used data type Date.
My problem is that in MS Access, my ODBC-linked table shows the Date data type fields as Text. This is then leading to problems with some dates being stored in the yyyy-dd-mm format and others in the yyyy-mm-dd format.
Is this a bug? Do I need to use smalldatetime?
Thanks for any assistance,
Jim
Had the same issue myself linking Access 2007 and SQL Server 2008.
See this question, if you look at Albert D. Kallal's comment to the first answer, it tells us that the problem is with an outdated driver connecting the front end to the back end.
If you aren't able to choose an up-to-date driver (and bear in mind that even if you can, other users of your database on different client machines may not be able to) the workaround is to use datetime data type in every case.
Just to expound on the comment given by #BiigJiim I actually had the Native client 11.0 driver already installed but as I was creating DSN-Less table connections I had to change my connection string formally to: Driver={SQL Server Native Client 11.0};
Also as an additional note, I do not believe the Date and DateTime2 data types are recommended for Microsoft Access integration. If memory serves me correctly it recommends either DateTime and SmallDateTime. I get not needing the Time in a lot of circumstances, but you can easily format it via the front end... especially within Access.

Cannot Find Data Type in SQL Server 2014 Express

MSDN lists "TIME" as an available Data Type. This is good, since I need time variables and the ability to perform functions on them (in the form of 00:00:00). Datetime is not an option because my scheduling database cares about Monday - Sunday, not 11/13/14, etc... When i go to create a column with data type time, it doesn't exist. I also tried doing it via query:
ALTER TABLE dbo.DataSchedule
ALTER COLUMN StartClock TIME
And get the message:
Msg 2715, Level 16, State 6, Line 1
Column, parameter, or variable #6: Cannot find data type TIME.
How can I resolve this issue? Is this a problem with 2014 express as opposed to the standard edition?
Thanks for reading.
Per SQL Server documentation TIME datatype is available from SQL Server version 2008 and above. I am pretty much sure that, you connected to a SQL Server 2005 instance and tried using the TIME datatype.
Also, it doesn't matter which SSMS version you are using to connect to since that's just the client. Make sure that, you are actually connected to a server version 2008 and above.

SSMA timestamp. What's it for, how is it used?

I rencently used the SQL Server Migration Assistant to import a database into SQL Server 2005. I noticed that a number of tables that were imported have been ammended with a new column called SSMA_timestamp.
Can anyone tell me what this is for and how it would be used?
The added SSMA_timestamp columns are not only used during migration. They actually help avoid errors when Access updates records in tables linked to SQL Server. So if you are still using an Access front end linked to the migrated SQL Server database, it would be best to not drop the SSMA_timestamp columns.
From the MSDN article Optimizing Microsoft Office Access Applications Linked to SQL Server:
Supporting Concurrency Checks
Probably the leading cause of updatability problems in Office Access–linked tables is that Office Access is unable to verify whether data on the server matches what was last retrieved by the dynaset being updated. If Office Access cannot perform this verification, it assumes that the server row has been modified or deleted by another user and it aborts the update.
There are several types of data that Office Access is unable to check reliably for matching values. These include large object types, such as text, ntext, image, and the varchar(max), nvarchar(max), and varbinary(max) types introduced in SQL Server 2005. In addition, floating-point numeric types, such as real and float, are subject to rounding issues that can make comparisons imprecise, resulting in cancelled updates when the values haven't really changed. Office Access also has trouble updating tables containing bit columns that do not have a default value and that contain null values.
A quick and easy way to remedy these problems is to add a timestamp column to the table on SQL Server. The data in a timestamp column is completely unrelated to the date or time. Instead, it is a binary value that is guaranteed to be unique across the database and to increase automatically every time a new value is assigned to any column in the table. The ANSI standard term for this type of column is rowversion. This term is supported in SQL Server.
Office Access automatically detects when a table contains this type of column and uses it in the WHERE clause of all UPDATE and DELETE statements affecting that table. This is more efficient than verifying that all the other columns still have the same values they had when the dynaset was last refreshed.
The SQL Server Migration Assistant for Office Access automatically adds a column named SSMA_TimeStamp to any tables containing data types that could affect updatability.
I think this is generated so that the Migration assistant can detect changes to the data during the migration.
Unless you are continuing to use Access as a front end to this specific database you have migrated to SQL Server (in which case see Simon's answer), I don't think they will be used for anything after migration is complete, so it should be safe to drop these new columns once you are sure everything is done.
<!-- Set project preference.
Preference path/name/value can be found in preferences.prefs file stored in SSMA project directory.
Preference path is the node name path starting from root to leaf node separating by "/". -->
<set-project-preference preference-path="prefs/ssma-for-access/a2ss/conversion"
preference-name="timestamp-columns-opt"
preference-value="never" />
From SSMA GUI you can also click tools--> default project setting --> conversion --> Tables --> add timestamp columns --> set to Never

Resources