SQL Server Data Types To JSON Data Types

Facebooktwitterredditpinterestlinkedinmail

In SQL Server 2016 they are added support for outputting your query results in JSON. When you serialize your data to JSON using the FOR JSON clause, a data type conversion happens. The data types are converted from SQL Server data types to JSON supported data types.

For more information on how to convert your result set to JSON: www.sqlserver.info/syntax/json-in-sql-server/

Here is a quick reference table showing the data type conversions that are happening:

[table width=”500″ colwidth=”250|250″ colalign=”left|left”]
SQL Data Type,JSON Data Type
VARCHAR/NVARCHAR,String
CHAR/NCHAR,String
INT,Number
BIGINT,Number
FLOAT,Number
DECIMAL,Number
NUMERIC,Number
BIT,Boolean
DATETIME,String
DATETIME2,String
DATE,String
TIME,String
DATETIMEOFFSET,String
VARBINARY,Base64-encoded string
BINARY,Base64-encoded string
IMAGE,Base64-encoded string
TIMESTAMP,Base64-encoded string
ROWVERSION,Base64-encoded string
UNIQUEIIDENTIFIER,String
MONEY,String
[/table]

 

Get Filestream Storage Directory

Facebooktwitterredditpinterestlinkedinmail

Filestreams are a great way to store files in SQL Server. SQL Server makes this super easy by managing the storage of the physical files on the file system somewhere. But what if you want to get access to these files or if you just want to know where they’re stored. SQL Server provides a function called PathName that you can call to get this path. You call this function like you would call an extension method in .Net. In the example below, we’ll assume that the column InvoiceFile is a Filestream datatype.

SELECT	TOP 1
		InvoiceFile.PathName() AS DirectoryOfFile
FROM	Invoice

Varchar Vs Nvarchar

Facebooktwitterredditpinterestlinkedinmail

People often want to know the difference between varchar vs nvarchar. If you are new to nvarchar, I don’t blame you for not understanding the difference… as they are very similar.

The major difference is that nvarchar stores data as Unicode, where varchar does not. This uses more space, but provides flexibility to handle foreign characters that would not fit in a varchar field.

 
How Varchar And Nvarchar Are Similar

  • They are both used to store text/string data in them
  • The amount of space that both use depends on the size of the data that you are putting in it. It grows with every character that you store.

 
How Varchar And Nvarchar Are Different

  • Nvarchar stores data as Unicode. Varchar stores data as non-Unicode.
  • Varchar stores data at 1 byte per character. Nvarchar stores data at 2 bytes per character.
  • Varchar supports up to 8000 characters in the field definition. Nvarchar only supports up to 4000 characters. Varchar(max) and nvarchar(max) not included.

 
My Thoughts On Which One To Use
Where I am at, I deal with a lot of data. Although it would be nice to be able to support all foreign characters in the database, it is likely that I will never get it. My philosophy is that unless you need to support these extended Unicode characters, just make the field varchar. Nvarchar uses double the space as varchar. Unless your business has a requirement for this, just save the space.

What Is SQL Server

Facebooktwitterredditpinterestlinkedinmail

What Is SQL Server
SQL Server is a database management system (DBMS) produced by Microsoft. The purpose of SQL Server is to store and retrieve data for use in computer applications. These applications could be mobile apps, web sites, desktop applications, or more.

Data in SQL Server is stored in tables. These tables have rows and columns, much like you would see in an Excel spreadsheet. SQL Server accesses this data using a coding language called T-SQL (Transact-SQL). This code is called a query. Here is a sample query that pulls all of the data from a table.

SELECT	*
FROM	AnimalTable

 
Easiest Definition Of “What Is SQL Server”
SQL Server is a program built by Microsoft that other programs can use to store and retrieve data.