How to set a substring from a sql statement through Groovy setter - sql

I am reading a sql table through a Sql statement, meanwhile i am also using a substring. Since the Substring is like virtual i am not writing in the database. How am i able to set this read substring using a setter in groovy using sql.eachrow.
I have tried setFinishingTime(row.FinishingTime)
This is in my groovy code.
sql.eachRow('select * from [Database].[dbo].[table] order by FinishingTime')
{ row ->
---
machine.setFinishingTime(row.FinishingTime)
}
In sql query i have a code.
substring([FinishingTime], charindex(' ', [FinishingTime]) + 1, len([FinishingTime])).
I want to set in FinishingTime the substring of the column finishingtime. How can i achieve that, It works in sql management studio but how do i use the same in groovy through sql.eachRow.

Related

Why does my online SQL editor not understand group_concat_max_len?

I don't have extensive experience in sql but I know basic.
I use online sql executor to receive the json file which will be prodiced from this sql query.
SET group_concat_max_len=18446744073709547520;
SELECT
CONCAT(
'[',
GROUP_CONCAT(
JSON_OBJECT(
'id',messages.message_id,
'mailing_list_url',messages.mailing_list_url,
'type_of_recipient',messages_people.type_of_recipient,
'email_address',messages_people.email_address,
'message_body',messages.message_body,
'is_response_of',messages.is_response_of
) SEPARATOR ',\r'),
']') AS list
FROM messages
LEFT JOIN messages_people
ON messages.message_id = messages_people.message_id
AND messages.mailing_list_url = messages_people.mailing_list_url
WHERE email_address IN ('dr#gorsvet.kz', 'mathews#uk2.net', 'd.jentsch#fu-berlin.de', 'atul.soman#microfocus.com', 'kkrugler_lists#transpac.com', 'hcorg#minions.org.pl', 'bruno.ronchetti#mac.com', 'christophe.thiebaud#sap.com', 'kc.baltz#copart.com', 'havanki4j#gmail.com', 'joseph.obernberger#ngc.com', 'goran#roseen.se', 'hboutemy#apache.org', 'johann#gyger.name', 'fredrik#jonson.org', 'jose-marcio.martins#mines-paristech.fr', 'dmytro.kostiuchenko#gmail.com', 'jochen.wiedmann#softwareag.com', 'guyskk#qq.com', 'alejandroscandroli#gmail.com', '1983-01-06#gmx.net', 'jstrayer#proofpoint.com', 'daniel#degu.cl', 'hontvari#flyordie.com', 'david#andl.org', 'gholmes#pinsightmedia.com', 'dennis.geurts#luminis.eu', 'carlspring#gmail.com', 'jozef.koval#protonmail.ch', 'gabriele.ctn#gmail.com', 'ashish.disawal#evivehealth.com', 'aarslan2#anadolu.edu.tr', 'kshukla#yahoo-inc.com', 'alex.huang#gmail.com', 'markus#openindex.io', 'marohn#sipgate.de', 'ea#apache.org', 'krzysztof.szalast#gmail.com', 'kfoskey#tpg.com.au', 'krauss#in.tum.de', 'kenhans#hotmail.co.uk', 'jokin.c#odeian.com', 'eitch#eitchnet.ch', 'm.kristian#web.de', 'bob.sandiford#sirsidynix.com', 'asandstrom2#eastlink.ca', 'kelly#apache.org', 'antony.blakey#linkuistics.com.au', 'ashawki#hotmail.com', 'h.klocker-mark#tsn.at');
However from the first line I receive error.
As instructions are not friendly for me please can you help me how could I take the final file?
I had no clue that there are such things as online sql executors. Basically SQL injections as a service, nice.
Anyways, the command:
SET group_concat_max_len=18446744073709547520;
is not part of the official SQL standard and specific to the database server you are using (here MYSQL). From the manual for group_concat_max_len:
The maximum permitted result length in bytes for the GROUP_CONCAT() function. The default is 1024.
For this very reason a "online SQL executor" (wow, I am still puzzled) won't be able to run this. Anyways if you need this, you probably won't get around setting up a MYSQL server yourself. Or you could just use a container.

How to read-in a FOR XML call from a Kamiak Query component in Delphi D6?

Performing an SQL FOR XML RAW statement call with a Kamiak ADO Query component in Delphi6. How to read the returned XML back out of that query component in your code?
Initial problems:
1) There is no "field name" to refer to in the Kamiak components "FieldByName()" method.
2) Attempts to reference the query results as Query.Fields.Fields[0] returns binary that the XMLPartner component can't read-in via their LoadFromMemory( ) method call.
The following is a beautifully simple tweak to the FOR XML RAW statement that made it exceptionally easy to read back out the XML result of the SQL call back out of the kamiak ADO Query component in code.
Essentially take the existing FOR XML RAW select statement, and wrap it up inside the following:
SELECT CAST((< your FOR XML RAW select stmt goes here >) AS VARCHAR(MAX)) AS XmlData
Then... within your Delphi code you can just refer to the returned XML as follows:
sXML := MyKamiakQryObject.FieldByName('XmlData').AsString();
Before doing this I was having a bear of a time with trying to extract the XML content of that query.
So I wanted to make sure to share this for the next guy.

Using Regex in a SQL Select

I have a SQL column that contains JSON payloads. I'm writing a SQL query that will extract part of that column w/ regex.
In the following field:
{
"myContent":{
"fieldG":null,
"valuable":"this is the text",
"fieldH":[
"a4a6ba1c2e0e4a9c89dac46f1092b505"
],
"fieldI":"1"
},
"fieldJ":"1441375349399"
}
I want to scrape the string immediately after "valuable":
I was experimenting w/ something like this:
\"valuable\":\".*\"
... but that doesn't help. For starters, I only want what's AFTER valuable and colon. Also, that particular regex matches from "valuable:" all the way through the entire end of the line. That's way more text than I need.
In SQL Server 2016 CTP3 you could use JSON_VALUE function, e.g.:
SELECT JSON_VALUE(column, '$.mycontent.valuable').
See http://blogs.msdn.com/b/jocapc/archive/2015/05/16/json-support-in-sql-server-2016.aspx
If you cannot wait for Sql Server 2016, you can use some JSON CLR library e.g. http://www.codeproject.com/Articles/1000953/JSON-for-SQL-Server-Part
Otherwise yo would need to use PATINDEX and substring.

RegEx in SQL - How to Change the values of a column in SQL Server Database

I am trying to change the values of a column in my SQL Server Database.
Table: crm.activity
Column: Content
The values look like this:
value 1:
{\rtf1\ansi\ansicpg1252\deff0\deflang2055{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\f0\fs17 Appointment made\par
}
value 2:
{\rtf1\ansi\ansicpg1252\deff0\deflang2055{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\f0\fs17 Ask for offer\par
}
What I need is just the bold text.
Is it possible like using RegEx to go through all the columns and change the value???
To my knowledge, you can't do regex replaces in MySQL out of the box. However, if there's only one bold string per column, it should be easy enough to do the trick with string functions like LOCATE and SUBSTR.
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html
Here you go. This is for MySQL:
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('{\rtf1\ansi\ansicpg1252\deff0\deflang2055{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}} \viewkind4\uc1\pard\f0\fs17 **Appointment made**\par}', '**', -2), '**', 1);
# => Appointment made
Or to update (try it first and make sure you have a backup):
UPDATE mytable SET mycolumn = SUBSTRING_INDEX(SUBSTRING_INDEX(mycolumn, '**', -2), '**', 1);
If its SQL Server, do a bit of googling (mssql update replace regex) on how to add a regex replace method to SQL server. The regex you will need is: .+\\fs\d+\s(.+)\\par\s?}, replace this with $1. As a fallback solution (not sure how much data you have in that table) you could write a small script that selects all records, replaces the string, and updates the database.

Use of LIKE clause in sql prepared statement, spring, SimpleJDBCTemplate

I have the following sql prepared statement:
SELECT * FROM video WHERE video_name LIKE ?
Im using spring and jdbc.
i have a method, where term is a searchterm, sjt is a SimpleJdbcTemplate, VideoMapper is a RowMapper and searchForTermQuery is the string from above
...
return sjt.query(searchForTermQuery, new VideoMapper(), term);
My table has 2 videos that match the term.
However when I run the query none is found. I get an empty List.
I tried playing with % around the question mark, but it only gave badGrammarExceptions.
You need to put the % around the value itself, not around the placeholder (the question mark).
So:
return sjt.query(searchForTermQuery, new VideoMapper(), "%" + term + "%");