Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / March 2008

Tip: Looking for answers? Try searching our database.

Newbie question:

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ty - 13 Mar 2008 18:27 GMT
Newbie question:
select b.ABCD_TYP_CD,a.* from ABX_REQ_BUCKT a inner join
auth_req_iteM_hdr b on a.MKTG_ITEM_HDR_ID = b.MKTG_ITEM_HDR_ID where
b.rights_req_id = <name> and a.ABCD_SYS_CD = "TS" and a.auth_id =
<auth_id>

Is it ok to use "*" for <name> and <auth_id> to display everything?

Thanks,
Ty
Russell Fields - 13 Mar 2008 18:40 GMT
Ty,

No, it is not OK to use "*" for that purpose.  I assume that this query is
either generated from client code or is code inside a stored procedure. If
you want such wildcarding there are a few things that you can do:

Character datatypes:    ColumnName LIKE '%'

You can, I suppose, case non-character datatypes into character and use the
LIKE operator, but that is sloppy and has many issues with how the person
might formulate the LIKE clause. For other datatypes, it is better to handle
the wildcard situation specifically.  For example, assuming that the ids are
integer and that parameters to a stored procedure are being used:

WHERE (b.rights_req_id = @Name OR @Name IS NULL) AND ...

RLF

> Newbie question:
> select b.ABCD_TYP_CD,a.* from ABX_REQ_BUCKT a inner join
[quoted text clipped - 6 lines]
> Thanks,
> Ty
Ty - 13 Mar 2008 20:03 GMT
On Mar 13, 12:40 pm, "Russell Fields" <russellfie...@nomail.com>
wrote:
> Ty,
>
[quoted text clipped - 26 lines]
>
> - Show quoted text -

Thanks for your quick response.... What if I need to narrow the search
down to display only 2 months of rows?

Example:

select * from ABXDB01.mgd_req_holdg where ABX_REQ_BUCKT = 'OK'.  So, I
have the following:

CMPL_Q_DT:  the list of dates are in this format 2007-06-17
23:00:13.827584

Is the following correct?

select * from ABXDB01.mgd_req_holdg where ABX_REQ_BUCKT = 'OK' where
(CMPL_Q_DT >#12/5/2007 5:00:00#)

-or-

select * from ABXDB01.mgd_req_holdg where ABX_REQ_BUCKT = 'OK' where
(CMPL_Q_DT Between #12/2/2007# And #1/1/2008#)

The # sound is used in SQL the same as the "*" is used in DOS.
Aaron Bertrand [SQL Server MVP] - 13 Mar 2008 20:16 GMT
Is the following correct?

select * from ABXDB01.mgd_req_holdg where ABX_REQ_BUCKT = 'OK' where
(CMPL_Q_DT >#12/5/2007 5:00:00#)

No.

(a) SQL Server does not use # to delimit dates;
(b) you should never use m/d/y or d/m/y or whatever that is (the fact that I
can't tell if that is Dec. 5th or May 12th should be a clue!);
(c) you can't say WHERE twice;
(d) you should always list your columns and never use * in production code.

SELECT <column_list>
FROM ABXDB01.mgd_req_holdg
WHERE ABX_REQ_BUCKT = 'OK'
AND CMPL_Q_DT > '2007-12-05T05:00:00';

select * from ABXDB01.mgd_req_holdg where ABX_REQ_BUCKT = 'OK' where
(CMPL_Q_DT Between #12/2/2007# And #1/1/2008#)

In addition to points above, which apply again, I strongly recommend NOT
using BETWEEN for date range queries.  The problem is, do you want to
include rows from January 1st at 12:00 AM exactly (or that were entered
without a time)?  What about rows from January 1st at 12:01 AM?  12:05 AM?
4:00 AM?  Your query will leave those out.  How about:

-- if you don't want to include any data from 2008:
SELECT <column_list>
FROM ABXDB01.mgd_req_holdg
WHERE ABX_REQ_BUCKT = 'OK'
AND CMPL_Q_DT >= '20071202
AND CMPL_Q_DT < '20080101';

-- if you do want to include data from 2008-01-01:
SELECT <column_list>
FROM ABXDB01.mgd_req_holdg
WHERE ABX_REQ_BUCKT = 'OK'
AND CMPL_Q_DT >= '20071202
AND CMPL_Q_DT < '20080102';

For more info on date range searching see Tibor's article:
http://www.karaszi.com/SQLServer/info_datetime.asp

The # sound is used in SQL the same as the "*" is used in DOS.

I have no idea what you mean.  It looks like you are borrowing code from
Access, not DOS.  In SQL Server, # is not used for delimiting dates.  % is
the wildcard character representing any number of characters, not * like in
Access.  And for single characters, it is an underscore _ not ? like in
Access.
Ty - 13 Mar 2008 21:25 GMT
On Mar 13, 2:16 pm, "Aaron Bertrand [SQL Server MVP]"
<ten....@dnartreb.noraa> wrote:
> Is the following correct?
>
[quoted text clipped - 46 lines]
> Access.  And for single characters, it is an underscore _ not ? like in
> Access.

I really appreciate the help.  You are exactly right.  I was looking
at MS Access.  I made the changes according to what you listed.  My
error "SQL0180N The syntax of the string representation of a datetime
value is incorrect.  SQLSTATE=22007".  I'm using yyyy-mm-dd.  I can
see the dates in a column on another query as 2007-06-17
23:00:13.827584 which is yyyy-mm-dd time(i guess).

My attempt:
SELECT <column_list>
FROM ABXDB01.mgd_req_holdg
WHERE ABX_REQ_BUCKT = 'OK'
AND CMPL_Q_DT > ''2008-03-01TO5:00:00';

tried:
AND CMPL_Q_DT > ''2008-03-01';

tried:
AND CMPL_Q_DT > ''2008-03-01%';

Ty
Kalen Delaney - 13 Mar 2008 22:05 GMT
Ty

Dates can be very tricky to work with, as you're finding out. The display
format for dates may have nothing to do with how SQL Server interprets an
input string as a date. Input and output formatting for dates can be
controlled completely separately.

Please read Tibor's great article:
The Ultimate Guide to the Datetime Datatypes
http://www.karaszi.com/sqlserver/info_datetime.asp

Signature

HTH
Kalen Delaney, SQL Server MVP
www.InsideSQLServer.com
http://DVD.kalendelaney.com

On Mar 13, 2:16 pm, "Aaron Bertrand [SQL Server MVP]"
<ten....@dnartreb.noraa> wrote:
> Is the following correct?
>
[quoted text clipped - 50 lines]
> Access. And for single characters, it is an underscore _ not ? like in
> Access.

I really appreciate the help.  You are exactly right.  I was looking
at MS Access.  I made the changes according to what you listed.  My
error "SQL0180N The syntax of the string representation of a datetime
value is incorrect.  SQLSTATE=22007".  I'm using yyyy-mm-dd.  I can
see the dates in a column on another query as 2007-06-17
23:00:13.827584 which is yyyy-mm-dd time(i guess).

My attempt:
SELECT <column_list>
FROM ABXDB01.mgd_req_holdg
WHERE ABX_REQ_BUCKT = 'OK'
AND CMPL_Q_DT > ''2008-03-01TO5:00:00';

tried:
AND CMPL_Q_DT > ''2008-03-01';

tried:
AND CMPL_Q_DT > ''2008-03-01%';

Ty
Aaron Bertrand [SQL Server MVP] - 18 Mar 2008 03:15 GMT
My attempt:
SELECT <column_list>
FROM ABXDB01.mgd_req_holdg
WHERE ABX_REQ_BUCKT = 'OK'
AND CMPL_Q_DT > ''2008-03-01TO5:00:00';

Why do you have '' and not ' at the beginning?  Did you notice that you
typed TO even though the code sample I provided had T0 (T zero)?  How about:

AND CMPL_Q_DT >= '2008-03-01T05:00:00';

AND CMPL_Q_DT > ''2008-03-01';

It's confusing, annoying, and not very intuitive, but when you are using
*just* a date as a literal, you should use YYYYMMDD in that case.  And
again, you need to not use two single quotes to start a string!  Also,
unless you meant to explicitly exclude rows marked at midnight, but include
rows from 12:00:00:003 AM on, you probably want to use >= as opposed to >.
How about:

AND CMPL_Q_DT >= '20080301';

AND CMPL_Q_DT > ''2008-03-01%';

No, dates are not strings, and wildcards do not work this way.  I strongly
recommend reading the article that Kalen and I both pointed out to you.
It's not going to solve all of your problems, but it should help you
understand them.

A
Ty - 21 Mar 2008 20:54 GMT
On Mar 17, 9:15 pm, "Aaron Bertrand [SQL Server MVP]"
<ten....@dnartreb.noraa> wrote:
> My attempt:
> SELECT <column_list>
[quoted text clipped - 26 lines]
>
> A

Got it!!!  It is a timestamp.  I used the
'2008-03-01-00.00.00.000000'
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.