如何解决将代理商名称和电话与地址SQL查询分开
OrderId AgencyName Phone
1234 Test (788) 888-8888
表格数据采用以下格式。
OrderId DataValue
1234 {"AgencyName":"Test","Phone":"(788) 888-8888","submit":false}
解决方法
如果您具有JSON值,请使用JSON_VALUE()
:
select orderid,json_value(datavalue,'$.AgencyName') agencyname,'$.Phone') phone
from mytable
请注意,这需要SQL Server 2016或更高版本。
,对于较低版本,您可以使用-
public class MongoBarConversion {
@Component
@ReadingConverter
public static class ToBarConverter implements Converter<Document,Bar> {
private final ObjectMapper mapper;
@Autowired
public ToBarConverter(ObjectMapper mapper) {
this.mapper = mapper;
}
public Bar convert(Document source) {
JsonNode json = toJson(source);
setObjectId(source,json);
return mapper.convertValue(json,new TypeReference<Bar>() {
});
}
protected void setObjectId(Document source,JsonNode jsonNode) {
ObjectNode modifiableObject = (ObjectNode) jsonNode;
String objectId = getObjectId(source);
modifiableObject.put(ID_FIELD,objectId);
}
protected String getObjectId(Document source) {
String objectIdLiteral = null;
ObjectId objectId = source.getObjectId("_id");
if (objectId != null) {
objectIdLiteral = objectId.toString();
}
return objectIdLiteral;
}
protected JsonNode toJson(Document source) {
JsonNode node = null;
try {
String json = source.toJson();
node = mapper.readValue(json,JsonNode.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return node;
}
}
@Component
@WritingConverter
public static class ToDocumentConverter implements Converter<Bar,Document> {
private final ObjectMapper mapper;
@Autowired
public ToDocumentConverter(ObjectMapper mapper) {
this.mapper = mapper;
}
public Document convert(Bar source) {
try {
JsonNode jsonNode = toJson(source);
setObjectId(source,jsonNode);
String json = mapper.writeValueAsString(jsonNode);
return Document.parse(json);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
protected void setObjectId(Bar source,JsonNode jsonNode) throws JsonProcessingException {
ObjectNode modifiableObject = (ObjectNode) jsonNode;
JsonNode objectIdJson = getObjectId(source);
modifiableObject.set("_id",objectIdJson);
modifiableObject.remove(ID_FIELD);
}
protected JsonNode getObjectId(Bar source) throws JsonProcessingException {
ObjectNode _id = null;
String id = source.getId();
if (id != null) {
_id = JsonNodeFactory.instance.objectNode();
_id.put("$oid",id);
}
return _id;
}
protected JsonNode toJson(Bar source) {
return mapper.convertValue(source,JsonNode.class);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。