What the query to display all data on my table in uppercase? - sql

I just try use
SELECT UPPER(*) FROM TABLE
but it didn't work

I suggest use the sentence UPPER for each varchar or char field in your table.
For example:
SELECT UPPER(Name), UPPER(LastName), UPPER(CityName) FROM ClientsTable
On this way you obtain the data in uppercase.

Related

How to query array which is present as string in postgres?

I have a table called table with column called column with datatype text with values like '["1","2"]'.
I need to get all records which has "1" as one of the element.
select *
from table
where column.....?
How should the where clause be?
Simply use LIKE. Keep the double quotes to pass 1, but avoid other numbers containing that digit.
select *
from table
where column like '%"1"%'
I think you can use ? operator on jsonb type:
select *
from (
select '["1","2"]' union all
select '["0"]'
) as a(data)
where
a.data::jsonb ? '1'
In general, I'd consider storing your data as jsonb instead of string.
db<>fiddle example

Converting db2 column names in select to be lowercase in json file

I currently have a very simple select that my code then dumps into JSON
SELECT user, phone
FROM table t;
But the select returns all uppercase column names, resulting in uppercase JSON keys, which I don't want. Is there a way in DB2 to return lowercase column names?
If you want to get lowercase columns names (not data) in DB2, you must use double quotes around the column names.
SELECT user as "user", phone as "phone"
FROM table t;
If you want the result of the query to be small, try this
SELECT LOWER(user), LOWER(phone)
FROM table t;
Use the LOWER() function;
SELECT LOWER(user) AS 'user', LOWER(phone) AS 'phone'
FROM table t;

Verify if the second character is a letter in SQL

I want to put a condition in my query where I have a column that should contain second position as an alphabet.
How to achieve this?
I've tried with _[A-Z]% in where clause but is not working. I've also tried [A-Z]%.
Any inputs please?
I think you want mysql query. like this
SELECT * FROM table WHERE column REGEXP '^.[A-Za-z]+$'
or sql server
select * from table where column like '_[a-zA-Z]%'
You can use regular expression matching in your query. For example:
SELECT * FROM `test` WHERE `name` REGEXP '^.[a-zA-Z].*';
That would match the name column from the test table against a regex that verifies if the second character is either a lowercase or uppercase alphabet letter.
Also see this SQL Fiddle for an example of data it does and doesn't match.
agree with #Gordon Linoff, your ('_[A-Z]%') should work.
if not work, kindly add some sample data with your question.
Declare #Table Table
(
TextCol Varchar(20)
)
Insert Into #Table(TextCol) Values
('23423cvxc43f')
,('2eD97S9')
,('sAgsdsf')
,('3Ss08008')
Select *
From #Table As t
Where t.TextCol Like '_[A-Z]%'
The use of '%[A-Z]%' suggests that you are using SQL Server. If so, you can do this using LIKE:
where col like '_[A-Z]%'
For LIKE patterns, _ represents any character. If the first character needs to be a digit:
where col like '[0-9][A-Z]%'
EDIT:
The above doesn't work in DB2. Instead:
where substr(col, 2, 1) between 'A' and 'Z'

SQL: Is there a way to get the average number of characters for a field?

Is there a simple sql query that can help me to determine the average number of characters that a (text) database field has?
For instance my field is called "message". Ideally I would love to do something like this...
select average(characterlength(message)) from mydatabasetable
is this even possible through sql?
Thanks!
Edit
Original bad phrasing: In SQL Server, LEN is for varchar fields. For Text fields, try DATALENGTH
Correction because #gbn is right: LEN will not work with Text or NText datatypes. For TEXT, try Datalength.
End Edit
SELECT AVG(DATALENGTH(yourtextfield)) AS TEXTFieldSize
Edit - added
The above is for the TEXT datatype. For NTEXT, divide by 2.
select avg(length(fieldname)) from table
Though the answer could potentially differ depending on your RDBMS.
For MySQL:
SELECT AVG(CHAR_LENGTH(<column>)) AS avgLength FROM <table>
Retrieved from:
http://forums.devshed.com/database-management-46/average-length-field-38905.html
select avg(length(textfield)) from mytable;
Yes, it is possible. In SqlServer for example it would be:
SELECT AVG(LEN(Name)) FROM MyTable
select sum(len(theTextColumn)) / count(*) from theTable;

Conversion failed when converting the varchar value '1,' to data type int

I have table categories (c) and an another table (x) with a column which can contain cat IDs separated with comma as varchar data type. I want to Select related categories but I'm having error "Conversion failed when converting the varchar value '5,' to data type int." when trying to select:
SELECT ID, Title FROM c WHERE ID IN (SELECT catIDs FROM x WHERE ID=any);
The subquery returns data like "1,3,4"
You need to split the 1,3,4 string returned by the subquery into separate int values. SQL Server does not have a built-in function to do it, but you can use this user-defined function.
Create the function dbo.Split in your database and then re-write your query as follows:
SELECT ID, Title
FROM c
WHERE ID IN
(
SELECT s
FROM dbo.Split(',', '1,3,4')
)
I replaced the subquery with example results 1,3,4 to shorten the query and make it easier to understand.
If I get it right, you actually have values like "1,3,4" in your column catIDs. So you should extract a substring in the select of your subquery.
By the way, I'm not an MS SQL Server expert, but it's probably a bad database design to do so. I'm not sure the RDBMS engine will use indexes in such a case...