If Condition In Where Clause
Mar 28, 2014I given my requirement below:
select * from table where (here if account_no = '123456' then name <> 'KING')
I given my requirement below:
select * from table where (here if account_no = '123456' then name <> 'KING')
I write a query to get some data as the following. but i need when a user check specified condition a query parameter change to specified condition :
create proc proc_ReservationDetails
(
@status nvarchar(50) = null
)
as
begin
select reservationId, reservationStatus, reservationDesc
[Code] .....
I want to add an exception to the WHERE clause below.
Something like:
IF @IDHuis = 'WH' THEN
add the extra condtion:
AND D.AfdelingZPT <> 'A01'
-- HRS VAST
INSERT INTO @T (JAAR, WEEK, HrsVast)
[Code] .....
Hi ALL,
Well, i have one view:
create view vwdate
as
select custno, custname, city, date
from salestab
where date = ????
well, this question mark...i have to fill..
like i want if month is june then i want for whole may month results of custno, custname and city..
if month is july then i want results for whole june month..
means for previous month results i want whatever the date is for that particular month..
Example:
if date='06/15/08'
then results will be
custno custname city date
---------------------------------
1 AAAA NJ 05/01/08
2 SDS CT 05/04/08
........................ 05/18/08
.......................... 05/30/08
if date='06/30/08' OR '06/1/08' then also results will be same..
so i want for previous month results..
so can anyone help me to get the correct condition in where clause?
Thanks ALL..
tblScore contains score for each problem
id problemID score
------------------------
1 1 10
2 2 30
tblSubmission contains problem submissions for each user
id user problemID accepted
-----------------------------------------------
1 UserA 1 0
2 UserA 1 0
3 UserA 1 1
4 UserA 2 1
5 UserB 1 0
6 UserB 1 1
7 UserB 2 1
For UserA :
- For problemID 1
-> submitted three times
-> rejected for first two submission and accepted on third submission.
- For problemID 2
-> submitted one time
-> accepted on first submission
For UserB :
- For problemID 1
-> submitted two times
-> rejected for first submission and accepted on second submission.
- For problemID 2
-> submitted one time
-> accepted on first submission
Now I would like to process the table and want to get the following result :
user Score
--------------------------
UserA 36 (6 + 30)
UserB 38 (8 + 30)
Explanation :
- For each rejected submission, a -2 point penalty.
- UserA have submitted probelmID 1
- > score of problemID 1 is 10.
- > first two times rejectd
- > third time accepted.
-> score = 10 - 4 = 6
- UserA have submitted problemID 2
- > score of problemID 2 is 30
- > first time accepted. No penalty will be counted
- > score = 30
so final score for UserA = 30 + 6 = 36
Similar for UserB.
I have written a merge Statement where i am facing trouble to delete the data basing on Where Clause Condition.
1) Case 1 : For example i have inserted Data from Source to Target based on Date Key Condition.Take an Instance 10 Records Inserted.
2) Case 2 : For example some changes in the records and it has been updated through the Merge Statement .
3) Case 3 : For the Same Date key based on Conditions now three records has came and need to be inserted and rest should be deleted for that Date Key.
How i need to proceed on this before 10 records are not getting deleted and new records adding for that one
My Example Code :
MERGE INTO TargetTable AS Target
USING (
SELECT DISTINCT Col1,
Col2
FROM Table1 AS cga
)
ON dad.AnchorDate = CASE
[Code] ....
Hello
I wonder if someone could suggest a way to obtain the following. Using SQL Server 2005 I want to create some stored procedures. I want to query the DB with various filter arguments and combinations of these. One way would be to create one stored procedure for each function signature. However, as the number of combinations of filter is large, if possible I'd rather have a generic input to the each stored procedure that corresponds to the entire WHERE clause' search condition.
The stereotype behavior I'm looking for is:
SELECT myField
FROM myTable
WHERE @mySearchCondition
Does any one have some good suggestion, code samples and/or links?
Kind regards
Jens Ivar
I want to change Set clause of Update Statement dynamically based on some condition.
Basically i have 2 Update statments having same FROM clause and same JOIN clause.
Only diff is SET clause and 1 Where condition.
So i am trying to combine 2 Update statements into 1 and trying to avoid visit to same table twice.
Update t
Set CASE **WHEN Isnull(td.IsPosted, 0) = 0
THEN t.AODYD = td.ODYD**
*ELSE t.DAODYD = td.ODYD*
END
From #ReportData As t
Join @CIR AS tmp On t.RowId = tmp.Max_RowId
[Code] ....
But CASE statement is not working...
Table : incident
----------------
incident_id usr_id item_id Inc_Date
10059926 191 61006 8-22-2015
10054444 222 3232 6-7-2015
Table: act_reg
--------------
act_reg_id act_type_id incident_id usr_id act_type_sc
454244 1 10059926 191 ASSIGN
471938 115 10059926 191 TRAVEL TIME
473379 40 10059926 191 FOLLOW UP
477652 115 10059926 191 TRAVEL TIME
489091 504 10059926 191 ADD_ATTCHMNTS
477653 504 10054444 222 ADD_ATTCHMNTSParameter: @attach (value=1, Label=Yes & Value=0, Label=No)
Result (While I am selecting 'Yes' in dropdown)
----------------------------------------------
incident_id usr_id item_id
10059926 191 61006
10054444 222 3232
SELECT incident.incident_id,incident.usr_id,incident.item_id
FROM incident
where exists (How i can write query here to check the act_type_sc=ADD_ATTCHMNTS is exists)
For example..
select * from mytable where MyNum = 7
If this brings back more than 1 row, I want to display a message that says,
Print 'There is more than one row returned'
Else (If only 1 row returned), I don't want to print anything.
Can I do this? Thx!
Dear friends,
I'm having a problem... maybe it's very simple, but with soo many work, right now I can't think well...
I need to filter rows in a dataflow...
I created a condition spli to that... maybe there is a better solution...
And the condition is: Datex != NULL(DT_DATE)
(Some DATE != NULL)
[Eliminar Datex NULL [17090]] Error: The expression "Datex != NULL(DT_DATE)" on "output "Case 1" (17123)" evaluated to NULL, but the "component "Eliminar Datex NULL" (17090)" requires a Boolean results. Modify the error row disposition on the output to treat this result as False (Ignore Failure) or to redirect this row to the error output (Redirect Row). The expression results must be Boolean for a Conditional Split. A NULL expression result is an error.
What is wrong??
Regards,
Pedro
I am trying to write an visibility function to have message shown based on two different IIF conditions:
If behavior is to Add a customer ( if message =NAME ALREADY EXISTS, return " NAME ALREADY EXISTS", otherwize return " NAME CREATED")If behavior is to Delete a customer (( if message =NAME DOES NOT EXIST, return "NAME DOES NOT EXIST", otherwize return "NAME SUCCESSFULLY DELETED")
I tried the following which doesn't work:
=IIF((UCase(First(Fields!Message.Value, "DataSetName")) = "NAME ALREADY EXISTS"), "WARNING: NAME ALREADY EXIST", "NAME CREATED"),
IIF((UCase(First(Fields!Message.Value, "DataSetName")) = " NAME DOES NOT EXIST"), "WARNING: NAME DOES NOT EXIST", " NAME DELETED")
I have a quite big SQL query which would be nice to be used using UNION betweern two Select and Where clauses. I noticed that if both Select clauses have Where part between UNION other is ignored. How can I prevent this?
I found a article in StackOverflow saying that if UNION has e.g. two Selects with Where conditions other one will not work. [URL] ....
I have installed SQL Server 2014 and I tried to use tricks mentioned in StackOverflow's article but couldn't succeeded.
Any example how to write two Selects with own Where clauses and those Selects are joined with UNION?
Hi
I am developing a scientific application (demographic forecasting) and have a situation where I need to update a variety of rows, say the ith, jth and kth row that meets a particular condition, say, x.
I also need to adjust rows, say mth and nth that meet condition , say y.
My current solution is laborious and has to be coded for each condition and has been set up below (If you select this entire piece of code it will create 2 databases, each with a table initialised to change the 2nd,4th,8th and 16th rows, with the first database ignoring the condition and with the second applying the change only to rows with 'type1=1' as the condition.)
This is an adequate solution, but if I want to change the second row meeting a second condition, say 'type1=2', I would need to have another WITH...SELECT...INNER JOIN...UPDATE and I'm sure this would be inefficient.
Would there possibly be a way to introduce a rank by type into the table, something like this added column which increments for each type:
ID
Int1
Type1
Ideal Rank by Type
1
1
1
1
2
1
1
2
3
2
1
3
4
3
1
4
5
5
1
5
6
8
2
1
7
13
1
6
8
21
1
7
9
34
1
8
10
55
2
2
11
89
1
9
12
144
1
10
13
233
1
11
14
377
1
12
15
610
1
13
16
987
2
3
17
1597
1
14
18
2584
1
15
19
4181
1
16
20
6765
1
17
The solution would then be a simple update based on an innerjoin reflecting the condition and rank by type...
I hope this posting is clear, albeit long.
Thanks in advance
Greg
PS The code:
USE
master
GO
CREATE DATABASE CertainRowsToChange
GO
USE CertainRowsToChange
GO
CREATE TABLE InitialisedValues
(
InitialisedValuesID int identity(1 ,1) NOT NULL PRIMARY KEY,
Int1 int NOT NULL
)
GO
CREATE PROCEDURE Initialise
AS
BEGIN
INSERT INTO InitialisedValues (Int1 )
SELECT 2
INSERT INTO InitialisedValues (Int1 )
SELECT 4
INSERT INTO InitialisedValues (Int1 )
SELECT 8
INSERT INTO InitialisedValues (Int1 )
SELECT 16
END
GO
EXEC Initialise
/*=======================================================*/
CREATE TABLE AllRows
(
AllRowsID int identity(1 ,1) NOT NULL PRIMARY KEY,
Int1 int NOT NULL
)
GO
CREATE TABLE RowsToChange
(
RowsToChangeID int identity(1 ,1) NOT NULL PRIMARY KEY,
Int1 int NOT NULL
)
GO
CREATE PROCEDURE InitialiseRowsToChange
AS
BEGIN
INSERT INTO RowsToChange (Int1 )
SELECT 2
INSERT INTO RowsToChange (Int1 )
SELECT 4
INSERT INTO RowsToChange (Int1 )
SELECT 8
INSERT INTO RowsToChange (Int1 )
SELECT 16
END
GO
EXEC InitialiseRowsToChange
GO
CREATE PROCEDURE PopulateAllRows
AS
BEGIN
INSERT INTO AllRows (Int1 )
SELECT 1
INSERT INTO AllRows (Int1 )
SELECT 1
INSERT INTO AllRows (Int1 )
SELECT 2
INSERT INTO AllRows (Int1 )
SELECT 3
INSERT INTO AllRows (Int1 )
SELECT 5
INSERT INTO AllRows (Int1 )
SELECT 8
INSERT INTO AllRows (Int1 )
SELECT 13
INSERT INTO AllRows (Int1 )
SELECT 21
INSERT INTO AllRows (Int1 )
SELECT 34
INSERT INTO AllRows (Int1 )
SELECT 55
INSERT INTO AllRows (Int1 )
SELECT 89
INSERT INTO AllRows (Int1 )
SELECT 144
INSERT INTO AllRows (Int1 )
SELECT 233
INSERT INTO AllRows (Int1 )
SELECT 377
INSERT INTO AllRows (Int1 )
SELECT 610
INSERT INTO AllRows (Int1 )
SELECT 987
INSERT INTO AllRows (Int1 )
SELECT 1597
INSERT INTO AllRows (Int1 )
SELECT 2584
INSERT INTO AllRows (Int1 )
SELECT 4181
INSERT INTO AllRows (Int1 )
SELECT 6765
END
GO
EXEC PopulateAllRows
GO
SELECT * FROM AllRows
GO
WITH Temp(OrigID)
AS
(
SELECT OrigID FROM
(SELECT Row_Number() OVER (ORDER BY AllRowsID Asc ) AS RowScore, AllRowsID AS OrigID, Int1 AS OrigValue FROM Allrows) AS FromTable
INNER JOIN
RowsToChange AS ToTable
ON FromTable.RowScore = ToTable.Int1
)
UPDATE AllRows
SET Int1=1000
FROM
Temp as InTable
JOIN Allrows as OutTable
ON Intable.OrigID = OutTable.AllRowsID
GO
SELECT * FROM AllRows
GO
USE
master
GO
CREATE DATABASE ComplexCertainRowsToChange
GO
USE ComplexCertainRowsToChange
GO
CREATE TABLE InitialisedValues
(
InitialisedValuesID int identity(1 ,1) NOT NULL PRIMARY KEY,
Int1 int NOT NULL
)
GO
CREATE PROCEDURE Initialise
AS
BEGIN
INSERT INTO InitialisedValues (Int1 )
SELECT 2
INSERT INTO InitialisedValues (Int1 )
SELECT 4
INSERT INTO InitialisedValues (Int1 )
SELECT 8
INSERT INTO InitialisedValues (Int1 )
SELECT 16
END
GO
EXEC Initialise
/*=======================================================*/
CREATE TABLE AllRows
(
AllRowsID int identity(1 ,1) NOT NULL PRIMARY KEY,
Int1 int NOT NULL,
Type1 int NOT NULL
)
GO
CREATE TABLE RowsToChange
(
RowsToChangeID int identity(1 ,1) NOT NULL PRIMARY KEY,
Int1 int NOT NULL,
Type1 int NOT NULL
)
GO
CREATE PROCEDURE InitialiseRowsToChange
AS
BEGIN
INSERT INTO RowsToChange (Int1,Type1 )
SELECT 2, 1
INSERT INTO RowsToChange (Int1,Type1 )
SELECT 4, 1
INSERT INTO RowsToChange (Int1,Type1 )
SELECT 8, 1
INSERT INTO RowsToChange (Int1,Type1 )
SELECT 16, 1
END
GO
EXEC InitialiseRowsToChange
GO
CREATE PROCEDURE PopulateAllRows
AS
BEGIN
INSERT INTO AllRows (Int1, Type1 )
SELECT 1, 1
INSERT INTO AllRows (Int1, Type1 )
SELECT 1, 1
INSERT INTO AllRows (Int1, Type1 )
SELECT 2, 1
INSERT INTO AllRows (Int1, Type1 )
SELECT 3, 1
INSERT INTO AllRows (Int1, Type1 )
SELECT 5, 1
INSERT INTO AllRows (Int1, Type1 )
SELECT 8, 2
INSERT INTO AllRows (Int1, Type1 )
SELECT 13, 1
INSERT INTO AllRows (Int1, Type1 )
SELECT 21, 1
INSERT INTO AllRows (Int1, Type1 )
SELECT 34, 1
INSERT INTO AllRows (Int1, Type1 )
SELECT 55, 2
INSERT INTO AllRows (Int1, Type1 )
SELECT 89, 1
INSERT INTO AllRows (Int1, Type1 )
SELECT 144, 1
INSERT INTO AllRows (Int1, Type1 )
SELECT 233, 1
INSERT INTO AllRows (Int1, Type1 )
SELECT 377, 1
INSERT INTO AllRows (Int1, Type1 )
SELECT 610, 1
INSERT INTO AllRows (Int1, Type1 )
SELECT 987, 2
INSERT INTO AllRows (Int1, Type1 )
SELECT 1597, 1
INSERT INTO AllRows (Int1, Type1 )
SELECT 2584, 1
INSERT INTO AllRows (Int1, Type1 )
SELECT 4181, 1
INSERT INTO AllRows (Int1, Type1 )
SELECT 6765, 1
END
GO
EXEC PopulateAllRows
GO
SELECT * FROM AllRows
GO
WITH Temp(OrigID)
AS
(
SELECT OrigID FROM
(SELECT Row_Number() OVER (ORDER BY AllRowsID Asc ) AS RowScore, AllRowsID AS OrigID, Int1 AS OrigValue FROM Allrows WHERE Type1=1) AS FromTable
INNER JOIN
RowsToChange AS ToTable
ON FromTable.RowScore = ToTable.Int1
)
UPDATE AllRows
SET Int1=1000
FROM
Temp as InTable
JOIN Allrows as OutTable
ON Intable.OrigID = OutTable.AllRowsID
GO
SELECT * FROM AllRows
GO
Hi, can anyone shed some light on this issue?SELECT Status from lupStatuswith a normal query it returns the correct recordcountSELECT Status from lupStatus GROUP BY Statusbut with a GROUP By clause or DISTINCT clause it return the recordcount= -1
View 3 Replies View RelatedI am working with a vendor on upgrading their application from SQL2K to SQL2K5 and am running into the following.
When on SQL Server 2000 the following statement ran without issue:
UPDATE dbo.Track_ID
SET dbo.Track_ID.Processed = 4 --Regular 1 leg call thats been completed
WHERE Processed = 0 AND LegNum = 1
AND TrackID IN
(
SELECT TrackID
FROM dbo.Track_ID
GROUP BY TrackID
HAVING MAX(LegNum) = 1 AND
TrackID + 'x1' IN
(
SELECT
dbo.Track_ID.TrackID + 'x' + CONVERT(NVARCHAR(2), COUNT(dbo.Track_ID.TrackID))
FROM dbo.Track_ID INNER JOIN dbo.transactions
ON dbo.Track_ID.SM_ID = dbo.transactions.sm_session_id
GROUP BY dbo.Track_ID.TrackID
)
)
Once moved to SQL Server 2005 the statement would not return and showed SOS_SCHEDULER_YIELD to be the waittype when executed. This machine is SP1 and needs to be upgraded to SP2, something that is not going to happen near time.
I changed the SQL to the following, SQL Server now runs it in under a second, but now the app is not functioning correctly. Are the above and the following semantically the same?
UPDATE dbo.Track_ID
SET dbo.Track_ID.Processed = 4 --Regular 1 leg call thats been completed
WHERE Processed = 0 AND LegNum = 1
AND TrackID IN
(
SELECT TrackID
FROM dbo.Track_ID
WHERE TrackID + 'x1' IN
(
SELECT dbo.Track_ID.TrackID + 'x' + CONVERT(NVARCHAR(2), COUNT(dbo.Track_ID.TrackID))
FROM dbo.Track_ID INNER JOIN dbo.transactions
ON dbo.Track_ID.SM_ID = dbo.transactions.sm_session_id
GROUP BY dbo.Track_ID.TrackID
)
GROUP BY TrackID
HAVING MAX(LegNum) = 1
)
2 examples:
1) Rows ordered using textual id rather than numeric id
Code Snippet
select
cast(v.id as nvarchar(2)) id
from
(
select 1 id
union select 2 id
union select 11 id
) v
order by
v.id
Result set is ordered as: 1, 11, 2
I expect: 1,2,11
if renamed or removed alias for "cast(v.id as nvarchar(2))" expression then all works fine.
2) SQL server reject query below with next message
Server: Msg 169, Level 15, State 3, Line 16
A column has been specified more than once in the order by list. Columns in the order by list must be unique.
Code Snippet
select
cast(v.id as nvarchar(2)) id
from
(
select 1 id
union select 2 id
union select 11 id
) v
cross join (
select 1 id
union select 2 id
union select 11 id
) u
order by
v.id
,u.id
Again, if renamed or removed alias for "cast(v.id as nvarchar(2))" expression then all works fine.
It reproducible on
Microsoft SQL Server 2000 - 8.00.2039 (Intel X86) May 3 2005 23:18:38 Copyright (c) 1988-2003 Microsoft Corporation Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 2)
and
Microsoft SQL Server 2005 - 9.00.3042.00 (Intel X86) Feb 9 2007 22:47:07 Copyright (c) 1988-2005 Microsoft Corporation Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 2)
In both cases database collation is SQL_Latin1_General_CP1251_CS_AS
If I check quieries above on database with SQL_Latin1_General_CP1_CI_AS collation then it works fine again.
Could someone clarify - is it bug or expected behaviour?
I am using web developer 2008, while connecting to I wanted to fetch data from Lotus notes database file, for this i used notesql connector, while connectiong to notes database i am fetting error
ERROR [42000] [Lotus][ODBC Lotus Notes]Table reference has to be a table name or an outer join escape clause in a FROM clause
I have already checked that database & table name are correct, please help me out
How i can fetch the lotus notes data in my asp.net pages.
I am using web developer 2008, while connecting to I wanted to fetch data from Lotus notes database file, for this i used notesql connector, while connectiong to notes database i am fetting error
ERROR [42000] [Lotus][ODBC Lotus Notes]Table reference has to be a table name or an outer join escape clause in a FROM clause
I have already checked that database & table name are correct, please help me out
How i can fetch the lotus notes data in my asp.net pages.
Hi,
What is HAVING clause equivalent in the following oracle query, without the combination of "GROUP BY" clause ?
eg :
SELECT SUM(col1) from test HAVING col2 < 5
SELECT SUM(col1) from test WHERE x=y AND HAVING col2 < 5
I want the equivalent query in MSSQLServer for the above Oracle query.
Also, does the aggregate function in Select column(here the SUM(col1)) affect in anyway the presence of HAVING clause?.
Thanks,
Gopi.
How Can I use Top Clause with GROUP BY clause?
Here is my simple problem.
I have two tables
Categories
Products
I want to know Top 5 Products in CategoryID 1,2,3,4,5
Resultset should contain 25 Rows ( 5 top products from each category )
I hope someone will help me soon.
Its urngent
thanks in advance
regards
Waqas
hi..
i have basic question like
what is differance between conditions put in ON clause and in WHERE clause in JOINS????
see conditions that shown in brown color
select d1.SourceID, d1.PID, d1.SummaryID, d1.EffectiveDate,
d1.Audit, d1.ExpirationDate, d1.Indicator
from[DB1].[dbo].[Implicit] d1 inner join [DB2].[dbo].[Implicit] d2
on d1.SummaryID=d2.SummaryID
AND d1.ListType = d2.ListType
AND (d1.EffectiveDate <= d2.ExpirationDate or d2.ExpirationDate is null)
AND (d1.ExpirationDate >= d2.EffectiveDate or d1.ExpirationDate is null)
whered1.ImplicitID >= d2.ImplicitID AND
(d1.SourceID<>d2.SourceID
OR (d1.SourceID IS NULL AND d2.SourceID IS NOT NULL)
OR (d1.SourceID IS NOT NULL AND d2.SourceID IS NULL)
)
select d1.SourceID, d1.PID, d1.SummaryID, d1.EffectiveDate,
d1.Audit, d1.ExpirationDate, d1.Indicator
from[DB1].[dbo].[Implicit] d1 inner join [DB2].[dbo].[Implicit] d2
on d1.SummaryID=d2.SummaryID
AND d1.ImplicitID = d1.ImplicitIDAND d1.ListType = d2.ListType
AND (d1.EffectiveDate <= d2.ExpirationDate or d2.ExpirationDate is null)
AND (d1.ExpirationDate >= d2.EffectiveDate or d1.ExpirationDate is null)
whered1.ImplicitID >= d2.ImplicitID AND
(d1.SourceID<>d2.SourceID
OR (d1.SourceID IS NULL AND d2.SourceID IS NOT NULL)
OR (d1.SourceID IS NOT NULL AND d2.SourceID IS NULL)
)
another thing...
if we put AND d1.ImplicitID = d1.ImplicitID condition in second query then shall we remove
d1.ImplicitID >= d2.ImplicitID from WHERE clause????
Hi everyone,
I saw some queries where SQL inner join clause and the where clause is used at the same time. I knew that "on" is used instead of the "where" clause. Would anyone please exaplin me why both "where" and "on" clause is used in some sql Select queries ?
Thanks
da = New Data.SqlClient.SqlDataAdapter("SELECT [Products].[Names], Count([ProductList].[Products]) AS [Total] FROM [Products] LEFT JOIN [ProductList] ON [ProductList].[Names] = [Products].[Names] GROUP BY [Products].[Names] ", strConnection)
can we use a where condition in the statement.If so how can we use it.
Hi all,I'm building a DataSet on Visual Studio and don't know how to do a condition (if/else) with SQL... I have a search form, with a DropDownList and have 2 options in it: Search by Title or Search by Author. If the "Title" is selected, then the value is "title and if "Author" is selected, then the value is "author".Here is what I have right now for the DataSet, as seperated queries but I think I can combine them to be one single query 1.This will returns the songs that matches the title:SELECT LYRICS_PK, LYRICS_TITLE, LYRICS_TITLE2, LYRICS_WRITER, LYRICS_WRITER2, LYRICS_COWRITER, LYRICS_DATE_ADDED, UserId_FK, LYRICS_APPROVED, LYRICS_TYPE, LYRICS_VIEWS, LYRICS_ADDED_BYFROM t_lyricsWHERE ((@LYRICS_TITLE IS NULL) OR (LYRICS_TITLE LIKE '%' + @LYRICS_TITLE + '%') OR (LYRICS_TITLE2 LIKE '%' + @LYRICS_TITLE + '%')) AND (@LYRICS_TYPE = 'title') 2. This returns the songs that matches the author: SELECT LYRICS_PK, LYRICS_TITLE, LYRICS_TITLE2, LYRICS_WRITER,
LYRICS_WRITER2, LYRICS_COWRITER, LYRICS_DATE_ADDED, UserId_FK,
LYRICS_APPROVED, LYRICS_TYPE, LYRICS_VIEWS, LYRICS_ADDED_BY
FROM t_lyrics
WHERE ((@LYRICS_AUTHOR IS NULL) OR (LYRICS_AUTHOR LIKE '%' +
@LYRICS_AUTHOR + '%') OR (LYRICS_AUTHOR2 LIKE '%' + @LYRICS_AUTHOR + '%'))
AND (@LYRICS_TYPE = 'author') This is very inefficient because I have 2 queries, and I need to build 2 ObjectDataSources as well as 2 different GridViews to display the results. I think we can do something likeSELECT .... ... FROM t_lyricsif (@LYRICS_TYPE = 'title') DO THE WHERE CLAUSE THAT RETURNS MATCHES WITH TITLEelse if (@LYRICS_TYPE = 'author') DO THE WHERE CLAUSE THAT RETURNS MATCHES WITH AUTHOR But I don't know how to write that in T-SQL.Any help would be greatly appreciated,Thank you very much,Kenny.
hi
i want to give .text filed of a TextBox to where condition but the result is not correct.
The SQL is like following:
selStr = "SELECT COUNT(*) FROM Users WHERE (Name = usernameTextBox.Text) and (Password = passwdTextBox.Text)"
i have a table like this
ID DATE time status
01 11/11/2011 10.23 Entry
02 11/11/2011 11 .47 Exit
03 11/11/2011 12.54 Entry
04 11/11/2011 2.54 Exit
i want to select the minimum time from entry and maximum time in exit.
the status should be checked when taking the max and min values .
how to do this.
Hi,I want to write a SQL stmt.(condition) that checks the following @ActionPLanID > 0 and ActionPlanID exists in table A simultaneously?
I was thinking of using IF NOT exists but don't know how to write it. Am I going in the right direction?
Thanks,
VBJP
hi,
i'm working on a query and have discovered something fairly simple regarding "and" / "or" condition.
if I use e.g.
id_product in ('1111','2222')
as a result i should get all products that match id = 1111 and id = 2222.
But if I do it like
id_product in ('1111')
and id_product in ('2222')
as a result i get 0 rows returned, where as i want to find invoices that have both products, and not those which have either product 1111 or 2222 or even both :)
thank you for any suggestions!
I have a query something like
select a, b, c from mytable where a=@prmA
If prmA is something like generic all, any, I want this query return value without condition. Is there a way to do this with sql or I should write stored procedure that checks @prmA and all other condition parameters and generate new SQL statement?
Regards,
Hakan
Hi All,
I have stored Candidates Resume in binary(Image) format in database.
I have a search feature to search resume.
For Example:
with all these words: ASP.Net, SQL Server, 2 Years
without these words: Java
Here I get Candidate which CONTAINS("ASP.Net" AND "SQL Server" AND "2 Years" AND NOT "Java")
Now I want to search candidates only
without these words: Java
it means the condition is CONTAINS(NOT Java)
is this possible?
if yes, how?
Thanks in advance.
Hi,
In following query, I use three conditions in WHERE caluse. When I use only CreditUnion.Id=@CreditUnionID
Then I get the right value set. But when I join other two conditions , i get all the values instead of relevant data for that parameter.
Can anyone say why it happenes?
Code Block
SELECT
Member.LastName + ' ' + Member.FirstName AS MemberName, CASE WHEN CuStatus = 'Existing' THEN 'Existing' ELSE 'New' END AS MemberType, EnumCUMembershipStatus.UIText AS Status, CDOrder.DecidedOnCU, SysUserLogon.LastName + ' ' + SysUserLogon.FirstName AS CUDecisionOfficer, 'CD' AS ProductType, CreditUnion.Name
FROM
Member INNER JOIN
CDOrder ON Member.LastCDOrderFK = CDOrder.Id AND Member.Id = CDOrder.MemberFK INNER JOIN
CreditUnion ON Member.CreditUnionFK = CreditUnion.Id INNER JOIN
EnumCUMembershipStatus ON Member.CuStatus = EnumCUMembershipStatus.Name INNER JOIN
SysUserLogon ON CreditUnion.Id = SysUserLogon.CreditUnionFK
WHERE (CreditUnion.Id = @CreditUnionID) AND (Member.CuStatus = 'Approved') OR (Member.CuStatus = 'Declined')
UNION
SELECT
Member_1.LastName + ' ' + Member_1.FirstName AS MemberName,
CASE WHEN CuStatus = 'Existing' THEN 'Existing' ELSE 'New' END AS MemberType, EnumCUMembershipStatus_1.UIText AS Status, LoanApplication.DecidedOnCU, SysUserLogon_1.LastName + ' ' + SysUserLogon_1.FirstName AS CUDecisionOfficer, 'Loan' AS ProductType,
CreditUnion_1.Name
FROM
Member AS Member_1 INNER JOIN
LoanApplication ON Member_1.LastLoanApplicationFK = LoanApplication.Id AND Member_1.Id = LoanApplication.MemberFK LEFT OUTER JOIN CreditUnion AS CreditUnion_1 ON Member_1.CreditUnionFK = CreditUnion_1.Id LEFT OUTER JOIN EnumCUMembershipStatus AS EnumCUMembershipStatus_1 ON Member_1.CuStatus = EnumCUMembershipStatus_1.Name LEFT OUTER JOIN SysUserLogon AS SysUserLogon_1 ON LoanApplication.SysUserLogonFK = SysUserLogon_1.Id AND LoanApplication.SysUserLogonCUFK = SysUserLogon_1.Id AND CreditUnion_1.Id = SysUserLogon_1.CreditUnionFK
WHERE (CreditUnion_1.Id = @CreditUnionID) AND
Member_1.CuStatus = 'Approved' OR Member_1.CuStatus = 'Declined'
I am an ASP.NET Developer I am using two SQL Server databases, 2005 and sql express.I am using a select statement on an IN CONDITION
One table, Table name = SOP10100 resides in a SQL Server 2005 DatabaseThe other table, Table name = ORDER resides on a SQL server express Database
I am writing the following sql statement
SELECT ORDERNO, CARRIER FROM SOP10100WHERE ORDERNO IN ('ORD000234','ORD000384',....)
My question is how many values can I fit on this IN conditionI mean the maximum number of values (upperlimit)
Is there a better way to do this Using ASP.NET ?