Grails withCriteria, value between two database columns? - sql

how do I test if a value is between two of my database columns?
I need to do something like this:
between(column1, column2, 'value')
or something like this:
or{
and {
ge("hora_inicio", evento.hora_inicio)
le("hora_fim", evento.hora_inicio)
}
and {
ge("hora_inicio", evento.hora_fim)
le("hora_fim", evento.hora_fim)
}
}
any ideias?
Thanks!

There is not an option to check that two columns are between a value. You are going to have to do two separate comparisons. You can compare one database column to another though, but it doesn't see like that's quite what you want. See documentation on this here: http://docs.grails.org/latest/ref/Domain%20Classes/createCriteria.html
From your second example, it looks like you need to find all the entries that have a start time between evento.hora_inicio and evento.hora_fim or that have a order time between evento.hora_inicio and evento.hora_fim.
It feels like this can be simplified to look for all the entries that have an hora_inicio before the evento.hora_fim and have a hora_fim after the evento.hora_inicio. That would look something like this:
Event evento = getEventFromSomewhere()
Appointment.createCriteria().list{
le 'hora_inicio', evento.hora_fim
ge 'hora_fim', evento.hora_inicio
}

Related

Phalcon use LIKE inside Query\Builder on two columns

How can I select data from two columns using Query\Builder with 'LIKE' %something% and 'OR'.
I get no data if there is no match in column title despite the use of the OR, but there must be a match inside the column subtitle.
I'm currently doing this:
$Query->andWhere(
'MyData.title LIKE :searchValue: OR MyData.subtitle LIKE :searchValue:', [
'searchValue' => '%' . $searchValue . '%',
]
);
Well it looks fine. Are you sure there is match? Maybe check what query is produced? You can easily add logs of all queries using 'db:beforeQuery' event.
Maybe you have some other conditions?

How can I write a WHERE statement that looks for a variable OR Null?

I am attempting to write a query for an Altiris report. This query is looking for machine information. The query works fine, however the problem I am running into is with my parameters. I have set up multiple parameters within Altiris to allow me to filter and search through the report for multiple fields. Then, in my query, I add those parameters into the WHERE statements.
All of the parameters were working fine, until I added Make and Model parameters. We have quite a few machines that do not have information populated into these fields. So when I add in the WHERE xxxx LIKE N'%Make%', I lose about 500 machines based on it now only looking for machines with something in that field. I tried to fix this by adding lines like the following:
Where ((xxxx LIKE N'%Make%' OR xxxx is null))
This kind of worked, in that now the report shows all machines... But if I enter "HP" into the Make parameter field and then rerun the report... it shows all HP machines like I want, but also all of the null machines as well.
How can I rewrite my where statements so that they do not exclude machines in the report, and allow me to filter by all HP machines, without showing null values as well?
Hope this made sense, and thank you
In this snip of code, the last two lines make me lose about 500 machines in my total machine count of the report. It is omitting all machines that have null values.
WHERE
(dbo.OS_Version.[OS Name] LIKE N'%OSName%') AND
(dbo.OS_Version.[OS Version] LIKE N'%Build%') AND
(dbo.OS_Version.Name LIKE N'%Name%') AND
(dbo.Inv_AeX_AC_Identification.[Hardware Serial Number] LIKE N'%Serial%') AND
(dbo.vHWComputerSystem.Manufacturer LIKE N'%Make%') AND
(dbo.vHWComputerSystem.Model LIKE N'%Model%')
This is how I tried to fix it, and now I get all 20,000 machines. But my make/model fields report on null fields as well.
WHERE
(dbo.OS_Version.[OS Name] LIKE N'%OSName%') AND
(dbo.OS_Version.[OS Version] LIKE N'%Build%') AND
(dbo.OS_Version.Name LIKE N'%Name%') AND
(dbo.Inv_AeX_AC_Identification.[Hardware Serial Number] LIKE N'%Serial%') AND
((dbo.vHWComputerSystem.Manufacturer LIKE N'%Make%') OR (dbo.vHWComputerSystem.Manufacturer is null)) AND
((dbo.vHWComputerSystem.Model LIKE N'%Model%') OR (dbo.vHWComputerSystem.Model is null))
I'm guess that if you don't enter a value for a parameter, it's coming through as an empty string, and of course, every varchar is LIKE '%%'.
I'm not sure what RDBMS this is, but if the ISNULL function is available, try this:
where ((ISNULL(xxxx,'') LIKE N'%Make%')
This replaces nulls with the empty string before doing the LIKE comparison.
I think you want something like this:
(cs.Manufacturer LIKE N'%#Make%' OR #Make = '') AND
(cs.Model LIKE N'%#Model%' OR #Model = '')
I am using = '' rather than IS NULL because you are clearly not passing in the parameters as NULL values (the LIKE wouldn't work).
This does not provide a method for filtering to get only the NULL values, because you are using the "special value" for the parameter to mean "don't apply a filter here".
Note that cs is intended as a table alias. I also strongly recommend that you use table aliases so your queries are easier to write and to read.
I think you're looking for something like WHERE ISNULL(Model, '') LIKE '%Model%'. However, you should replace '%Model%' with a variable. The above example would literally match the word 'Model'
DECLARE #Model NVARCHAR(100) = 'T-800'
...
WHERE ISNULL(Model, '') LIKE '%' + #Model + '%'
^ This would not include rows with NULL Model values

Selecting all info from nodes with the same name

I'm a total newbie when it comes to xml stuff.
So far I have this piece of xml that I want to extract info from, but all the node names are the same (so it just grabs one of them, unless stated otherwise).
It looks something like this:
<DocumentElement>
<Screening>
<ScreeningID>2</ScreeningID>
<ScreeningDate>2011-09-13T00:00:00-04:00</ScreeningDate>
<ScreeningResult>1</ScreeningResult>
<ScreeningResultText>Negative</ScreeningResultText>
<TextResult>0</TextResult>
<TextResultText>Not Tested</TextResultText>
<PageNumber>0</PageNumber>
<AddedDate>2015-05-03T16:06:41.71774-04:00</AddedDate>
<UpdateDate>2015-05-03T16:06:41.71774-04:00</UpdateDate>
</Screening>
<Screening>
<ScreeningID>3</ScreeningID>
<ScreeningDate>2011-09-13T00:00:00-04:00</ScreeningDate>
<ScreeningResult>1</ScreeningResult>
<ScreeningResultText>Negative</ScreeningResultText>
<TextResult>1</TextResult>
<TextResultText>Negative</TextResultText>
<PageNumber>9</PageNumber>
<AddedDate>2015-05-03T16:25:21.2904988-04:00</AddedDate>
<UpdateDate>2015-05-03T16:25:21.2904988-04:00</UpdateDate>
</Screening>
And I'm currently using this kind of snippet to extract info from the TextResult area
Select
answer.value('(/DocumentElement/Screening/TextResult)[1]','int')
From
Answers
However, that only grabs the first bit of info, I know that if I write something like this, it'll get me the second bit of info but on another column: answer.value('(/DocumentElement/Screening[2]/textResult)[1]','int')
I have two issues with this: 1. There isn't necessarily going to be only 2 nodes with the same name - it could go on infinitely. And 2. I would like all the info to be gathered into only one column.
Any help would be appreciated!
You can try this way :
SELECT
X.value('.','int') as 'TextResult'
FROM Answers as 'a'
CROSS APPLY a.answer.nodes('/DocumentElement/Screening/TextResult') as answers(X)
SQL Fiddle
I understand your meaning is: get all TextResult in your xml document. If so, you can try this:
string xml = #"<DocumentElement>
<Screening>
<ScreeningID>2</ScreeningID>
<ScreeningDate>2011-09-13T00:00:00-04:00</ScreeningDate>
<ScreeningResult>1</ScreeningResult>
<ScreeningResultText>Negative</ScreeningResultText>
<TextResult>0</TextResult>
<TextResultText>Not Tested</TextResultText>
<PageNumber>0</PageNumber>
<AddedDate>2015-05-03T16:06:41.71774-04:00</AddedDate>
<UpdateDate>2015-05-03T16:06:41.71774-04:00</UpdateDate>
</Screening>
<Screening>
<ScreeningID>3</ScreeningID>
<ScreeningDate>2011-09-13T00:00:00-04:00</ScreeningDate>
<ScreeningResult>1</ScreeningResult>
<ScreeningResultText>Negative</ScreeningResultText>
<TextResult>1</TextResult>
<TextResultText>Negative</TextResultText>
<PageNumber>9</PageNumber>
<AddedDate>2015-05-03T16:25:21.2904988-04:00</AddedDate>
<UpdateDate>2015-05-03T16:25:21.2904988-04:00</UpdateDate>
</Screening>
</DocumentElement>";
XElement xmlTree = XElement.Parse(xml);
IEnumerable<XElement> textResultList = from c in xmlTree.Descendants("TextResult")
select c;
foreach (var item in textResultList)
{
Console.WriteLine(item.Value);
}
Console.Read();
I hope this help

PostgreSQL -- Clean way of extracting protobuf field via SQL

I have been trying to figure out how to cleanly extract some_value from something that looks like this:
SELECT protobuff_text FROM table_name;
>>
resource_id {
high_part: 10310138280989442894
low_part: 12186277462739912197
}
...
}
known_field_name: some_value
mask {
points {
...
It should be extracted without assuming anything about what happens before or after it besides the newline. This is easy in MySQL... but I have been having a hard time figuring it out in postgres.
Any PostgreSQL experts out there who can help me with this?
P.S. The string that is inserted into the database is created via com.google.protobuf.TextFormat.printToString(Message m).
Try something like:
SELECT regexp_matches(protobuff_text,'resource_id\s*\{\s*high\_part\:\s*([0-9]*)\s*low\_part\:\s*([0-9]*)\s*\}')
FROM table_name;
This regex will get you the numeric values, you shown in the example.
For the known_field_name try something like:
SELECT regexp_matches(protobuff_text,'known_field_name\:\s*(\w*)')
FROM table_name;
Details here.

Rails3 - Is there a way to do NOTLIKE?

I previously asked a question regarding pulling specific items out of a database if they contained a specific word in their string, someone kindly offered the following which did just the job:
def SomeModel < ActiveRecord::Base
scope :contains_city,
lambda { |city| where("some_models.address LIKE ?","%"+city+"%" ) }
end
However, I have some instances where I would like to do the opposite, i.e. pull out all the items which do not have the specified word in their string. Is there a way to do a NOT LIKE function? I have prevously seen people use '!=' for a NOT EQUALS, but have had no success along these lines for the LIKE function. Is there an equivalent or is it best to iterate through the database putting items in 2 separate databases based on whether they satisfy the LIKE condition?
You could try NOT LIKE in your query; MySQL supports this.
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html