Can someone help me convert this sqlite query to a postgres query? - postgresql

SELECT substr(strftime('%Y', date),3,2) AS month, 0 AS zero FROM listvalue
I have this query in SQLite, when I import it into Postgres I'm having problem translating the substr(strftime('%Y', date),3,2) part.

substr(strftime('%Y', date),3,2) extracts the last 2 digits of the year part of the column date, but in your code you alias it as month!
if you want to do the same in Postgresql you can use extract() to get the year, typecast it to varchar and use substr() to get the last 2 chars:
substr(extract(year from date)::varchar, 3, 2)

You can translate the expression with TO_CHAR function to PostgreSQL:
SUBSTR(TO_CHAR(date, 'YYYY'), 3, 2)
Note: The format %Y will return the year of the date and not the month as your query suggests.
The SQLite query will return the last to digits of year of the date. You can omit the SUBSTR in PostgreSQL call by using the apropriate format:
TO_CHAR(date, 'YY')

We suspect that you want to extract the last two digits of the year value from the date column. The strftime() function won’t work in Postgres server. So, suggest you to use either one of the below queries to achieve your requirement,
SELECT substring(date_part('year', date)::varchar, 3, 2) AS year, 0 AS zero FROM listvalue;
SELCT to_char(shipped_date, 'YY'), 0 AS zero from listvalue;
Thanks,
Renuka N.

Related

how to converting string date in 'yyyy-m-dd' to 'yyyy-mm-dd' in Hive query?

I searched up and down but couldn't find anything that works.
I have a date that is stored as a string in this format: '2021-9-01' so there are no leading zeros in the month column. This is an issue when trying to select a max date as it interprets September to be greater than October.
Any time I run something that tried to convert this it literally never finishes. I can pull back 1 row when selecting * from... but this fails to complete:
select unix_timestamp(bad_date, 'yyyy-m-dd') from mytable
I'm using hive query so not sure how to make this conversion work so I can actually get October (this month) to show up as the max date?
Correct pattern for month is MM. mm is minutes.
from_unixtime(unix_timestamp(bad_date, 'yyyy-M-dd'),'yyyy-MM-dd')
One more method is to split and concatenate with lpad:
select concat_ws('-',splitted[0], lpad(splitted[1],2,0),splitted[2])
from
(
select split('2021-9-01','-') splitted
)s
Result:
2021-09-01

how to format date column in kdb

I am newbie to KDB. I have a KDB table which I am querying as:
select[100] from table_name
now this table has got some date columns which have dates stored in this format
yyyy.mm.dd
I wish to query that table and retrieve the date fields in specific format (like mm/dd/yyyy). If this would've been any other RDBMS table this is what i would have done:
select to_date(date_field,'mm/dd/yyyy') from table_name
I need kdb equivalent of above. I've tried my best to go through the kdb docs but unable to find any function / example / syntax to do that.
Thanks in advance!
As Anton said KDB doesn't have an inbuilt way to specify the date format. However you can extract the components of the date individually and rearrange as you wish.
For the example table t with date column:
q)t
date
----------
2008.02.04
2015.01.02
q)update o:{"0"^"/"sv'flip -2 -2 4$'string`mm`dd`year$\:x}date from t
date o
-----------------------
2008.02.04 "02/04/2008"
2015.01.02 "01/02/2015"
From right to left inside the function: we extract the month,day and year components with `mm`dd`year$:x before stringing the result. We then pad the month and day components with a null character (-2 -2 4$') before each and add the "/" formatting ("/"sv'flip). Finally the leading nulls are filled with "0" ("0"^).
Check out this GitHub library for datetime formatting. It supports the excel way of formatting date and time. Though it might not be the right fit for formatting a very large number of objects (but if distinct dates are very less then a keyed table and lj can be used for lookup).
q).dtf.format["mm/dd/yyyy"; 2016.09.23]
"09/23/2016"
q).dtf.format["dd mmmm yyyy"; 2016.09.03] // another example
"03 September 2016"
I don't think KDB has built-in date formatting features.
The most reliable way is to format date by yourself.
For example
t: ([]date: 10?.z.d);
update dateFormatted: {x: "." vs x; x[1],"/",x[2],"/",x[0]} each string date from t
gives
date dateFormatted
------------------------
2012.07.21 "07/21/2012"
2001.05.11 "05/11/2001"
2008.04.25 "04/25/2008"
....
Or, more efficient way to do the same formatting is
update dateFormatted: "/"sv/:("."vs/:string date)[;1 2 0] from t
now qdate is available for datetime parsing and conversion

To filter sales based on max date value minus 6 months

I'm trying to filter the data based on the date filter. The date column in my table is in date9. format(30JUN2017).
I want to filter the date column by subtracting 6 months from the existing date, which will be 31DEC2017.
e.g: date<31DEC2017
Can anyhow suggest on how to do this, I have tried using intx function and other options as well.
Thanks for your help.
Best Regards,
AJ
When manipulating dates in SAS using their formatted values you should enclose them in ""d, i.e. in your example it should be where date<"31DEC2017"d. If your date is stored in date9 format as a string, you can make it into a date like so: where input(date,date9.)<"31DEC2017"d
Now your question seems to differ slightly from its title. According to the latter, you want to filter on max_date - 6 months, whatever that max date might be in your dataset. This could be done so:
proc sql;
select *
from t
where intnx('month',date,6)<(select max(date) from t)
;
quit;
If however, for some strange reason, by "max date" you mean the current date, the above query simply becomes this:
proc sql;
select *
from t
where intnx('month',date,6)<date()
;
quit;

how to change the first day of the week in PostgreSQL

I need to change the week start date from Monday to Saturday in PostgreSQL. I have tried SET DATEFIRST 6; but it doesn't work in PostgreSQL. Please suggest solution for this.
It appears that DATEFIRST is a thing in Microsoft Transact-SQL.
I don't believe Postgres has any exact equivalent, but you should be able to approximate it.
Postgres supports extracting various parts of a TIMESTAMP via the EXTRACT function. For your purposes, you would want to use either DOW or ISODOW.
DOW numbers Sunday (0) through Saturday (6), while ISODOW, which adheres to the ISO 8601 standard, numbers Monday (1) through Sunday (7).
From the Postgres doc:
This:
SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-18 20:38:40');
Returns 0, while this:
SELECT EXTRACT(ISODOW FROM TIMESTAMP '2001-02-18 20:38:40');
Returns 7.
So you would use a version of EXTRACT in your queries to get the day of the week number. If you're going to use it in many queries, I would recommend creating a function which would centralize the query in one spot, and return the number transposed as you would like such that it started on Saturday (the transposition would vary depending on which numbering method you used in EXTRACT). Then you could simply call that function in any SELECT and it would return the transposed number.
You can just add +x, where x is the offset between postgres dow and what you want to achieve.
For example:
You want Sunday to be the first day of the week. 2018-06-03 is a Sunday and you want to extract the dow of it:
SELECT EXTRACT(DOW FROM DATE '2018-06-03')+1;
returns 1
That is probably the reason why postgres dow yields 0 for Sunday. Would it be 7, then
SELECT EXTRACT(DOW FROM DATE '2018-06-03')+1;
would return 8.

Convert a Date field into Week Number in IBM DB2

I have a Date field (CHAR Datatype) which has values in this format: YYYYMMDD.
For example 20140729.
I want to convert it into a Weeknumber in format YYYYWKNO
For example, the result would be 201432.
How this can be done in IBM DB2?
Luckily for you, DB2 has some pretty good date formatting functions. Although the links for the documentation are for DB2 for Linux/Unix/Windows, it will also work on DB2 for z/OS.
You can use TIMESTAMP_FORMAT() to convert your CHAR field to an actual date, which you can then use VARCHAR_FORMAT() to format it in the way you wish:
SELECT
VARCHAR_FORMAT(
TIMESTAMP_FORMAT(
'20140801'
,'YYYYMMDD'
)
,'YYYYWW'
)
FROM SYSIBM.SYSDUMMY1
There are two different formats for "week", one is WW, which will give the week based on a week beginning with January 1 and ending January 7, and IW, which will give the ISO Week.. Please see the documentation page for VARCHAR_FORMAT for the other formats available.
The following gets week 31 instead of 32. But overall I think it is a good solution for this problem:
SELECT TO_DATE('20140801', 'YYYYMMDD') AS MYDATE
, YEAR(TO_DATE('20140801', 'YYYYMMDD')) * 100 + WEEK(TO_DATE('20140801', 'YYYYMMDD')) AS YYYYWKNO
FROM SYSIBM.SYSDUMMY1