I have this SQL query:
SELECT clientName FROM Clients;
resulting in:
clientName
"Einstein"
"Edison"
"Newton"
I want to add an empty record on top of "Einstein" like this
clientName
""
"Einstein"
"Edison"
"Newton"
Please help me with a SQL sintax for this, and i want it to work in msaccess.
Thanks
add UNION
SELECT DISTINCT '' AS clientName FROM Clients
UNION
SELECT clientName FROM Clients
Try This Query:
SELECT '""' AS clientName FROM Clients UNION SELECT clientName FROM Clients
Actually this is more or like same as that of J W. But he seems to forget the ""
This is DB2 version
SELECT '""' FROM SYSIBM.SYSDUMMY1
UNION
SELECT SELECT clientName FROM Clients;
The trick in Access is to change the query type to a Pass-Through query and then write it as Luv did with a few minor changes. This is what it looks like against a WCS DB2 table.
SELECT '' AS DisplayName FROM SYSIBM.SYSDUMMY1
UNION
SELECT DB2ADMIN.ACACTDESC.DISPLAYNAME
FROM DB2ADMIN.ACACTDESC;
Related
With this query:
SELECT *
FROM table1
WHERE name = 'Peter'
I can retrieve all data from Peter from table1. This can be done with the "Wildcard *".
Question
Is there any kind of wildcard for the WHERE part? For example:
SELECT *
FROM table1
WHERE name = *
This option of course not working, but I am looking for a wild card there so that all names will be included in my query. I know it's easier to remove the WHERE statement, but due to some reasons I still need it.
SELECT *
FROM table1
WHERE True OR name = 'Peter'
;
This may look silly, but it can come in handy when generating query strings, eg in PHP/PDO:
$query = "SELECT * FROM names
WHERE ($ignore_name OR name = :the_name)
AND ($ignore_address OR address LIKE :the_address)";
, where the $ignore_xxx variables are either True or False, and completely under your control (not user-input!)
select *
from table1
where name = 'Peter' or name = name;
You can query output into your WHERE clause like so:
SELECT *
FROM table1
WHERE [name] IN (SELECT DISTINCT [name]
FROM table1)
I need to select where column contains numbers only and ends with a hyphen
I'm running SQL Server Management Studio v17.9.1
I have tried:
select * from [table] where [column] like '[0-9]*-'
select * from [table] where [column] like '[0-9]{1,}-'
select * from [table] where [column] like '[0-9]{1,2}-'
none of these work. The expression ([0-9]*-) works in any regex tester I've run it against, SQL just doesn't like it, nor the other variations I've found searching.
You can filter where any but the last character are not numbers and the last is a dash. DATALENGTH/2 assumes NVARCHAR type. If you're using VARCHAR, just use DATALENGTH
SELECT
*
FROM
[table]
WHERE
[column] like '%-'
AND
LEFT([column], (datalength([column])/2)-1) NOT LIKE '%[^0-9]%'
SQL Server does not support regular expressions -- just very limited extensions to like functionality.
One method is:
where column like '%-' and
column not like '%[^0-9]%-'
You can use left() and right() functions as below :
with [table]([column]) as
(
select '1234-' union all
select '{123]' union all
select '1234' union all
select '/1234-' union all
select 'test-' union all
select '1test-' union all
select '700-'
)
select *
from [table]
where left([column],len([column])-1) not like '%[^0-9]%'
and right([column],1)='-';
column
------
1234-
700-
Demo
Let's say
SELECT 'Max' AS Foo
results into a result with one field. But there are also ways to give multiple values into your SQL application like
SELECT * FROM Customers WHERE Name IN ('Max','Tim')
question:
Is there also a way to display multiple rows like
SELECT ('Max','Tim') AS Foo
you can try like below
SELECT Stuff(
(SELECT N', ' + Name FROM table_name where Name IN ('Max','Tim')
FOR XML PATH(''),TYPE)
.value('text()[1]','nvarchar(max)'),1,2,N'') as name
You can use union statement
select 'max' as foo
union
select 'tim' as foo
Can we use switch case to filter the columns of a table in this way?
select distinct stud.name, stud.num,
case WHEN stud.sub like '%data%' THEN stud.sub
WHEN stud.sub like '%informatics%' THEN stud.sub
WHEN stud.sub like '%genomics%' THEN stud.sub
ELSE '---'
END
from table_A
The expected result is
Name ID Sub
victor 2098 -----
Lui 6754 Bioinformatics
Willis 7653 Advanceddatascience
Thanks!
Yes, your query should work. If you want to try something different, this should work in Oracle. Easy to change it to any other RDBMS.
Also easy to add any other subject.
With aux as ( select '%data%' as auxText from dual union all
select '%informatics%' as auxText from dual union all
select '%genomics%' as auxText from dual )
select distinct stud.name, stud.num,
nvl(stud.sub,'---') as sub
from table_A left join aux on table_A.sub like aux.auxText;
Would anyone know what to do with this I am doing a like query to select info. example:
SELECT *
FROM customers
WHERE customer_name LIKE '26%'
which will return
26_xx
26_xx
265_xx
but i only want to display 26_xx
I have tried which was suggested from a site :
SELECT *
FROM customers
WHERE customer_name LIKE 'H%!%' escape '!';
but that also returned
26_xx
26_xx
265_xx
In T-SQL you can use [] to escape the wildcard character _;
SELECT * FROM customers
WHERE customer_name LIKE '26[_]%'
The ESCAPE version would be;
SELECT * FROM customers
WHERE customer_name LIKE '26!_%' ESCAPE '!'
SELECT *
FROM customers
WHERE customer_name LIKE '26_%'
Is this what you mean? Only where the names start "26_" or do I read your question wrong?
This should work:
SELECT *
FROM customers
WHERE customer_name LIKE '26[_]%'
and len(customer_name) = 5
Proof:
select
sample_data.*
from
(
select '26_55' as customer_name
union
select '26_abc'
union
select '265_22'
union
select '26-22'
union
select 'abc'
union
select '26_18'
) sample_data
where sample_data.customer_name like '26[_]%'
and len(sample_data.customer_name) = 5