Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 681
  • Last Modified:

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
0
RTsal
Asked:
RTsal
  • 3
  • 2
  • 2
  • +1
1 Solution
 
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
 
mirtheilCommented:
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
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.

 
Bill BachPresidentCommented:
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 BachPresidentCommented:
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
 
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 BachPresidentCommented:
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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now