SQL select statement dout - sql

I have a table in which it contains the Name column contains some names in Capital letters and some data is in small letters in my WHERE clause if i give (where name='Syed') it will give only matching records because it is case sensitive but i want my output should display like(SYED,Syed,syed) how to do that please help me

SELECT Name
FROM Persons
WHERE UPPER(Name) = 'SYED'
This will return the Name in whatever case it exists as in the table, but will return all instances of it.

Related

SQL check if row value in one column is in another column that is a list of strings

I am pulling from a table that looks something like this:
Name
List
John
'John,Mary,Fred'
Mary
'Jack, John'
I need to pull only the rows where the value in the Name column is in the comma-separated list of names in the List column. In the example above, it would return just the first row. How can I do this using SQL (AWS Redshift version)?
This work?
SELECT *
FROM <table>
WHERE list LIKE '%'||name||'%'
This has a hazard that 'jon' will match 'jonas'. You can fix this will a bit extra conditions. Also do you need this to be case insensitive? Then use ILIKE
If you need to match complete names you need to cover the 4 cases of how a name can show up in a list - first in list, middle of list, end of list, or all of the list. Like so:
SELECT *
FROM <table>
WHERE list LIKE name||',%'
OR list LIKE '%,'||name||',%'
OR list LIKE '%,'||name
OR list LIKE name

Teradata Character Column with non alphabet values

I have a name column in Teradata that has customer full name all in one column. There are some names with -,_,.,/,#,! in between the name characters. I want to be able to pull records where there are names with these conditions. Is there a better option to pull records with the scenario below?
Currently, I am writing query like this
SELECT NAME FROM TABLESOURCE WHERE NAME LIKE ANY('%-%','%.%','%#%','%~%','%!%')
Thanks in advance.
I haven't tested this but I think you could test for equality when those characters are removed from the name using otranslate
select name
from tablesource
where name <> otranslate(name,'-.#~!','')

using case and contains in SQL

I have a column x-property which has values xxx-abc, xxx-def, 123, mno ....etc.
I have another column isx.
I wish to update table to fill up column isx such that if the row in x-property column contains xxx then add abc, else add xyz.
I do not have SQL full text search in my table.
Any help is appreciated.
Instead of contains use like, because from the docs:
CONTAINS is a predicate used in the WHERE clause of a Transact-SQL SELECT statement to perform SQL Server full-text search on full-text indexed columns containing character-based data types.
This is your query:
update
table
set
isx =
case
when x-property like '%xxx%' then 'abc'
else 'xyz'
end

How to select a sql table's column value by giving column index?

I have table with 3 columns. One is Id, second column is Name and the third one Description. How can I select the value in the Description field by giving the column index, 3?
Thanks in advance
You can't, from plain SQL (other than in the ORDER BY clause, which won't give you the value but will allow you to sort the result set by it).
If you are using another programming language to construct a dynamic query, you could use that to identify the column being selected by its index number.
Alternatively, you could parameterise your query to return a specific column based on a case statement - like so:
select a, b, c, d, e, ...,
case ?
when 1 then a
when 2 then b
when 3 then c
when 4 then d
when 5 then e
...
end as parameterised_column
from ...
The problem with referring to a column by an index number is that, one day, someone may add a column and break your application as the wrong value will be returned.
This principle is enforced in SQL because you can select named columns, or all columns using the * syntax.
This principle is not enforced in programming languages, where you can usually access the column by ordinal in code, but you should consider the principle before deciding to use a statement such as (psuedo code)
value = results[0].column[2].value;
It should be possible. You'd have to query the system tables (which do vary from one version of SQL to another) to get the 3rd (or Nth) column name as a string to form a following query using that column name.
In SQL 2000 the tables you'll need to start with are syscolumns with a join to sysobjects for the table name. Then the rank() function on "Colid" will give you the Nth column and "name" (shockingly) the name of the column. Once you've got that in a variable the following command can return the value, compare to it, order by it or whatever you need.
This is how you can retrieve a Column's name by passing it's index.
Here variable AcID is used as the index of the column.
Below is the code e.g
dim gFld as string
vSqlText1 = "Select * from RecMast where ID = 1000"
vSql1 = New SqlClient.SqlCommand(vSqlText1, cnnRice)
vRs1 = vSql1.ExecuteReader
if vRs1.Read then
gFld = vRs1.GetName(AcID)
msgbox gfld
end if
declare #searchIndex int
set #searchIndex = 3
select Description from tbl_name t where t.Id = #searchIndex

Get alias name dynamically in Postgresql

I have one table named tblalias.which is having two columns cid, description
cid description
1 Employee
2 Join Date
3 Retire Date
Like this three record is present
Now I have another table tblemployee. I want to write a query for tblemployee to get record but alias name for that query I want should come from tblalias
select nama as Employee,
joindate as "Join Date",
retiredate as "Retire Date"
from tblemployee
If I change value is tblalias table to my select query should return new value as alias is it possible if yes how please help me
The only way to do this is with dynamic SQL. First fetch the alias names then build the final SQL and execute it.
There is no way doing this with a single "hardcoded" statement.
If you want spaces in names you should quote them. (spaces in names is generally a bad Idea, but that's another matter)