Showing posts with label build. Show all posts
Showing posts with label build. Show all posts

Monday, March 26, 2012

query with like

How do I build a query using like, but the options come from a table? Such
as
Select lname from tblClients where lname like '%smith%'
but the "smith" comes from another table, such as
select lname from lnamelist.Simple example using the Pubs database.
declare @.lname varchar(50)
select @.lname = au_lname from authors where au_id = '172-32-1176'
Select * from authors
where au_lname like '%' + @.lname + '%'
HTH
Jerry
"et" <eagletender2001@.yahoo.com> wrote in message
news:%23TdPK6TvFHA.464@.TK2MSFTNGP15.phx.gbl...
> How do I build a query using like, but the options come from a table?
> Such as
> Select lname from tblClients where lname like '%smith%'
> but the "smith" comes from another table, such as
> select lname from lnamelist.
>|||Something like:
SELECT *
FROM Clients c1
WHERE EXISTS ( SELECT *
FROM Names n1
WHERE c1.last_name LIKE '%' + n1.last_name + '%' );
Anith|||Example:
select a.*, b.*
from t1 as a inner join t2 as b on a.c1 like '%' + b.c1 + '%'
Do not expect that sql server performs an index s if there is an index in
[t1] by [c1], because this kind of expressions (using "%" wildcard at the
begining) are not considered as search arguments.
AMB
"et" wrote:

> How do I build a query using like, but the options come from a table? Suc
h
> as
> Select lname from tblClients where lname like '%smith%'
> but the "smith" comes from another table, such as
> select lname from lnamelist.
>
>|||You can join the two:
SELECT C.lane
FROM Clients AS C
JOIN Names AS N
ON C.lname LIKE '%' + N.lname + '%';
BG, SQL Server MVP
www.SolidQualityLearning.com
"et" <eagletender2001@.yahoo.com> wrote in message
news:%23TdPK6TvFHA.464@.TK2MSFTNGP15.phx.gbl...
> How do I build a query using like, but the options come from a table?
> Such as
> Select lname from tblClients where lname like '%smith%'
> but the "smith" comes from another table, such as
> select lname from lnamelist.
>|||et wrote:
> How do I build a query using like, but the options come from a table?
> Such as
> Select lname from tblClients where lname like '%smith%'
> but the "smith" comes from another table, such as
> select lname from lnamelist.
If the entire string '%smith%' is stored in another table, you could use
something like:
Select
lname
From
dbo.tblClients
Where
lname LIKE (Select lname from dbo.lname_table where lnameid =
@.some_pk_value)
But something tells me your table does not have the percent signs in the
string. If that's the case, you need to use dynamic sql, which has some
drawbacks:
http://www.sommarskog.se/dynamic_sql.html
David Gugick
Quest Software
www.imceda.com
www.quest.com|||David Gugick wrote:
> forget what I wrote
My mind is mush today...
David Gugick|||"Anith Sen" <anith@.bizdatasolutions.com> wrote in message
news:e2bjUAUvFHA.2540@.TK2MSFTNGP09.phx.gbl...
> Something like:
> SELECT *
> FROM Clients c1
> WHERE EXISTS ( SELECT *
> FROM Names n1
> WHERE c1.last_name LIKE '%' + n1.last_name + '%' );
> --
> Anith
>
I haven't run this yet, but wouldn't this create a correlated subquery where
the SELECT in the exists clause gets run once for every c1.last_name in the
outer query?
Simply wondering if my head is working properly today.
Rick Sawtell
MCT, MCSD, MCDBA|||Yes, it does create a correlation. But functionally, it is the same as the
ones posted by Alejandro or Itzik.
Anith

Tuesday, March 20, 2012

query to retrieve the columns that are null in a table

Hi,
I need help to build a query that shows me how many columns inside a range on columns are null.
Example: quantity1;quantity2;quantity3;quantity4;quantity5; quantity6;quantity7;
Which columns are null?
Thanks in advanceHi Teixeira,
I'm not sure what you are asking. If you could supply a table creation script some test data, and what the "result" should be based on the test data, that would help enormously.

Thanks,
Cat|||USE [myDB]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[books](
[book_id] [int] IDENTITY(1,1) NOT NULL,
[book_description] [nvarchar](max) COLLATE Latin1_General_CI_AS NULL,
[quantity1] [decimal](18, 2) NOT NULL,
[quantity2] [decimal](18, 2) NULL,
[quantity3] [decimal](18, 2) NULL,
[quantity4] [decimal](18, 2) NULL,
[quantity5] [decimal](18, 2) NULL,
[quantity6] [decimal](18, 2) NULL,
[quantity7] [decimal](18, 2) NULL,
[quantity8] [decimal](18, 2) NULL,
[quantity9] [decimal](18, 2) NULL,
[quantity10] [decimal](18, 2) NULL

this is my struture adapted.
based on this, i want to know which columns are not NULL, for my qyery result do not display for example 10 Quantity columns when i have just 3 that have quantities.|||Do you expect your query to return a single rowset, or is it possible to return multiple rows?|||yes!
It can return several rows.
but its not necessary to return columns that has null or empty values, because it would generated a lot of unnecessary columns in my datagrid display object|||I would change your structure from this:

CREATE TABLE [dbo].[books](
[book_id] [int] IDENTITY(1,1) NOT NULL,
[book_description] [nvarchar](max) COLLATE Latin1_General_CI_AS NULL,
[quantity1] [decimal](18, 2) NOT NULL,
[quantity2] [decimal](18, 2) NULL,
[quantity3] [decimal](18, 2) NULL,
[quantity4] [decimal](18, 2) NULL,
[quantity5] [decimal](18, 2) NULL,
[quantity6] [decimal](18, 2) NULL,
[quantity7] [decimal](18, 2) NULL,
[quantity8] [decimal](18, 2) NULL,
[quantity9] [decimal](18, 2) NULL,
[quantity10] [decimal](18, 2) NULL)

to this:

CREATE TABLE [dbo].[books](
[book_id] [int] IDENTITY(1,1) NOT NULL,
[book_description] [nvarchar](max) COLLATE Latin1_General_CI_AS NULL)
GO
CREATE TABLE [dbo].[bookquantity](
[book_id] [int] IDENTITY(1,1) NOT NULL,
[quantity] [decimal](18, 2) NOT NULL)
GO
ALTER TABLE [dbo].[bookquantity]
ADD CONSTRAINT FK_book (book_id) REFERENCE [books] (book_id)
GO

This way you are not tied to only 10 quantities and you can don't need to even store the NULL values.

If you can't change the structure of the table, I would suggest either creating a temp table with the above structure and populating it with the data from the master table so that you can weed out the nulls, or creating a single delimited string which the application can parse through. SQL can't really handle returning a result set with a variable number of fields.

The first suggestion would yield a result set like:
book_id quantity
------
1 12.70
1 33.45
1 9.00

The second suggestion would yield a result set like:
book_id quantity_list
--------
1 12.70|33.45|9.00

Hope this helps.
Cat|||I think you're both ideas are a good solution.
As i've some data already in the tables, normalize it more as you suggested would'd take me more time, but the second idea solves the problem perfectly.

Thanks for the help.

Teixeira

Saturday, February 25, 2012

Query the results of another query

I'm new to the database world - I know what I want to do, but not sure if or how to do it...

Is there a way to run a query and then build on it to query the results of that query?

My example is as follows:

Query the number of distinct machines by ID in Week 1 = Results

Query the distinct number of machines by ID in Week 2 minus the Results from Week 1

- NOT EXISTS
SELECT pub_name
FROM publishers
WHERE NOT EXISTS
(SELECT *
FROM titles
WHERE pub_id = publishers.pub_id
AND type = 'business')

- Use NOT IN
SELECT pub_name
FROM publishers
WHERE pub_id NOT IN
(SELECT pub_id
FROM titles
WHERE type = 'business')


- Perform a LEFT OUTER JOIN and check for a NULL condition
SELECT pub_name
FROM publishers A
LEFT OUTER JOIN titles B
ON A.pub_id = B.pub_id
WHERE A.pub_id IS NULL

|||

Thank you - I used the NOT IN and it seems to be doing exactly what I wanted.

Much Appreciated!!

Query that automatically deletes records that are older than 365 d

Hi all,
I'm trying to build a self running query that deletes all records that are
older than 365 days. the code i've used is -
DELETE RECadvertId
RECadRecruiterId
RECadreference
RECadjobtype
RECadLocation
RECadJobTitle
RECadSalary
RECadcategory
RECadduration
RECadstartdate
RECadDatePosted
RECadCompanyID
RECadDescription
RECadDescriptionshort
RECaddayspostedfor
RECadBenefits
FROM [dbo].[RECadvert]
WHERE RECadDatePosted > DATEADD(d,365,RECadDatePosted)
It isn't working, any ideas?
thanksLeave out the column names:
DELETE FROM [dbo].[RECadvert]
WHERE RECadDatePosted < DATEADD(dd,-365,getdate())
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"GTN170777" <GTN170777@.discussions.microsoft.com> wrote in message
news:84131850-B81C-4878-84CD-BC8C0116AD56@.microsoft.com...
Hi all,
I'm trying to build a self running query that deletes all records that are
older than 365 days. the code i've used is -
DELETE RECadvertId
RECadRecruiterId
RECadreference
RECadjobtype
RECadLocation
RECadJobTitle
RECadSalary
RECadcategory
RECadduration
RECadstartdate
RECadDatePosted
RECadCompanyID
RECadDescription
RECadDescriptionshort
RECaddayspostedfor
RECadBenefits
FROM [dbo].[RECadvert]
WHERE RECadDatePosted > DATEADD(d,365,RECadDatePosted)
It isn't working, any ideas?
thanks|||On Sun, 29 Jan 2006 12:55:28 -0800, GTN170777 wrote:

>Hi all,
>I'm trying to build a self running query that deletes all records that are
>older than 365 days. the code i've used is -
>DELETE RECadvertId
> RECadRecruiterId
> RECadreference
> RECadjobtype
> RECadLocation
> RECadJobTitle
> RECadSalary
> RECadcategory
> RECadduration
> RECadstartdate
> RECadDatePosted
> RECadCompanyID
> RECadDescription
> RECadDescriptionshort
> RECaddayspostedfor
> RECadBenefits
> FROM [dbo].[RECadvert]
> WHERE RECadDatePosted > DATEADD(d,365,RECadDatePosted)
>It isn't working, any ideas?
>thanks
Hi GTN170777,
You've got the syntax of the DELETE statement wrong (you don't include a
column list - a DELETE will always rempve the entire row). And the WHERE
clause is incorrect too - this checks if RECadDatePosted is 365 days
older than itself (which it of course never is). You should compare
RECadDatePosted with the date that was 365 days before "now".
Something like this:
DELETE FROM dbo.RECadvert
WHERE RECadDatePosted < DATEADD(d, -365, CURRENT_TIMESTAMP)
(Note: this query is untested. Please test it on a test database first.
Also, enclose it in a transaction and check results before issuing a
COMMIT or a ROLLBACK command).
Hugo Kornelis, SQL Server MVP|||The WHERE clause is the problem. You cannot find one row where the value in
a column is not the same
as the value in that same column, i.e. the same value (take a long look at y
our WHERE clause and you
will understand). You probably want something like:
WHERE RECadDatePosted < DATEADD(d,-365,CURRENT_TIMESTAMP)
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"GTN170777" <GTN170777@.discussions.microsoft.com> wrote in message
news:84131850-B81C-4878-84CD-BC8C0116AD56@.microsoft.com...
> Hi all,
> I'm trying to build a self running query that deletes all records that are
> older than 365 days. the code i've used is -
> DELETE RECadvertId
> RECadRecruiterId
> RECadreference
> RECadjobtype
> RECadLocation
> RECadJobTitle
> RECadSalary
> RECadcategory
> RECadduration
> RECadstartdate
> RECadDatePosted
> RECadCompanyID
> RECadDescription
> RECadDescriptionshort
> RECaddayspostedfor
> RECadBenefits
> FROM [dbo].[RECadvert]
> WHERE RECadDatePosted > DATEADD(d,365,RECadDatePosted)
> It isn't working, any ideas?
> thanks|||DELETE FROM [dbo].[RECadvert]
WHERE DATEDIFF(dd, RECadDatePosted, GETDATE()) > 365
You need the GETDATE() function to return the current date and time. The
DATEDIFF function, as used here, calculates the number of days that have
passed between a value for RECadDatePosted and today. If that number is more
than 365, that row will be deleted.
You don't need to specify column names when deleting.
"GTN170777" wrote:

> Hi all,
> I'm trying to build a self running query that deletes all records that are
> older than 365 days. the code i've used is -
> DELETE RECadvertId
> RECadRecruiterId
> RECadreference
> RECadjobtype
> RECadLocation
> RECadJobTitle
> RECadSalary
> RECadcategory
> RECadduration
> RECadstartdate
> RECadDatePosted
> RECadCompanyID
> RECadDescription
> RECadDescriptionshort
> RECaddayspostedfor
> RECadBenefits
> FROM [dbo].[RECadvert]
> WHERE RECadDatePosted > DATEADD(d,365,RECadDatePosted)
> It isn't working, any ideas?
> thanks|||>> t isn't working, any ideas? <<
You might try writing SQL instead of whatever this language was. The
unit of work in SQL is a row; but you do not know that row is nothing
whatsoever like a record!! Please read a book or your error messages
before you post again.|||What hospitality. This will learn him to be curious about a subject. I
will bet he ran out and bought an entire catalog of books :)
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1138582546.129921.115400@.g49g2000cwa.googlegroups.com...
> You might try writing SQL instead of whatever this language was. The
> unit of work in SQL is a row; but you do not know that row is nothing
> whatsoever like a record!! Please read a book or your error messages
> before you post again.
>|||Why is it that 5 other people knew what he was talking about and gave the
correct solution and you didn't?
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials
"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1138582546.129921.115400@.g49g2000cwa.googlegroups.com...
> You might try writing SQL instead of whatever this language was. The
> unit of work in SQL is a row; but you do not know that row is nothing
> whatsoever like a record!! Please read a book or your error messages
> before you post again.
>