SQL Server datetime convert function equivalent to postgresql (pivotal hawq)

NEO

We have following SQL script in SQL Server 2012. We are about to write similar script in postgresql (HAWQ 1.3.1) at database conversion

SELECT * 
FROM tablename_1 
LEFT OUTER JOIN
     (SELECT 
          SUM(b.OrderValue) AS OrderValue, b.OrderDate, b.Description 
      FROM 
          (SELECT * 
           FROM tablename_2 rcd
           LEFT JOIN 
                (SELECT Distinct 
                     afv.Item, afv.Description, afd.KeyField
                 FROM tablename_3 afd
                 JOIN tablename_3 afv ON afv.FormType = afd.FormType 
                                      AND afv.FieldName = afd.FieldName 
                                      AND afv.Item = afd.AlphaValue
                 WHERE
                      afd.FormType = 'CUS'
                      AND afd.FieldName = 'COR002') a ON a.KeyField = rcd.Customer 
           WHERE
               OrderDate >= CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())-1),GETDATE()), 101)) b 
    GROUP BY
        b.OrderDate, b.Description) c ON rtr.CorpAcctName = c.Description

We tried and wrote the following script:

Above script compiled into postgresql ( VERSION HAWQ 1.3.1)

SELECT * from tablename_1  rtr LEFT OUTER JOIN
(SELECT SUM(b."OrderValue") as OrderValue,b."OrderDate", b."Description" from
(SELECT *  from tablename_2 rcd
LEFT JOIN 
( SELECT Distinct afv."Item", afv."Description", afd."KeyField"
FROM tablename_2 afd
  Join tablename_3 afv on afv."FormType" = afd."FormType" and afv."FieldName"=afd."FieldName" and afv."Item"=afd."AlphaValue"
Where   afd."FormType" = 'CUS'and afd."FieldName" = 'COR002') a
ON a."KeyField" =rcd."Customer" where "OrderDate">=TO_CHAR((CURRENT_DATE -(CURRENT_DATE-INTERVAL '1 DAY)) ,'MM-DD-YYYY')) b 
group by b."OrderDate", b."Description") c
on rtr."CorpAcctName"=c."Description"

Also tried with :

  • When we tried to convert ms sql server convert function into postgres for orderdate column comparison of OrderDate must reflect as 'MM-01-YYYY'(desired result) which is actually coming '00-01-0000' not desired.instead we are looking for outcome as '11-01-2015'

**

  • What will be the convert() function expression in postgresql for getting desired result?

**

a_horse_with_no_name

i wanted to achieve first day of month from my current_date

So you want everything that was created after the start of the current month.

A simple

where "OrderDate" >= date_trunc('month', current_date)

will do that.

Details on the date_trunc() method can be found in the manual:
http://www.postgresql.org/docs/current/static/functions-datetime.html


You should understand what your expression TO_CHAR((CURRENT_DATE -(CURRENT_DATE-INTERVAL '1 DAY')) ,'MM-DD-YYYY') is doing so that you avoid that error in the future:

First: current_date - interval '1 day' subtracts an interval from a date, which yields a timestamp: "yesterday" at 00:00:00.

Then you subtract that from today's date, so current_date - timestamp '2015-11-13 00:00:00.0' (if today is 2015-11-14).

This yields an interval: 0 years 0 mons 1 days 0 hours 0 mins 0.00 secs

You then pass that interval to the to_char() function which formats the passed interval. As it only has "1 day", no year, no month, the result of applying the format string 'MM-DD-YYYY' on that does indeed yield 00-01-0000.

You then compare this character value against a real date - which is also something you should not do.


You should really get rid of those dreaded quoted identifiers. They are much more trouble than they are worth it

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Convert Function from SQL Server to PostgreSQL

From Dev

Need a SQL Server function to convert local datetime to UTC that supports DST

From Dev

Need a SQL Server function to convert local datetime to UTC that supports DST

From Dev

Convert Epoch to DateTime SQL Server

From Dev

Convert nvarchar to datetime sql server

From Dev

Convert Epoch to DateTime SQL Server

From Dev

SQL Server Convert int to Datetime

From Dev

Convert nvarchar to datetime sql server

From Dev

What is the CHARINDEX (SQL SERVER) equivalent in POSTGRESQL?

From Dev

searching for microsoft sql server equivalent of postgresql form

From Dev

Convert datetime string in datetime format in SQL server

From Dev

SQL convert DateTime to Date (SQL server 2005)

From Dev

SQL Server equivalent of Excel's TINV function

From Dev

Equivalent of MySQL HEX / UNHEX function in SQL Server?

From Dev

SQL Server equivalent of Excel's TINV function

From Dev

Equivalent of MySQL HEX / UNHEX function in SQL Server?

From Dev

sql server convert datetime into another timezone?

From Dev

How to Convert datetime value to yyyymmddhhmmss in SQL server?

From Dev

SQL Server convert string to datetime fails

From Dev

SQL Server : convert datetime in place in table

From Dev

Convert Varchar Column to Datetime format - SQL Server

From Dev

convert datetime error in SQL Server (2005)?

From Dev

How to convert string to datetime in sql server

From Dev

Query in Microsoft SQL Server using Datetime Convert

From Dev

SQL Server query (view) Convert String to DateTime

From Dev

SQL Server : varchar convert to datetime error

From Dev

Convert INT column to Datetime in SQL Server

From Dev

Convert DateTime into desired format in SQL Server

From Dev

Convert string date to datetime on sql server

Related Related

  1. 1

    Convert Function from SQL Server to PostgreSQL

  2. 2

    Need a SQL Server function to convert local datetime to UTC that supports DST

  3. 3

    Need a SQL Server function to convert local datetime to UTC that supports DST

  4. 4

    Convert Epoch to DateTime SQL Server

  5. 5

    Convert nvarchar to datetime sql server

  6. 6

    Convert Epoch to DateTime SQL Server

  7. 7

    SQL Server Convert int to Datetime

  8. 8

    Convert nvarchar to datetime sql server

  9. 9

    What is the CHARINDEX (SQL SERVER) equivalent in POSTGRESQL?

  10. 10

    searching for microsoft sql server equivalent of postgresql form

  11. 11

    Convert datetime string in datetime format in SQL server

  12. 12

    SQL convert DateTime to Date (SQL server 2005)

  13. 13

    SQL Server equivalent of Excel's TINV function

  14. 14

    Equivalent of MySQL HEX / UNHEX function in SQL Server?

  15. 15

    SQL Server equivalent of Excel's TINV function

  16. 16

    Equivalent of MySQL HEX / UNHEX function in SQL Server?

  17. 17

    sql server convert datetime into another timezone?

  18. 18

    How to Convert datetime value to yyyymmddhhmmss in SQL server?

  19. 19

    SQL Server convert string to datetime fails

  20. 20

    SQL Server : convert datetime in place in table

  21. 21

    Convert Varchar Column to Datetime format - SQL Server

  22. 22

    convert datetime error in SQL Server (2005)?

  23. 23

    How to convert string to datetime in sql server

  24. 24

    Query in Microsoft SQL Server using Datetime Convert

  25. 25

    SQL Server query (view) Convert String to DateTime

  26. 26

    SQL Server : varchar convert to datetime error

  27. 27

    Convert INT column to Datetime in SQL Server

  28. 28

    Convert DateTime into desired format in SQL Server

  29. 29

    Convert string date to datetime on sql server

HotTag

Archive