Solved

SQL Statement to return records with today's date

Posted on 2010-08-18
9
598 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…

735 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