0

How can I combine several columns (of varying types, e.g. int, datetime, nvarchar) as a single JSON-formatted expression in a select query? For example, I have columns col1, col2 and want the query to return a single column where each row like this:

{col1: val1, col2: val2}

Can I use FOR JSON? I'm using SQL Server 2017, but the database I'm querying is 2008 (100) compatibility mode.

Apologies if this has been asked before, but I couldn't find anything that quite matches, although it seems like a very simple question.

Hadi
  • 36,233
  • 13
  • 65
  • 124
Yury
  • 722
  • 7
  • 14
  • Silly question, but does it help any that my server is actually 2017 - or I still cannot use any of the new JSON stuff on a 2008 database? – Yury Feb 11 '19 at 20:09
  • If the database compatibility is 2008 than you cannot benefit from newer SQL syntax – Hadi Feb 11 '19 at 20:10
  • https://github.com/ahliana/SQLTableOrViewToJSON you doesn’t cannot use new Json object on sql server leeser than 2016 – Sanpas Feb 11 '19 at 20:12
  • @Yury you have to convert columns and concatenate thoses al mano if you want dynamic capability you have to build dynamic query using EXec see previous link it’s a stored procedure convert table to json output – Sanpas Feb 11 '19 at 20:14
  • I appreciate that you guys found the duplicate question, except that there was no (good) answer! I just posted one though :) – Yury Feb 12 '19 at 19:22

0 Answers0