I have to select all fields which contain a specific text and ended with ", ', ) or blank space characters or this text is placed in the end of string.
So, I need something like that:
select *
from MyTable
Where Column1 like '%' + #text + '["'') ]%'
This query works fine until text is not placed in the end of Column1.
You could use
Where Column1 + '"' like '%' + #text + '["'') ]%'
Why not try OR?
Where Column1 like '%' + #text + '["'') ]%'
or
Column1 like '%' + #text + '["'') ]'
Or do I misunderstand the requirement? Your question is a little hard to follow.
SELECT *
FROM MyTable
WHERE Column1 LIKE '%' + #text + '%["'') ]'
OR Column1 LIKE '%' + #text
This should work better and faster:
SELECT * FROM MyTable
WHERE
( (RIGHT(Column1,1) IN (' ', '"', '''', ')'))
AND
(SUBSTRING(Column1, LENGTH(#text)-1, LENGTH(#text))=#text)
)
OR ( RIGHT(Column1, LENGTH(#text))=#text )
Related
Hi I have the following dyanmic stored procedure which returns a normal result set at the end however crystal reports is not returning any fields along with it might anyone have an idea what is wrong.
When I run the proc in studio management it works fine. This is a report using the sap version of crystal in case that makes a difference.
I would normally output the results to excel and it runs correctly but for crystal its not allowing me to select any fields in the field explorer.
ALTER PROCEDURE [dbo].[ReportByDates]
#DateFrom date NULL,
#DateTo date NULL
AS
SET NOCOUNT ON;
DECLARE #DynamicColumnsDivision AS VARCHAR(max)
DECLARE #DynamicColumnsDivision2 AS VARCHAR(max)
DECLARE #DynamicColumnsDivisionTotal AS VARCHAR(max)
SELECT #DynamicColumnsDivision = COALESCE(#DynamicColumnsDivision + ', ', '')
+ 'ISNULL(' +Quotename(fieldname) + ',0) as ' + Quotename(fieldname)
FROM (
SELECT
DISTINCT
PrcCode + ' - ' + cc.PrcName as fieldname
FROM OPRC cc
WHERE Active = 'Y' AND CCTypeCode = 'Division'
) AS FieldList
SELECT #DynamicColumnsDivision2 = COALESCE(#DynamicColumnsDivision2 + ', ', '')
+ Quotename(fieldname)
FROM (
SELECT
DISTINCT
PrcCode + ' - ' + cc.PrcName as fieldname
FROM OPRC cc
WHERE Active = 'Y' AND CCTypeCode = 'Division'
) AS FieldList
SELECT #DynamicColumnsDivisionTotal = COALESCE(#DynamicColumnsDivisionTotal + '+', '')
+ 'ISNULL(' +Quotename(fieldname) + ',0)'
FROM (
SELECT
DISTINCT
PrcCode + ' - ' + cc.PrcName as fieldname
FROM OPRC cc
WHERE Active = 'Y' AND CCTypeCode = 'Division'
) AS FieldList
DECLARE #DynamicColumnsGQO AS VARCHAR(max)
DECLARE #DynamicColumnsGQO2 AS VARCHAR(max)
DECLARE #DynamicColumnsGQOTotal as VARCHAR(max)
SELECT #DynamicColumnsGQO = COALESCE(#DynamicColumnsGQO + ', ', '')
+ 'ISNULL(' +Quotename(fieldname) + ',0) as ' + Quotename(fieldname)
FROM (
SELECT
DISTINCT
PrcCode + ' - ' + cc.PrcName as fieldname
FROM OPRC cc
WHERE Active = 'Y' AND CCTypeCode = 'GQO'
) AS FieldList
SELECT #DynamicColumnsGQO2 = COALESCE(#DynamicColumnsGQO2 + ', ', '')
+ Quotename(fieldname)
FROM (
SELECT
DISTINCT
PrcCode + ' - ' + cc.PrcName as fieldname
FROM OPRC cc
WHERE Active = 'Y' AND CCTypeCode = 'GQO'
) AS FieldList
SELECT #DynamicColumnsGQOTotal = COALESCE(#DynamicColumnsGQOTotal + '+', '')
+ 'ISNULL(' +Quotename(fieldname) + ',0)'
FROM (
SELECT
DISTINCT
PrcCode + ' - ' + cc.PrcName as fieldname
FROM OPRC cc
WHERE Active = 'Y' AND CCTypeCode = 'GQO'
) AS FieldList
DECLARE #Query NVARCHAR(MAX)
SET #Query =
'SELECT Account, AcctName, ' + #DynamicColumnsDivision + ', ' + #DynamicColumnsDivisionTotal + ' as DivisionTotal , ' + #DynamicColumnsGQO + ', ' + #DynamicColumnsGQOTotal + ' as GQOToatl
FROM
(
SELECT
line.Account,
nom.AcctName,
line.ProfitCode + '' - '' + cc.PrcName as fieldname,
CASE WHEN line.Debit <> 0 THEN line.Debit *-1 WHEN line.Credit <> 0 THEN line.Credit ELSE 0 END as DispValue
FROM OJDT head
LEFT JOIN JDT1 line on head.TransId = line.TransId
LEFT JOIN OACT nom on line.Account = nom.AcctCode
LEFT JOIN OPRC cc ON line.ProfitCode = cc.PrcCode
WHERE head.RefDate BETWEEN ''' + CONVERT(VARCHAR(10),#DateFrom, 101) + ''' AND ''' + CONVERT(VARCHAR(10),#DateTo, 101) + '''
) AS SourceTable PIVOT
(
SUM(DispValue)
FOR fieldname IN (' + #DynamicColumnsDivision2 + ', '+#DynamicColumnsGQO2 + ')
) AS PivotTable;
'
--SELECT #Query
execute(#Query)
Edit 2
Weird I copied the entire proc and declared the date from and date to elements and it worked fine. So why when it being called from a stored proc is it working ok.
If the stored procedure does not return any data Crystal Reports won't be able to show you the columns. Make sure it returns the data.
Anyways, you can always try to remove the stored procedure and add it again in the database assistant. Crystal Reports is not very good updating the fields when you use a stored procedure, or at least it wasn't in older versions.
I'm trying to set the columnName, databaseName, schemaName etc. dynamically based on a temporary table but cant seem to make it work. i've tried below?
Create Table #test(databaseName varchar(128), schemaName varchar(128), columnName varchar(128), datatypeName varchar(128));
INSERT INTO #test ('testDatabase', 'testSchema', 'testTable', 'priceColumn');
SELECT
Case
WHEN DataType = 'int'
THEN SELECT MAX(ColumnName) FROM Concat(databaseName, '.', schemaName, '.', tableName)
ELSE 0
end
FROM #test;
DROP TABLE #test;
the expected result is that this below subquery take each line in the row in #test table and then query based on these values so it return the maxPrice from that table
SELECT MAX(ColumnName) FROM Concat(databaseName, '.', schemaName, '.', tableName)
Are you trying to aggregate on a calculated field while preserving the query flow?
You can try something like this.
SELECT
MAX(FullName)
FROM
(
SELECT
FullName = databaseName+ '.' + schemaName + '.' + columnName,
*
FROM
#test
) AS A
WHERE
DataTypeName='int'
Another example
SELECT
MAX(FullNameInt),
MAX(FullNameOther)
FROM
(
SELECT
FullNameInt = CASE WHEN DataTypeName='int' THEN databaseName+ '.' + schemaName + '.' + columnName ELSE NULL END,
FullNameOther = CASE WHEN DataTypeName<>'int' THEN databaseName+ '.' + schemaName + '.' + columnName ELSE NULL END,
*
FROM
#test
) AS A
I have successfully constructed the output that I have been looking for from using dynamic SQL to create a pivot table with dynamically created column names.
My code is:
IF OBJECT_ID('tempdb..#TempDB') IS NOT NULL
DROP TABLE #TempDB
SELECT CASEID, FORMNAME, NAME, VALUE INTO #TempDB FROM dbo.EFORM WHERE FORMNAME='IncidentReporting'
IF OBJECT_ID('tempdb..#TempDB1') IS NOT NULL
DROP TABLE #TempDB1
SELECT DISTINCT Name INTO #TempDB1 FROM #TempDB
DECLARE #columns varchar(max)
DECLARE #query varchar(max)
SELECT #columns = COALESCE(#columns + ',[' + cast([Name] as varchar(100)) + ']',
'[' + cast([Name] as varchar(100))+ ']')
FROM #TempDB1
SET #query = 'SELECT * FROM #TempDB AS PivotData '
SET #query = #query +
'PIVOT (MAX(VALUE) FOR [NAME] IN (' + #columns + ')) AS p'
EXEC (#query)
This successfully gives me results like:
CASEID FORMNAME Column1 Column2 Column3
501000000621 IncidentReporting Value1 Valuea Valuev
501000000622 IncidentReporting Value2 Valueb Valuew
601000000126 IncidentReporting Value3 Valuec Valuex
601000000127 IncidentReporting Value4 Valued Valuey
601000000128 IncidentReporting Value5 Valuee Valuez
These results, outputed from the #query variable, are in exactly the format that I want a table of these results to be in.
Can anyone tell me how to get the results that are in the #query variable into a standard SQL table?
I have tried doing a statement like this, but I get the message "Incorrect syntax near ' + #columns + '":
SELECT *
INTO #TempDB4
FROM (SELECT * FROM #TempDB AS PivotData
PIVOT (MAX(VALUE) FOR [NAME] IN (' + #columns + ')) AS p)
Many thanks in advance.
In your existing code, add your into to this line:
SET #query = 'SELECT * FROM #TempDB AS PivotData '
so that you get:
SET #query = 'SELECT * INTO #TempDB4 FROM #TempDB AS PivotData '
Or add insert in the same manner.
To get your unsuccessful query to work as you expect, you'd have to turn that into dynamic SQL, much like your successful query, and call it using exec or sp_executesql
I want to select some data in sql express by IN clause and LIKE but no row returned.
my code is here :
SELECT PointId, PointTitleId, PointTitleName, PointTitleContentText
FROM PointsTitlesData
WHERE (PointTitleContentText LIKE '%' + #SearchingClause + '%')
AND (PointId IN ('2','3'))
above code don't return any data but when i use only LIKE code return data.
SELECT PointId, PointTitleId, PointTitleName, PointTitleContentText
FROM PointsTitlesData
WHERE (PointTitleContentText LIKE '%' + #SearchingClause + '%')
how can i use both IN and LIKE?
Thanks.
I tried to recreate your scenario with the script below
Declare #SearchingClause nvarchar(20);
Create table PointsTitlesData (Pointid int, PointTitleContentText nvarchar(20));
insert into PointsTitlesData (Pointid, PointTitleContentText)
values ( 1, 'aaamypoint1aaa'), (2, 'bbbmypoint2bbb'),(3,'cccmypoint3ccc');
Set #SearchingClause = 'mypoint2';
SELECT PointId, PointTitleContentText
FROM PointsTitlesData
WHERE (PointTitleContentText LIKE '%' + #SearchingClause + '%')
AND (PointId IN ('2','3'))
-- above code don't return any data but when i use only LIKE code return data.
SELECT PointId, PointTitleContentText
FROM PointsTitlesData
WHERE (PointTitleContentText LIKE '%' + #SearchingClause + '%')
I got a result for both queries so it looks like you have a data issue.
Have you tried
SELECT PointId from PointsTitlesData where ((PointId = 2) or (PointId = 3))
to check the data is there.
Regards
Jude
probably drop the quotes:
'2','3' should be 2,3
I have a small problem and I know a few solutions for it, but I don't know the best way (or less dirty spaghetti way) to go.
I have a string variable, and I need to use it in a like expression.
So:
declare #a varchar(100) = 'my string to use as a join from'
select *
from table
where
column like '%' + #a + '%'
But I don't want any rows from table that contains the #a variable, I want any rows from table that are contained in the #a variable, so:
select *
from table
where
#a like '%' + column + '%'
Result:
'my string'
'as a join from'
But now I need to remove the matched rows from the #a variable. How can I do that?
(edit)
expected result:
#a = ' to use '
'my string'
'as a join from'
You can alter the value of #a with each matched row in the select:
select #a = replace(#a, MyColumn, '')
from MyTable
where #a like '%' + MyColumn + '%'
Demo: http://www.sqlfiddle.com/#!3/187d6/5 (result: #a = ' to use ')
If you want to get a result set as well, use a temp table to store matching rows:
select MyColumn
into #Temp
from MyTable
where #a like '%' + MyColumn + '%'
-- #a = ' to use '
select #a = replace(#a, MyColumn, '') from #Temp
-- returns result set of words that matched
select * from #Temp
drop table #Temp
Demo: http://www.sqlfiddle.com/#!3/187d6/13