I require following functionality, How can I frame it in a query?
Select *
from table
where column 4 includes column3
Example:
Please find attachment for table screenshot.
.
I want to extract all the rows where column 4 includes column 3.
Select * from table where column4=column3
Use a LIKE statement with the "value" from column3
select *
from the_table
where column4 like '%'||column3||'%'
|| is the ANSI SQL operator for string concatenation, but not all DBMS respect that.
I would probably use a string find function or something along those lines, and as I saw this being tagged with Mysql, you should be able to use LOCATE
SELECT
*
FROM
table
WHERE
LOCATE(column3, column4) > 0
Related
My question is simple.
In a DB2 Database, I have my table
Table (Id integer, name varchar)
I want to select entries which names like 'ac1%' or 'ac2%' or 'ac3%', so which names match regex
'^ac[123]*'
Is there any method to have this select query without write :
WHERE name LIKE 'ac1%' OR name LIKE 'ac2%' OR name LIKE 'ac2%'
Probably the most efficient method is:
where name >= 'ac1' and
name < 'ac4'
You can also use regular expressions:
where regexp_like(name, '^ac[1-3]')
Depending on your Db2-server platform (Z/OS, i-Series, Linux/Unix/Windows) and Db2-version, you can use REGEXP_LIKE function. This allows regular expressions in SQL.
See documentation and examples here.
i think below will work cause REGEXP_LIKE(col,'[0-9]{3}-[0-9]{3}-[0-9]{2}') will always return true if col contain any of 0 to 9 value
SELECT * FROM table1
WHERE REGEXP_LIKE(name,'[1-3]')
I am using a search algorithm to find tables in my database that meet certain criteria. The output looks like this:
TableName | ColumnName
--------------+---------------
Table1 | Column1
Table1 | Column2
Table1 | Column3
Table2 | Column4
Table2 | Column1
... | ...
Table652 | Column873
I then try to use that table as input for another query where I attempted to use either:
WHERE Tablename.ColumnName LIKE 'Post 2013 - %'
Or
WHERE ((TableName).(ColumnName)) LIKE 'Post 2013 - %'
Or
SELECT ...
CONCAT(TableName, '.', ColumnName) AS TabCol
FROM search
WHERE TabCol LIKE 'Post 2013 - %'
The problem is that my code is returning errors at each of these WHERE statements. Is it possible for me to use the first output as a way to scan through those tables for a specific value, returning the TableName and ColumnName where it found the value?
To do something like that there are generally two methods that I have found that work. One is to extract the entire database to text file and use a text searching tool for the data you seek.
The other is to use the system tables to identify all the tabels / fields. Then have some code loop through those fields running your query. You may want to check the data type first to eliminate things like date, numeric or integer data types. Or you could do this manually. But there is not really as standard Sql way to do this.
If your using SQL Server as Database :- You can use dynamic queries.
you can write a stored procedure or Function which has logic similar to that in the screenshot which return table data which the desired results. And you can pass Tablename , Columnname, Conditions as the parameters . Assuming you have all the data in the table.
Here is a sample query i tried with above scenario.
select * from search;
select * from table2;
Declare #sql varchar(max)
select #sql='select * from '+ tablename+' where '+columname+'='+'''john-her'''
from search where tablename='table2' and columname='namedata2'
select #sql
exec(#sql)
I just tried with sample ...there may be multiple scenario with conditions. Hope this helps you in giving you an idea how to solve your problem!!
I am trying to concatenate multiple columns in a query in SQL Server 11.00.3393.
I tried the new function CONCAT() but it's not working when I use more than two columns.
So I wonder if that's the best way to solve the problem:
SELECT CONCAT(CONCAT(CONCAT(COLUMN1,COLUMN2),COLUMN3),COLUMN4) FROM myTable
I can't use COLUMN1 + COLUMN2 because of NULL values.
EDIT
If I try SELECT CONCAT('1','2','3') AS RESULT I get an error
The CONCAT function requires 2 argument(s)
Through discourse it's clear that the problem lies in using VS2010 to write the query, as it uses the canonical CONCAT() function which is limited to 2 parameters. There's probably a way to change that, but I'm not aware of it.
An alternative:
SELECT '1'+'2'+'3'
This approach requires non-string values to be cast/converted to strings, as well as NULL handling via ISNULL() or COALESCE():
SELECT ISNULL(CAST(Col1 AS VARCHAR(50)),'')
+ COALESCE(CONVERT(VARCHAR(50),Col2),'')
SELECT CONCAT(LOWER(LAST_NAME), UPPER(LAST_NAME)
INITCAP(LAST_NAME), HIRE DATE AS ‘up_low_init_hdate’)
FROM EMPLOYEES
WHERE HIRE DATE = 1995
Try using below:
SELECT
(RTRIM(LTRIM(col_1))) + (RTRIM(LTRIM(col_2))) AS Col_newname,
col_1,
col_2
FROM
s_cols
WHERE
col_any_condition = ''
;
Blockquote
Using concatenation in Oracle SQL is very easy and interesting. But don't know much about MS-SQL.
Blockquote
Here we go for Oracle :
Syntax:
SQL> select First_name||Last_Name as Employee
from employees;
Result: EMPLOYEE
EllenAbel
SundarAnde
MozheAtkinson
Here AS: keyword used as alias.
We can concatenate with NULL values.
e.g. : columnm1||Null
Suppose any of your columns contains a NULL value then the result will show only the value of that column which has value.
You can also use literal character string in concatenation.
e.g.
select column1||' is a '||column2
from tableName;
Result: column1 is a column2.
in between literal should be encolsed in single quotation. you cna exclude numbers.
NOTE: This is only for oracle server//SQL.
for anyone dealing with Snowflake
Try using CONCAT with multiple columns like so:
SELECT
CONCAT(col1, col2, col3) AS all_string_columns_together
, CONCAT(CAST(col4 AS VARCHAR(50), col1) AS string_and_int_column
FROM table
If the fields are nullable, then you'll have to handle those nulls. Remember that null is contagious, and concat('foo', null) simply results in NULL as well:
SELECT CONCAT(ISNULL(column1, ''),ISNULL(column2,'')) etc...
Basically test each field for nullness, and replace with an empty string if so.
I'm using SQLite database and I'm wondering whether I'm allowed to write queries as follows:
SELECT SUM(column1 * column2)
FROM my_table;
I googled but references say that SUM function is has the following format:
SUM([DISTINCT|ALL] column)
And my question is: does column mean actually column or does it allow expressions (like above) too?
You can always use a table expression:
SELECT SUM(Calc)
FROM (
SELECT Column1 * Column2 AS 'Calc'
FROM My_Table) t
I don't have SQLite but checking the docs indicates this should work fine.
Yes, you can use an expression like the one you mentioned, if the datatype of both columns allow it.
I'm am using MS Reporting Services to graph some data from an Oracle database. I want to name the columns in my select statement with values from another select statement. Is this possible?
Like instead of
Select Column1 As 'Test' From Table1
could I do something like
Select Column1 As (Select column2 from Table2 where Value = 1) From Table1
?
I would think you'd have to query out separately, then form the query dynamically. Interested to see if there is a different answer.
My PL/SQL's a little rusty, so what follows is more pseudocode than compilable & tested code. And this is completely off the top of my head. But if you know the specific ordinal location of the column in the table, you may try this:
columnName varchar2(50) :=
Select column_name
From all_tab_columns c
Where lower(table_name) = '<% Your Table2 Name %>' And
column_id = 9 -- The appropriate ordinal
Order By column_id;
Select Column1 As columnName From Table1;
There may be more column values drawn from "all_tab_columns" that'll help you as well. Take a look around and see.
I hope this helps.
You can query all needed column names into separate report dataset, create hidden multivalue report parameter vColumns, set dataset with columns as a parameter default values, and use it as a string array:
Parameters!vColumns(0).Value - will be the first column etc. So you can use them as a query parameters.
See Lesson 4: Adding a Multivalue Parameter