Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Statement to return records with today's date

Posted on 2010-08-18
9
Medium Priority
?
664 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
What we learned in Webroot's webinar on multi-vector protection.
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…

722 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