Solved

SQL Statement to return records with today's date

Posted on 2010-08-18
9
577 Views
Last Modified: 2012-05-10
I am using a PSQL db I need to return records from the current day. What statement would I use. The date is store as 20100818
0
Comment
Question by:RTsal
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 16

Expert Comment

by:carsRST
ID: 33469592
you might try something like this:

select * from <<table name>>
where
where
SUBSTRING('20100818', 5,2)=Month(GETDATE())
and
SUBSTRING('20100818', 1,4)=Year(GETDATE())
and
SUBSTRING('20100818', 7,2)=DAy(GETDATE())

Swap 20100818 with the field name
0
 
LVL 18

Expert Comment

by:mirtheil
ID: 33469596
What are you using to generate the statement?  
I can't think of an easy way to do it within the SQL functions provided by PSQL.  If you are using something like VB, you can do something like:

sSQL = "select * from table where datefield = " & Format(Date, "YYYYMMDD")

Then Execute the statement.  You would need to change "table" and "datefield" to match your table name and field name.  
0
 
LVL 16

Expert Comment

by:carsRST
ID: 33469604
Drop one "where"  :)


select * from <<table name>>
where
SUBSTRING('20100818', 5,2)=Month(GETDATE())
and
SUBSTRING('20100818', 1,4)=Year(GETDATE())
and
SUBSTRING('20100818', 7,2)=DAy(GETDATE())
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 28

Expert Comment

by:Bill Bach
ID: 33469719
Or, try this:

SELECT *
   FROM TableName
WHERE
   FieldName = Cast(Year(CURDATE()) as CHAR(4))
+ RIGHT('0' + RTRIM(Cast(Month(CURDATE()) as CHAR(2))),2)
+ RIGHT('0' + RTRIM(Cast(DayOfMonth(CURDATE()) as CHAR(2))),2)

Note that you'll need to supply your own Tablename and FieldName for the pseudo-date field.  The lengthy function converts the current date into the right format and then checks for equivalency to your data field.  

If you are building an application, I would strongly consider building the function to convert CURDATE() into the expected format FIRST and then just supply the right value -- it will run MUCH faster.
0
 

Author Comment

by:RTsal
ID: 33470037
I tried the code from carsRST and I get the error
[LNA][Pervasive][ODBC Engine Interface]Error in expression: Month ( GETDATE ( VOID ) )
I tried the code from BillBach and it takes a very long time ( and cause the PCC to stop responding).  Is there a way to quicken it.
0
 
LVL 28

Accepted Solution

by:
Bill Bach earned 500 total points
ID: 33470108
I think the problem with the carsRST query was that you posted in the Microsoft SQL Server zone, and this command is not valid in Pervasive PSQL.

The only way to speed it up is to build a stored procedure to pre-calculate the value to eliminate the functions.  Something like this:

CREATE PROCEDURE GetTodaysRecords()
RETURNS (Fieldlist)
AS
BEGIN
    DECLARE :today CHAR(10);
   SET :today = Cast(Year(CURDATE()) as CHAR(4)) + RIGHT('0' + RTRIM(Cast(Month(CURDATE()) as CHAR(2))),2) + RIGHT('0' + RTRIM(Cast(DayOfMonth(CURDATE()) as CHAR(2))),2);
   SELECT * From TableName Where FieldName = :today;
END;

Again, you'll need to define the fields, and the field list for the RETURNS Clause.  You'll also need to verify that the field is a CHAR field, and not an integer field, and change it accordingly if it IS an integer field instead.  
0
 

Author Comment

by:RTsal
ID: 33470243
I am getting a syntax error. Please let me know where I have to correct from the code below
CREATE PROCEDURE GetTodaysRecords()
RETURNS (NUMBER, CUST_NAME, CUST_NO, BVADDR_CEV_NO_2, CEV_NO, ADDR_TYPE, BVADDR1, BVADDR2, BVADDR3, BVADDR4, 
BVCITY, BVPROVSTATE, BVPOSTALCODE, BVCOUNTRYCODE, BVADDRTELNO1, BVADDREMAIL)
AS
BEGIN
    DECLARE :today CHAR(10);
   SET :today = Cast(Year(CURDATE()) as CHAR(4)) + RIGHT('0' + RTRIM(Cast(Month(CURDATE()) as CHAR(2))),2) + RIGHT('0' + RTRIM(Cast(DayOfMonth(CURDATE()) as CHAR(2))),2);
   SELECT * From "SALES_HISTORY_HEADER", "HISTORY_ADDRESS" Where IN_DATE = :today;
END;

Open in new window

0
 
LVL 28

Expert Comment

by:Bill Bach
ID: 33470330
You have to indicate the data types for each column in the RETURNS Clause.  See the SQL Syntax Manual (part of the PCC Documentation) for exact details.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now