postgres select query on text to array type - postgresql-9.1

in my table customer column cc_emails datatype is text and it contains an array I want to query on my cc_emails that give result
select * from customers where cc_emails ? 'ajay#gmail.com'
id name cc_emails(Data Type Text)
1 ajay [ajay#gmail.com,aju#gmail.com]
2 ajay12 [ajay12#gmail.com,aju12#gmail.com]
3 ajay13 [ajay13#gmail.com,aju13#gmail.com]
id name cc_emails(Data Type Text)
1 ajay [ajay#gmail.com,aju#gmail.com]```

change the data type from text to jsonb and use a query on Postgres

Related

SQLite replacing data in existing column

Really new to SQLite and have a basic question that would help me a lot. If I have a table that looks like this:
_id NAME
1 Mark
2 Bob
Let's say that I do an insert statement in to row 2 and put the name John. Will the table look like this:
_id NAME
1 Mark
2 John
Or would I have to query the database, see if row 2 already contains a name, if it does, replace it with John? In other words, it doesn't automatically replace what's there?
There's basically 3 ways to change table contents:
INSERT: this will always add a new row to the table
UPDATE: this will change existing rows in the table
DELETE: this will remove existing rows in the table
Lets say that your table is defined like this:
CREATE TABLE test (
_id INT NOT NULL AUTO_INCREMENT,
Name varchar(32),
PRIMARY KEY (id)
);
and already contains this data:
_id NAME
1 Mark
2 Bob
if you then run
INSERT INTO test (Name) VALUES ('John')
then the table will look like this:
_id NAME
1 Mark
2 Bob
3 John
Note that you could also have done:
INSERT INTO test VALUES( 3, 'John' )
but then you would have to know that 3 is the next available number in the _id column.
If instead you want to change the Name of the row with _id 2, you would run this query:
UPDATE test SET Name='John' WHERE _id = 2

Sqlite: using alias for view in the SELECT query

The table:
CREATE TABLE "table1"( "fld1" Text, "fld2" Text );
The view:
CREATE VIEW v1 AS SELECT * FROM table1
The query:
SELECT V.fld1 FROM v1 V
The result (column name): V.fld1
The question:
Why the name of the field in the query result contains the alias of the view? What should be the text of the query to get valid field name?
Use AS:
SELECT v1.fld1 AS fld1 from v1;

how to bind a repeater inside a repeater on the basis of values from the first repeater

student code| student.Name |Region Name|Location Name| Assets Details |
I have to display asset details on the basis of each student. There can be more than one asset details for one student, how can i use this in repeater or gridview.
Table which i have in SQL.
Student Code | student name | Asset Details
1 Ray laptop
1 Ray Bed
2 Raj Phone
2 Raj charger\
....
I want to display asset details for each unique student code like this on my page.
Student Code | student name | Asset Details
1 Ray laptop
Bed
2 Raj Phone
Charger
How do i perform the same with only one hit from sql.?
You can do the following:
Use a stored procedure to return two results set for the following queries
SELECT DISTINCT StudentCode, studentname FROM StudentInfo
SELECT StudentCode, student name, Asset Details
Create a DataSet for the results with 2 tables. You can name the first table "Student" and the second table "StudentInfo"
Create a relationship for the tables (please use the intellisense for syntax)
DataSet.Relationships.Add("RelationshipName",Table1.StudentCode,Table2.StudentCode)
Bind the GridView with the DataSet
Create a Template column for Asset Details and insert a repeater or Gridview for that column.
On the ItemDataBound event of the GridView.
DataRowView drv = (DataRowView)e.ItemData;
Repeater.DataSource = dv.CreateChildView();
Instead of using two repeater control, try to make a sql query to get data in this format
use such query
select studentID,
stuff((select ',' + AssetDetails
from Tbl_Student t2
where t2.studentID= t1.studentID
order by studentID
for xml path('')),1,1,'') as AssetDetailsString
from Tbl_Student t1
group by studentID
this will return single row for every studentID with Assestdetails in CSV format in same row

Loop queried result

I'm totally new to asp. Sorry, if it's really basic but I couldn't find through my research.
I want to query Table_A by ID and NAME. (ID is PK, Name is optional)
then if ID is found but Name is null, I want to use that ID to query from other table.
Select *
From Table_A;
gives me
ID NAME
1 PAUL
2 BOB
3 NULL
Then save it into somewhere like Cursor in Stored Procedure.
Then during the loop, ID has empty name so run
Select * From Table_B where ID = 3;
If I tag something, please help out to tag correctly.
You don't need to do 2 queries; you can instead do this:
select coalesce(a.name,b.name) as name
, a.id
from table_a a left join table_b b on b.id=a.id;
This will return the name from table a if not null; otherwise from table b.

SQLite: extended field description

In MS-SQL a field can have a description:
Name: FName
Description: First Name
Can a SqLite - table have the same??
Thank you!
Reinhard
I don't think SQLite support that.
An alternative way is to use comments in the create statement, like this
create table foo (
id integer -- field 1
, name text -- field 2
)
then you can get back the create query and see the descriptions.
Example:
select sql from sqlite_master where name = 'foo'
output:
CREATE TABLE foo (
id integer -- field 1
, name text -- field 2
)

Resources