MSSQL to POSTGRES conversion - postgresql

Anyone can help me with the following query from mssql to postgres?
DateAdd(Week,-(doc.[DelaiLivraisonNum]),Convert(date,sas.DateFin)) as IdDate
where DelaiLivraisonNum and DateFin are columns
Thanks

I always struggle with the SQL Server syntax, but I think this would be the equivalent (it's hard to tell as you did not explain what your code does):
datefin::date - make_interval(weeks => delailivraisonnum)
This assumes that datefin can be cast to a date (e.g. because it's a timestamp) and delailivraisonnum is an integer.

Related

Understanding case statement in sas proc sql

I am having difficulty in understand this sas code.
select
case
when DM_TURNOVER_TMP_STOCK."LIITM"n then
DM_TURNOVER_TMP_STOCK."LIITM"n
else
DM_TURNOVER_TMP_SALES."SDITM"n
end as "LIITM"n
case
when DM_TURNOVER_TMP_STOCK."LIMCU"n then
DM_TURNOVER_TMP_STOCK."LIMCU"n
normally we use sas in sql in condition statment of column but here seems to be diffrent.Please help me in understanding this in postgres term.
Assuming this is a query from tables DM_TURNOVER_TMP_STOCK and DM_TURNOVER_TMP_SALES,
when DM_TURNOVER_TMP_STOCK.LIITM is not missing and non zero, LIITM will get the value of DM_TURNOVER_TMP_STOCK.LIITM.
Otherwise, it will get the value of DM_TURNOVER_TMP_SALES.SDITM.

PostgreSQL in list, mixing string and int

We are using PostgreSQL 11 and have a query from Redmine database. It is a query that works fine in MySQL 8 but on PostgreSQL we get an error.
SELECT fixed_version_id
FROM issues WHERE
((issues.fixed_version_id IN ('current_version','2')));
ERROR: invalid input syntax for integer: "current_version"
LINE 1: ...d FROM issues WHERE ((issues.fixed_version_id IN ('current_v...
I understand that fixed_version_id is an int and that I quering strings. However, is other SQL like MySQL 8 you can do this and it actually returns values. But in PostgreSQL we get an error. Not sure if we have it setup wrong or if this is just the way PostgreSQL works?
Any help would be most appreciated thank you.
We ran this query
SELECT fixed_version_id
FROM issues
WHERE ((issues.fixed_version_id IN ('current_version','2')));
We were expecting Not to get an error.
SQL is a tightly typed language (seems MySql does not adhere to the standard). The only correction is using the correct type - in this case integer. But you can `CAST' an integer to text and compare.
SQL Standard:
WHERE ((cast (issues.fixed_version_id as text) IN ('current_version','2')));
Postgresql extension:
WHERE ((issues.fixed_version_id::text IN ('current_version','2')));

What is the postgreSql equivalent of Oracle timestamp(6). Does postreSql support timestamp in nanoseconds format?

How do I execute the following Query from Oracle in PostgreSql:
SELECT to_timestamp('20210603033632200995','yyyymmddhh24missFF6');
PostgreSQL has the standard conforming data type timestamp.
Six decimal places are microseconds, and yes, PostgreSQL supports that. It does not support nanoseconds, which would be 9 decimal places.
I derived at
SELECT to_timestamp('20210603033632200995','yyyymmddhh24missUS');
This worked in PostgresSql workbench. Can anyone else the verify this solution?
https://dbfiddle.uk/?rdbms=postgres_11&fiddle=b220060f7029507c29ab1f81c5e0acbd

How to Fix Postgres Numeric Auto Become 6 Decimal Places When Using Insert Into OPENQUERY from SQL Server

I found an issue of Postgres decimal places auto become 6 places when try to insert data from SQL Server into Postgres using OPENQUERY.
I have searched many references that suggested using CAST or Convert to limit decimal places from SQL Server, everything is work fine when I just tried select from the SQL Server side (It is 0.001), but whenever run the query like below, in Postgres (for example the 'Rounding' will become 0.001000).
For example:
INSERT INTO OPENQUERY(RND,
'SELECT
name,
rounding
FROM test.public.product_uom')
SELECT
UoMID,
0.001
FROM dbo.tUoM
WHERE UoMID IN ('YEAR', 'ZAK');
The expected result that I would like is to have the same value of Rounding when Insert into from SQL Server to Postgres that is 0.001. Any help or suggestions will be appreciated and thanks in advance.

Can't enter date into postgres field with datatype reltime

I'm trying to make an insert into postgres 8.4.13
insert into my_table (id, hour_memo) values (1,'17:30:00.000000 +01:00:00');
hour_memo is 'reltime datatype'
During the execution of the insert task i have this trouble:
ERROR: invalid input syntax for type reltime: "17:30:00.000000 +01:00:00"
I have absolutely no idea on how to do this?
The answer is that reltime doesn't support time zones, so the "+01..." thing is breaking it. Still - using reltime type is bad idea, and should be replaced by some normal type.