1

I have a table in SQL Server which has an address column of string datatype.

Address values like

{"line1":"Nav Place Road","line2":"Nyork City","line3":"USA 34576"}

I want to get result in a separate column line1, line2, line3, line4 with select query.

I try with split function, but I can't get proper result.

9
  • 3
    Don't store multiple values like this in a single column - this violates the first normal form of database design Commented Nov 20, 2014 at 8:59
  • so your address column contain like this , right ? line1:Nav Place Road,line2:Nyork City,line3:USA 34576 Commented Nov 20, 2014 at 9:01
  • 1
    See simple-talk.com/sql/t-sql-programming/… Commented Nov 20, 2014 at 9:02
  • @marc_s Or to put it another way, it's fine to store multiple values in a column like this. But don't expect access to them from a SQL query; only from your client application. Commented Nov 20, 2014 at 9:03
  • Stored as XML would be better; at least you could query it then. Commented Nov 20, 2014 at 9:04

1 Answer 1

2

You could use JSON Select which providers several functions for extracting different datatypes from JSON. For your example you could do something like this:

select 
    dbo.JsonNVarChar450(address, 'line1') as line1,
    dbo.JsonNVarChar450(address, 'line2') as line2,
    dbo.JsonNVarChar450(address, 'line3') as line3
from your_table

DISCLOSURE: I am the author of JSON Select, and as such have an interest in you using it :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.