Postgresql: find multiple records from comma separated column - laravel

I am using PostgreSQL with Laravel 8.
In a Postgres table, I have one comma separated character varying column
I need those records from this table where value of this comma separated column is 43 OR 56 OR 252.

Storing comma separated values in a single column is a huge mistake to begin with. But if you can't change the data model, you can achieve what you want by converting the value into an array, then use the array overlaps operator && with the values you want to test for:
select *
from the_table
where string_to_array(bad_column, ',')::int[] && array[43, 56, 252];

Related

how to find if a string exists in oracle column

i have a table that store mutiple values in column i need to find if the value exists then return entire row
Example : 963963963;35793579;741741
Query am using
select * from table where column like (%963%);
which return all the rows that have the value starts or ends with 963
i need only one row that match with 963
It is generally bad table design to store semicolon separated data this way. If you must continue with your current design, you may use the following query to find an exact match for 963:
SELECT *
FROM yourTable
WHERE ';' || col || ';' LIKE '%;963;%';

Laravel data is not getting inserted for some columns

I am trying to insert the data in laravel where I want to insert 4 columns in DB - empid, name, in-time and out-time.
but i am getting syntax error 'unexpected =' for columns separated by '-' e.g: in-time, out-time.
Is there any way to allow inserting values for '-' separated columns in DB using laravel ?
You should use underscores for attributes names but you can still use it like this:
$attendata->{'in-time'} = $col[8];
$attendata->{'out-time'} = $col[9];
Please remove hyphens(-) from your column fields. As the columns fields are treated as properties of the model which acts a variable. So if talk about the variable they can't have any special characters except (_). You can use in_time or out_time instead of using in-time or out-time.

how to selects rows based on a single value against a comma separated column in oracle?

I have a table structure like below:
TestTable:
ID(number)|Names(clob)
1 'a','b','c','d'
2 'b','c','d'
3 'g','h','e'
Now I want to select rows in which Names column contains 'b',so the expected output will be the first 2 columns.
How can do it in Oracle.
Please remember, the solution has to work in Oracle 10g as well as 11g.
Thanks in advance.
Try this using LIKE operator:
select
*
from your_table
where ',' || Names || ',' like '%,b,%';
Concatenated commas are to cover for the cases where b is present at the start or end of the string.

Replace data of one column with substring of another column in sql loader

I am loading data from a csv file into a table using sqlldr. There is one column which is not present in every row of the csv file. The data needed to populate this column is present in one of the other columns of the row. I need to split (split(.) )that column's data and populate into that column.
Like:-
column1:- abc.xyz.n
So the unknown column(column2) should be
column2:- xyz
Also, there is another column which is present in the row but it's not what I want to input into the table. It is also needed to be populated from column1. But there are around 50 if-else cases in that. Is decode preferable to do this?
column1:- abc.xyz.n
Then,
column2:- hi if(column1 has 'abc')
if(column1 has 'abd' then 'hello')
like this there are around 50 if-else cases.
Thanks for help.
For the first part of your question, define the column1 data in the control file as BOUNDFILLER with a name that does not match a table column name which tells sqlldr to remember it but don't use it. If you need to load it into a column, use the column name plus the remembered name. For column2, use the remembered BOUNDFILLER name in an expression where it returns the part you need (in this case the 2nd field, allowing for NULLs):
x boundfiller,
column1 EXPRESSION ":x",
column2 EXPRESSION "REGEXP_SUBSTR(:x, '(.*?)(\\.|$)', 1, 2, NULL, 1)"
Note the double backslash is needed else it gets removed as it gets passed to the regex engine from sqlldr and the regex pattern is altered incorrectly. A quirk I guess.
Anyway after this column1 ends up with "abc.xyz.n" and column2 gets "xyz".
For the second part of your question, you could use an expression as already shown but call a custom function you create where you pass the extracted value and it would return the searched value from a lookup table. You certainly don't want to hardcode your 50 lookup values. You could do the same thing basically in a table level trigger too. Note I show a select statement for an example only but this should be encapsulated in a function for reusability and maintainability:
Just to show you can do it:
col2 EXPRESSION "(select 'hello' from dual where REGEXP_SUBSTR(:x, '(.*?)(\\.|$)', 1, 2, NULL, 1) = 'xyz')"
The right way:
col2 EXPRESSION "(myschema.mylookupfunc(REGEXP_SUBSTR(:x, '(.*?)(\\.|$)', 1, 2, NULL, 1)))"
mylookupfunc returns the result of looking up 'xyz' in the lookup table, i.e. 'hello' as per your example.

Import CSV which every cell terminated by newline

I have CSV file. The data looks like this :
PRICE_a
123
PRICE_b
500
PRICE_c
1000
PRICE_d
506
My XYZ Table is :
CREATE TABLE XYZ (
DESCRIPTION_1 VARCHAR2(25),
VALUE NUMBER
)
Do csv as above can be imported to the oracle?
How do I create a control.ctl file?
Here's how to do it without having to do any pre-processing. Use the CONCATENATE 2 clause to tell SQL-Loader to join every 2 lines together. This builds logical records but you have no separator between the 2 fields. No problem, but first understand how the data file is read and processed. SQL-Loader will read the data file a record at a time, and try to map each field in order from left to right to the fields as listed in the control file. See the control file below. Since the concatenated record it read matches with TEMP from the control file, and TEMP does not match a column in the table, it will not try to insert it. Instead, since it is defined as a BOUNDFILLER, that means don't try to do anything with it but save it for future use. There are no more data file fields to try to match, but the control file next lists a field name that matches a column name, DESCRIPTION_1, so it will apply the expression and insert it.
The expression says to apply the regexp_substr function to the saved string :TEMP (which we know is the entire record from the file) and return the substring of that record consisting of zero or more non-numeric characters from the start of the string where followed by zero or more numeric characters until the end of the string, and insert that into the DESCRIPTION_1 column.
The same is then done for the VALUE column, only returning the numeric part at the end of the string, skipping the non-numeric at the beginning of the string.
load data
infile 'xyz.dat'
CONCATENATE 2
into table XYZ
truncate
TRAILING NULLCOLS
(
TEMP BOUNDFILLER CHAR(30),
DESCRIPTION_1 EXPRESSION "REGEXP_SUBSTR(:TEMP, '^([^0-9]*)[0-9]*$', 1, 1, NULL, 1)",
VALUE EXPRESSION "REGEXP_SUBSTR(:TEMP, '^[^0-9]*([0-9]*)$', 1, 1, NULL, 1)"
)
Bada-boom, bada-bing:
SQL> select *
from XYZ
/
DESCRIPTION_1 VALUE
------------------------- ----------
PRICE_a 123
PRICE_b 500
PRICE_c 1000
PRICE_d 506
SQL>
Note that this is pretty dependent on the data following your example, and you should do some analysis of the data to make sure the regular expressions will work before putting this into production. Some tweaking will be required if the descriptions could contain numbers. If you can get the data to be properly formatted with a separator in a true CSV format, that would be much better.

Resources