SQL Statement to return records with today's date

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
RTsalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

carsRSTCommented:
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
mirtheilSoftware DeveloperCommented:
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
carsRSTCommented:
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
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

Bill BachPresident and Btrieve GuruCommented:
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
RTsalAuthor Commented:
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
Bill BachPresident and Btrieve GuruCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RTsalAuthor Commented:
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
Bill BachPresident and Btrieve GuruCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.