I'm noticing when I make any select statement with json data outputs a column name with random title e.g.
select 'john' as firstname, 'smith' lastname for json path
if I run this in sql management studio (text results) I'll get
JSON_F52E2B61-18A1-11d1-B105-00805F49916B
-------------------------------------------- [{"firstname":"john","lastname":"smith"}]
(1 row(s) affected)
How to change the column name of the generated json data. I've tried using the root option but couldn't override the column title.
This is the same as using XML.
you cannot set the column name
for my opinion since you will always get single row and single column (which means that this is only one value)
the column name have no meaning. but maybe you have different scenario that i am not aware of.
anyway, if you want to workaround it you can use this query
select (select 'john' as firstname, 'smith' lastname for json path) as MyColumn
Related
I have table which contain comma separated string I want to perform like query on 'name' column but 'name' is comma separated so it will not retrieve data easily so I am using replace to eliminate comma and than perform like query on alias column ,but It is not working.is there any way to perform like query on comma separated string
Table:
id name
i school,education
mysql query :
SELECT id,name, lower((REPLACE(name, ',', ''))) as test FROM `list`
where test like '%education%'
You should seriously avoid storing CSV data into single table columns as you are currently doing. That being said, here is one possible workaround:
SELECT id, name
FROM list
WHERE CONCAT(',', LOWER(name), ',') LIKE '%,education,%';
The idea behind the above trick is to build a CSV name string looking something like:
,A,B,C,D,
That is, every single name value is always surrounded by comma boundaries on both sides. Then, we only need to check that ,somename, be present in this CSV string.
I am trying to write a Query to find if a string contains part of the value in Column (Not to confuse with the query to find if a column contains part of a string).
Say for example I have a column in a table with values
ABC,XYZ
If I give search string
ABCDEFG
then I want the row with ABC to be displayed.
If my search string is XYZDSDS then the row with value XYZ should be displayed
The answer would be "use LIKE".
See the documentation: https://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
You can do WHERE 'string' LIKE CONCAT(column , '%')
Thus the query becomes:
select * from t1 where 'ABCDEFG' LIKE CONCAT(column1,'%');
If you need to match anywhere in the string:
select * from t1 where 'ABCDEFG' LIKE CONCAT('%',column1,'%');
Here you can see it working in a fiddle:
http://sqlfiddle.com/#!9/d1596/4
Select * from table where #param like '%' + col + '%'
First, you appear to be storing lists of things in a column. This is the wrong approach to storing values in the database. You should have a junction table, with one row per entity and value -- that is, a separate row for ABC and XYZ in your example. SQL has a great data structure for storing lists. It is called a "table", not a "string".
If you are stuck with such a format and using MySQL, there is a function that can help:
where find_in_set('ABC', col)
MySQL treats a comma delimited string as a "set" and offers this function. However, this function cannot use indexes, so it is not particularly efficient. Did I mention that you should use a junction table instead?
I am developing an application, in one part of it I get a string as the phone number and I have to return the phone provider of that number, in database I have table prefix with this structure:
id , name, code
Some data in it is like:
(23242, 'UK-Mobile-T Mobile', '447984'),
(23243, 'UK-Mobile-T Mobile', '447985'),
(23244, 'UK-Mobile-T Mobile', '447986'),
(23245, 'UK-Mobile-T Mobile', '447987'),
(23246, 'UK-Mobile-Vodafone', '447407'),
(23247, 'UK-Mobile-Vodafone', '447423'),
name is the provider and code is the prefix belongs to that provider
and what I get as input is a phone number just like 447243xxxxx
Question is this:
how should I create a query to return the UK-Mobile-Vodafone as a result when the above input is given ?
please remember length of this code is not same for every country
This may work for you:
select t.*
from table t
where '447243xxxxx' like concat(t.code, '%');
This assumes, among other things, that only one prefix matches each number. Otherwise you need to choose among them.
If you need to choose among them, typically you would want the longest matching one:
select t.*
from table t
where '447243xxxxx' like concat(t.code, '%')
order by length(t.code) desc
limit 1;
And then, if you want to be able to use an index, you don't want to use concat() on the code. Instead, extract the first n characters. This is easy if all have the same length (6) as in your example:
select t.*
from table t
where left('447243xxxxx', 6) = t.code;
Just use the LIKE-operator with GROUP BY:
SELECT NAME FROM TBL_TABLENAME WHERE CODE LIKE '447243%' group by NAME;
I have a variable list_name containing list of names separated by ':'
list_name=(rahul:john:steve) => list_name is of type 'string'
The list contains 1000s of names
Can't change the list_name because we get it from other department/company
want to write a query do to the purpose as shown below, but for that string manipualation would be required i.e. extracting the names from list_names based on the separator ':'
select roll_no from students
where names is rahul or john or steve (problem part)
suggest to use some string manipulation technique in sql to extract the names from the string
PS: I am using proprietary sql and looping is not supported
You can do this in standard'ish SQL using:
select roll_no
from students
where concat(':', list_name, ':') like concat('%:', names, ':%')
In some databases, the where might be written as:
where ':'+ list_name+':' like '%:'+names+':%'
or
where ':'|| list_name||':' like '%:'||names||':%'
select concat(mem_fname, mem_lname) as 'Membership Name'
from membership;
select concat (mem_street, mem_city, mem_state, mem_zip) as 'Membership Address'
from membership;
I am trying to make these 2 concat statements make 1 single table. The table should be like this:
Membership name and membership address are the fields with the correct data below them (tried to make a table in this but it is not letting me).
Now each of these work if I just use 1 concat statement, so I know that they are working and giving me the output that I am looking for but I do not know how to put them into 1 single table. If you want to see the full data for the tables I am looking to make I can post it.
I am using MySQL.
I think you are looking for this:
select concat(mem_fname, mem_lname) as Name,
concat (mem_street, mem_city, mem_state, mem_zip) as Address
from membership;