I have certain column names that are named
car_model1
car_model2
bike_model1
bike_model2
I'm trying to regex just the part before the "_" but my regex statement isn't working in SQL Server 2008.
I have:
select COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where table_name = 'dt_model_data'
and COLUMN_NAME like '%([^\_]+)%'
This doesn't return anything but in python this regex statement works. Am I missing something?
Thanks
Related
I have to retrieve distinct entities from a column, all of which start with a vowel. The query looks like this :
Select DISTINCT column_name
FROM table_name
WHERE column_name LIKE '[aeiou]%';
It's not giving compilation errors, but it's not returning anything. Any ideas?
You can use regular expressions:
Select DISTINCT column_name
FROM table_name
WHERE column_name REGEXP '^[aeiou]';
LIKE wildcard patterns do not support character classes, except in a couple of databases that extend the definitions of the LIKE pattern.
Also, you might want:
WHERE column_name REGEXP '^[aeiouAEIOU]'
If you have a case-sensitive collation.
Hi everyone I have multiples tables which I want to get tables with specific column name which I am able with the following code:
SELECT COLUMN_NAME AS 'ColumnName'
,TABLE_NAME AS 'TableName'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'DATABASENAME'
AND COLUMN_NAME LIKE '%columnName_I_Need%' // Example not actual search
ORDER BY TableName
,ColumnName;
Now I want to get the all data from the resulted tables.
For example, get all columns and their data in resulted tables.
This is an example but is not working :
SELECT * WHERE columnName_I_Need = 1
Is this possible with MySQL?
MySQL version: 5.5.5-10.3.23 MariaDB
This is quite complicated. If you used only dynamic SQL, you would need a looping mechanism. My recommendation is to generate the SQL and then run it manually:
SELECT GROUP_CONCAT('SELECT * FROM ', TABLE_NAME,
' WHERE ', COLUMN_NAME, ' = 1'
SEPARATOR ';
'
)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'DATABASENAME' AND
COLUMN_NAME LIKE '%columnName_I_Need%';
Then copy the statement and run it manually.
The 'FROM' is missing in your query. Also when you specify a column name it should be written inside single quotes.
SELECT * FROM your_table_name WHERE columnName_you_Need = 'colum_id'
This is a simple and basic query to access the data from the table.
SELECT * FROM tablename
WHERE can include a condition here.
* is used to select each and every column.
One can use the column name to access a particular column.
I'm looking to replace a string in a CMS multi-site database across a common set of tables. Here is the initial query to collect the target tables:
SELECT TABLE_NAME as target_table
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '%_content'
...against the results of which I'd like to run the following:
UPDATE target_table
SET title = replace(title, 'SEARCH_STRING', 'REPLACE_STRING')
WHERE title LIKE ('%SEARCH_STRING%');
Thanks in advance for the assist!
For a one off, a workable approach is to use SQL to generate a set of SQL statements.
Assuming that the table_schem and table_name don't contain backtick characters, and if your SEARCH_STRING and REPLACE_STRING don't contain single quotes (or are properly escaped), we could do something like this:
SELECT CONCAT('UPDATE `',t.table_schema,'`.`',t.table_name,'` c'
,' SET c.title = REPLACE(c.title, ''SEARCH_STRING'', ''REPLACE_STRING'')'
,' WHERE c.title LIKE (''%SEARCH_STRING%'') ;') AS `-- stmt`
FROM INFORMATION_SCHEMA.TABLES t
WHERE t.table_name LIKE '%_content'
AND t.table_schema NOT IN ('information_schema','mysql','performance_schema')
ORDER BY t.table_schema, t.table_name
we can save the results from the query into a file. and then submit the SQL statements in the file to the MySQL server.
(I think I would be using INFORMATION_SCHEMA.COLUMNS, with tables containing column named 'title' as well as the table_name matching a pattern, but the approach is the same.
Note that this cannot be accomplished in a single SQL statement; the query to get the list of tables is going to have to be a separate statement, separated from the execution of the actual UPDATE statement(s).
EDIT I just took a look at the answer linked to in the question; that is totally unrelated. There's nothing there that would apply to the problem we are trying to solve here.
Because of the way SQL is processed (parse, syntax check, semantic check, determine execution plan, then execute) ... identifiers (e.g. table names) must be supplied as tokens in the SQL text. Identifiers cannot be supplied as values at execution time. That's why we need separate statements.
I am trying to look for a way to replace all occurrences of a particular pattern in my database across many tables and columns, for this I need to create some way to do this, it does not need to be done by a script, just some SQL code that will do this.
For example, I want to replace all occurrences of 'v2' with 'www' but have no idea how to do this.
I am not looking for a tutorial, just a bit of guidance on what to do and how to script the SQL needed.
How do I go about doing this?
Just to guide you in a direction. You can use the replace function:
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, 'SearchForThis', 'ReplaceWithThis')
WHERE SomeOtherColumn LIKE '%PATTERN%';
In an earlier post there is more information:
How can I use mySQL replace() to replace strings in multiple records?
You can use a select CONCAT(...) from information_schema.columns to generate an update query for every table-column combination, where ... is a combination of the strings used in an update query, and column names of the information_schema.columns database.
For example:
select CONCAT("UPDATE ", TABLE_NAME, " SET ", COLUMN_NAME, "=REPLACE(",COLUMN_NAME,"'[string-to-find]'","'[string-that-will-replace-it]'",");") FROM information_schema.columns where table_schema = 'your_db';
COLUMN_NAME and TABLE_NAME are columns in the information_schema.columns table, as documented by MySQL
The above query should make the result set:
UPDATE table1 SET field1 = replace(field1,'[string-to-find]','[string-that-will-replace-it]');
UPDATE table1 SET field2 = replace(field2,'[string-to-find]','[string-that-will-replace-it]');
UPDATE table2 SET field3 = replace(field3,'[string-to-find]','[string-that-will-replace-it]');
...
You could output the results of the SELECT statement to a file, which then becomes a script to execute. Alternatively, if you use phpadmin or any other programming language as an interface, you can cycle through the results set, and execute the value of each row in the results set.
I got the idea from MikeW's answer here, about selecting all rows where a data value exists, and from some other stack overflow answers that I have now lost track of, sadly (sorry to the original writers)
To be honest, I think this question may be a duplicate of this, in addition to this though...
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, 'Text to search, 'Text to replace it with');
Is it possible to do the following in MySQL
SELECT * FROM 'table name' WHERE 'field name' NOT LIKE '%string%';
I'm working on a database previously set up by somebody else - there are lots of field headings for shoe size based on location - i.e. quantity_size_125_grantham
I want to run a query that will SELECT all columns and rows from the database where the field heading name is NOT LIKE '%grantham%'. There are multiple columns for quantity_size_integer_*location* that I want to rule out.
Is this possible?
Thanks,
No, it's not possible to dynamically create a SQL statement and execute it within a single statement. It can be done in two steps.
It is possible to retrieve column metadata from the information_schema database, from the columns, for example:
SELECT c.*
FROM information_schema.columns c
WHERE c.column_name NOT LIKE '%string%'
AND c.table_schema = 'mydatabase'
AND c.table_name = 'mytable'
ORDER BY c.table_name, c.ordinal_position
Since a MySQL statement has to define the columns to be returned, it's not possible to dynamically include/exclude columns in a query.
You can use the information_schema database to help you create a SQL statement, something like this, as an example:
SELECT CONCAT('SELECT '
,GROUP_CONCAT(CONCAT('t.`',c.column_name,'`') ORDER BY c.ordinal_position)
,' FROM `',c.table_name,'` t'
) AS stmt
FROM information_schema.columns c
WHERE c.table_schema = 'mydatabase'
AND c.table_name = 'mytable'
AND c.column_name NOT LIKE '%string%'
GROUP BY c.table_name
NOTE: The length of the string returned by the GROUP_CONCAT function is limited by the group_concat_max_len and the max_allowed_packet variables.
This query only returns a string; it doesn't actually execute the generated statement. That would need to be done as a separate step.
Yes, it's possible. But don't use single quote around identifiers.
If you need to "quote" identifiers, the MySQL standard is to use backticks around them. (It's also possible to use double quotes if SQL_MODE includes ANSI_QUOTES)
This:
SELECT t.*
FROM `table_name` t
WHERE t.`field_name` NOT LIKE '%string%'
will return all rows from the table where value in the field_name column is not set to NULL and the value does not contain the string string.