Date conversion in postgres - sql

I have something like this:
2010-09-14 00:00:00
and would like to show only the month/year or something like this in the example above:
9/10
I was able to extract the Year part by doing this
select extract (year from E.DISCHARGE_DATE)as YR_NB
but cannot get the 9/10 result that I would like to see in my output result.

select to_char(the_date_column, 'mm/yy') from the_table;
More details in the manual: http://www.postgresql.org/docs/current/static/functions-formatting.html

Related

How to convert Epoch to date in athena?

I have written a query to convert epoch to date conversion in Athena but the result is not expected
select from_unixtime(cast(epoch_values as bigint))as dates from mybdbase
result is :
dates
+54113-07-13 10:11:53.000
+54113-07-13 10:11:57.000
The year is shown in the above table, How to solve this?
Use this:
from_unixtime(event_timestamp/1000000)
You could validate the results with this page:
https://www.epochconverter.com/

How I can extract year from a date in sql and use it in where clause

I have a date column in a table which is named released_date and the format of its data is for example: 01-Jan-1995. I want to have just the years which are greater than 1997. Does anybody know how I can write such query?
if the date is in dd-MMM-yyyy format, you can use regexp_extract:
select regexp_extract (released_date, '(\\d{4})$',1) as year
Try this:
SELECT * FROM table_name WHERE year(released_date) > 1997

pg_query like with timestamp

I get 0 results if I query the timestamp column for entries that contain 2017 (or any other year).
The data in w_presse_erscheinung is a "timestamp without timezone"
and looks like this for example: 2017-06-01 00:00:00
I assume there is something wrong with the LIKE part of the query ...
My query looks like this:
$result = pg_query("
SELECT
name,
w_presse_link,
w_presse_erscheinung,
w_presse_quelle,
description
FROM adempiere.w_presse
WHERE isactive ='Y'
AND description='PS' AND w_presse_erscheinung LIKE '%2017%'
ORDER BY w_presse_erscheinung
DESC
")
Instead of relying on string conversions, you could explicitly extract the year from the timestamp:
EXTRACT(YEAR FROM w_presse_erscheinung) = 2017
You cannot use like with a timestamp field like that. Either typecast the timestamp field to varchar then use like or use extract function to match the year from the field.

Date in varchar and dd-mmm-yy format

I am stuck in this query. The below part "TRANSMITTED_DATE LIKE '17-JUL-14'" is hardcoded. I want to make it a generalized one.
I want to do something like this
WHERE CAST(TRANSMITTED_DATE AS DATE FORMAT 'DD-MMM-YY') BETWEEN
CAST(CURRENT_DATE AS DATE FORMAT 'DD-MMM-YY')
AND CAST(CURRENT_DATE AS DATE FORMAT 'DD-MMM-YY')-7
i.e the last seven days record from the last time this query is run against the table.
But, the transmitted date is VARCHAR field in the table and format is like 31-OCT-13 i.e DD-MMM-YY.
Please help me with the query.
SELECT
MLI_MDL_NUMBER as DOCUMENTUM_MLI
,TRANSMITTAL_NUMBER
,PROJECT_ID
,TRANSMITTED_DATE
FROM GEEDW_PLP_BULK_V.CDR_DOCUMENTUM_TRSMTL
WHERE
( TRANSMITTED_DATE LIKE '17-JUL-14' OR
TRANSMITTED_DATE LIKE '18-JUL-14' OR
TRANSMITTED_DATE LIKE '19-JUL-14' OR
TRANSMITTED_DATE LIKE '20-JUL-14' OR
TRANSMITTED_DATE LIKE '21-JUL-14' OR
TRANSMITTED_DATE LIKE '22-JUL-14' OR
TRANSMITTED_DATE LIKE '23-JUL-14' OR
TRANSMITTED_DATE LIKE '24-JUL-14' OR
TRANSMITTED_DATE LIKE '25-JUL-14'
);
Depending on a global system setting (Century Break in dbscontrol) or your Teradata release this might be easy.
If you're on TD14 you can utilize Oracle's TO_DATE:
WHERE TO_DATE(TRANSMITTED_DATE, 'dd-mon-yy')
BETWEEN CURRENT_DATE - 7 AND CURRENT_DATE
Otherwise try following query and check what's returned:
SELECT CAST('17-JUL-14' AS DATE FORMAT 'DD-MMM-YY')
If it's 2014-07-17 you can simply use
WHERE CAST(TRANSMITTED_DATE AS DATE FORMAT 'DD-MMM-YY')
BETWEEN CURRENT_DATE - 7 AND CURRENT_DATE
If it's 1914-07-17
WHERE CAST(SUBSTRING(TRANSMITTED_DATE FROM 1 FOR 7) || '20' ||
SUBSTRING(TRANSMITTED_DATE FROM 8) AS DATE FORMAT 'DD-MMM-YY')
BETWEEN CURRENT_DATE - 7 AND CURRENT_DATE
Of course all go them are bad because every row needs to be converted from string to date, which is a big overhead and you'll loose an existing statistics.
If this is a big table and you need to run that query often you should try to change the datatype to a DATE and do the typecast once during import.
And there's another problem: if there's any string representing a non-valid date your query will fail.

SQL query to convert Date to another format

Thanks for your help. I am not able to make out the type/format of the "Value" in a Date column.I guess its in Julian Date format.
The Column is paid_month and the values are below.
200901
200902
So,please help in writing SQL query to convert the above values(Mostly in Julian Format) in the Date Column to normal date (MM/DD/YYYY) .
Thanks
Rohit
Hi,
I am sorry for missing in giving the whole information.
1)Its a Oracle Database.
2)The column given is Paid_Month with values 200901,200902
3)I am also confused that the above value gives month & year.Day isnt given if my guess is right.
4)If its not in Julian format ,then also please help me the SQL to get at least mm/yyyy
I am using a Oracle DB and running the query
THANKS i GOT THE ANSWER.
**Now,i have to do the reverse meaning converting a date 01/09/2010 to a String which has 6 digits.
Pls help with syntax-
select to_char(01/01/2010,**
It looks like YYYYMM - depending on your database variant, try STR_TO_DATE(paid_month, 'YYYYMM'), then format that.
Note: MM/DD/YYYY is not "normal" format - only Americans use it. The rest of the world uses DD/MM/YYYY
For MySQL check
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
Example:
SELECT DATE_FORMAT(NOW(), '%d/%m/%Y')
For MySQL, you would use the STR_TO_DATE function, see http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date
SELECT STR_TO_DATE(paid_month,'%Y%m');
Sounds like the column contains some normal dates and some YYYYMM dates. If the goal is to update the entire column, you can attempt to isolate the YYYYMM dates and update only those. Something like:
UPDATE YourTable
SET paid_month = DATE_FORMAT(STR_TO_DATE(paid_month, '%Y%m'), '%m/%d/%Y')
WHERE LENGTH(paid_month) = 6
SELECT (paid_month % 100) + "/01/" + (paid_month/100) AS paid_day
FROM tbl;
I'm not sure about how oracle concatenates strings. Often, you see || in SQL:
SELECT foo || bar FROM ...
or functions:
SELECT cat (foo, bar) FROM ...