I am looking to make a array out of this pug template:
each test in profile
= '["' + test.profile_name + '"],'
This currently looks like this:
["Reach First"],
["Test Company"],
["Test Company"],
I want to omit that last comma and make it look like this:
["Reach First"],
["Test Company"],
["Test Company"]
Notice the comma missing at the very end. I am not sure hot to omit that last comma in a loop.
You can include the index in the iteration, and check for it not being the last one. That would probably look something like this:
each test, index in profile
= '["' + test.profile_name + '"]' + (index === profile.length - 1 ? '' : ',')
Related
i'm trying to make a migration using Laravel. The idea is to get the values of columns 'code' and 'id' in different tables, and merge them into a column 'name' with '-' separators.
So in part it is a SQL problem.
Currently i'm trying something like this.
First migration to create the new column 'name'
public function up()
{
Schema::table('work_order', function (Blueprint $table) {
$table->string('name')->nullable();
});
}
(works just fine)
And second migration to populate the new column with values
class AlterColumnNameWorkOrder extends Migration
{
public function up()
{
$company_code = DB::statement("SELECT code FROM company");
$campaign_code = DB::statement("SELECT code FROM campaign");
$work_order_number = DB::statement("SELECT id FROM work_order");
DB::statement("UPDATE work_order SET name = $company_code + '-' + $campaign_code + '-' + $work_order_number");
}
and i'm getting this error
SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "-"
LINE 1: UPDATE work_order SET name = 1 + '-' + 1 + '-' + 1
I'm thinking that i'm not getting the values but the index instead.
UPDATE: I solved the problem by replacing + for ||. But now in column 'name' all my values are "1-1-1". The values are not being represented. What am i missing?
UPDATE2: I noticed that i was defining an unnecessary variable $work_order_number. As it belongs to the same table to be updated. So I removed it and put the field "id" directly in the statement.
DB::statement("UPDATE work_order SET name = $company_code || '-' || $campaign_code || '-' || id");
Now the third value is shown correctly. And this reduces my problem to getting values from another table into an column.
First of all, you shouldn't use migrations to populate the database. Read the documentation about seeds.
Also, i think your select query is wrong. Isn't you missing a where clause?
Run php artisan migrate to migrate your database, then create a new seed, and add this to the run method:
$company_code = DB::table('company')->select('code')->where('id', some_id)->first()->code;
$campaing_code = DB::table('campaing')->select('code')->where('id', some_id)->first()->code;
$work_order_number = DB::table('work_number')->select('id')->where('id', some_id)->first()->id;
$sql = "UPDATE work_order SET name = concat('{$company_code}', '-', '{$campaign_code}', '-', '{$work_order_number}');
DB::statement(DB::raw($sql));
Now, just run php artisan db:seed --class=YourSeedClassName and it should work.
lets say have column with values
"Html", "Java", "JQuery", "Sqlite"
and if user enters
"Java is my favorite programming language"
then result should be
1 row selected with value "Java"
because entered sentence starts with "Java"
but if user enters
"My application database is in Sqlite"
then query should return empty.
0 row selected
because entered sentence does not start with "Sqlite".
I am trying following query:
SELECT * FROM demo where 'Java' like demo.name; // work
but if enter long text then it fails
SELECT * FROM demo where 'Java is my favorite programming language' like demo.name; // Fails
I think you want something like this :
SELECT * FROM demo WHERE yourText LIKE demo.name || '%' ;
Just put the column name on the left side of the like operator, and concatenate a wildcard at the right side of your string keywork:
select * from demo where name like 'Java%' ; -- or like 'Sqlite%' etc
You can make it more generic like:
select * from demo where name like ? || %' ;
where ? is a bind parameter that represents the value provided by the user (you may also concatenate with the wildcard in your application before passing the parameter).
Concatenate your column with a wildcard:
SELECT * FROM demo where 'Java is my favorite programming language' like demo.name || '%';
You can make your query like this
Select * from SearchPojo where name GLOB '*' || :namestring|| '*'"
Not sure if I got the problem but ...
You can query these values (Html, Java, JQuery, Sqlite) put them in a collection, like List, then grab user's input and check it:
List<String> values ... // Html, Java, JQuery, Sqlite
for (String value : values) {
if (userInput.contains(value) {
// print
System.out.println("1 row selected with value \"Java\");
}
}
It supports cases where user type more then one option.
I'm doing a school project and need to code a query to filter a dataset to certain variables. All my SQL works fine, except I can't get the LIKE statement to work with %-signs. I believe my syntax is wrong. Can anybody please tell me what I'm doing wrong. Thanks
The code:
qryMovie.SQL.Clear;
qryMovie.SQL.Add('SELECT * FROM Movies');
qryMovie.SQL.Add('WHERE Genre = ' + QuotedStr(genre));
qryMovie.SQL.Add('AND Price BETWEEN ' + minPrice + ' AND ' + maxPrice);
qryMovie.SQL.Add('AND Title LIKE %' + title + '%');
qryMovie.Open;
Error produced:
'Syntax error in query expression 'Genre = 'Action/Adventure'
AND Price BETWEEN 0 AND 200
AND Title LIKE %Star Wars%''
LIKE %Star Wars%
but you need
LIKE '%Star Wars%'
You need to quote % with ':
qryMovie.SQL.Add(' AND Title LIKE ''%' + title + '%''');
Anyway you should use binded parameters instead of concatenating SQL string. It is error-prone and could lead to SQL Injection attacks.
I have data in a column 'fruits' like this:
apple/green/
apple/red/
apple/brown
what i need to do is remove the '/' character at the end in rows 1 and 2. No change needs to be done in the 3rd row. My output should be
apple/green
apple/red
apple/brown
I have tried doing this..
b = foreach a generate (fruits), ENDSWITH(fruits,'/')==true ? REPLACE(SUBSTRING(fruits, (INT)LAST_INDEX_OF(fruits, '/'), (INT)SIZE(fruits)),'');
Basically I am trying to replace the '/' symbol with space ' ' in the ending.
But i am getting error with this command. Can anyone please help?
Bincond operator has this synthax:
(condition ? value_if_true : value_if_false)
Therefore, else part is mondatory and write like this :
b = foreach a generate (fruits), ENDSWITH(fruits,'/')? REPLACE(SUBSTRING(fruits, (INT)LAST_INDEX_OF(fruits, '/'), (INT)SIZE(fruits)),'') :fruits ;
Or more easier, think about using REPLACE function :
b = foreach a generate REPLACE(fruits,'[/]$','');
I have a column as like given below
EB.Liter+'L'+EB.CC+'cc'+'-ci'+EB.BlockType+EB.Cylinders+
(
EB.EngBoreIn+'x'+
EB.EngStrokeIn+';'+
EB.EngBoreMetric+'x'+
EB.EngStrokeMetric
) as EngineBase
This pull data something like this
1.3 L 1339 cc L 4 (2.87 x3.15 ;73.0 x80.0 )
but i am trying to get something like
1.3L 1339cc -ci L4 (2.87x3.15; 73.0x80.0)
may anyone please suggest if possible
i have tried something like this.
LTRIM(RTRIM(EB.Liter))+'L'+LTRIM(RTRIM(EB.CC))+'cc'+'-
ci'+LTRIM(RTRIM(EB.BlockType))+LTRIM(RTRIM(EB.Cylinders))+
+ (
LTRIM(RTRIM(EB.EngBoreIn))+'x'+
LTRIM(RTRIM(EB.EngStrokeIn))+';'+
LTRIM(RTRIM(EB.EngBoreMetric))+'x'+
LTRIM(RTRIM(EB.EngStrokeMetric))
) as EBase
which get me result as below
1.3L1339cc-ciL42.87x3.15;73.0x80.0
but correct output is as
1.3L 1339cc -ciL4(2.87x3.15; 73.0x80.0)
modify like this
LTRIM(RTRIM(EB.Liter))+'L '+LTRIM(RTRIM(EB.CC))+'cc '+'-ci'+LTRIM(RTRIM(EB.BlockType))+LTRIM(RTRIM(EB.Cylinders))+
+ '('+
LTRIM(RTRIM(EB.EngBoreIn))+'x'+
LTRIM(RTRIM(EB.EngStrokeIn))+'; '+
LTRIM(RTRIM(EB.EngBoreMetric))+'x'+
LTRIM(RTRIM(EB.EngStrokeMetric))
+')' as EBase
you can use trim functionality to remove leading and trailing spaces. Your query would look like that then:
TRIM(EB.Liter) + 'L'+TRIM(EB.CC)+'cc'+'-ci'+TRIM(EB.BlockType)+TRIM(EB.Cylinders)+
(
TRIM(EB.EngBoreIn)+'x'+
TRIM(EB.EngStrokeIn)+';'+
TRIM(EB.EngBoreMetric)+'x'+
TRIM(EB.EngStrokeMetric)
) as EngineBase