how to get a date in "YYYY-MM" format? [duplicate] - sql

This question already has answers here:
YYYY-MM column type in PostgreSQL
(2 answers)
Closed 2 years ago.
I have a column with date format like YYYY-MM-DD HH:MM:SS. I need to convert this column to the YYYY-MM format. What do I need to do for this?
I tried use DATE_TRUNC('MONTH', date), it works but not that way I want to. For example, I have a date 2018-11-14T00:00:00.000Z and when I apply DATE_TRUNC('MONTH', date_column), I get 2018-10-31T23:00:00.000Z. I expected 2018-11-31T00:00:00.000 or 2018-11-011T00:00:00.000 or 2018-11. Please, help.

Assuming that column is defined as timestamp or timestamptz (which it really should be) then you can use to_char() to format the output:
select to_char(the_timestamp_column, 'yyyy-mm')
from the_table;

Related

How to Select only Date Hour and Minutes from 2017-09-27 15:39:36.000 [duplicate]

This question already has answers here:
A way to extract from a DateTime value data without seconds
(9 answers)
Closed 4 years ago.
I have a column in my table (having value- 2017-09-27 15:39:36.000).
I want to select only Date, Hour, Minutes (i.e:- 2017-09-27 15:39) from column.
Can anyone please tell me how to select.?
Use the FORMAT function to format the date:
SELECT FORMAT(CAST('2017-09-27 15:39:36.000' AS DATETIME), 'yyyy-MM-dd HH:mm')
-- 2017-09-27 15:39
Here is a list of available format specifiers.
One option would be to use CONVERT with a mask matching the format you want:
SELECT CONVERT(varchar(16), GETDATE(), 120)
2018-09-28 07:58
Demo
We convert to varchar(16) explicitly so that only the hour and minute components are retained.
Use the datepart function which can extract specific parts of the date value.
https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-2017

Converting string into date does not return the date in specific format [duplicate]

This question already has answers here:
change date format 'yyyy/mm/dd' to 'mm-dd-yyyy' in Oracle
(2 answers)
Where to change the NLS_DATE_FORMAT in oracle 11g?
(1 answer)
Oracle NLS_DATE_FORMAT not working properly
(3 answers)
Closed 5 years ago.
I'm working on Oracle. As you can understand from the title, the problem is in this code:
select TO_DATE( '21991231', 'YYYYMMDD' ) from dual;
And the result is:
31/12/2199
I want to achieve this result:
21991231
Why is Oracle doing this? How can escape this problem?
The date doesnt have a format in Oracle. It's your client.
If you want to modify the default format in the current session, you can use:
alter session set nls_date_format='yyyymmdd'
If you want to convert the date into a format on adhoc basis, you can use to_char with format string:
select to_char(date_col, 'yyyymmdd') from table;

Convert date to MM//DD HH/MM [duplicate]

This question already has answers here:
Sql Server string to date conversion
(16 answers)
Closed 5 years ago.
How do you convert date to MM/DD HH/MM
example: 04-05 12:42
Since you are on SQL Server 2012, use FORMAT:
SELECT FORMAT(GETDATE(), 'MM-dd HH:mm')
Try using the MONTH, DAY, HOUR, MINUTE functions in T-SQL and concatenate your results into a string form like:
CONCAT(STRING(HOUR(created_at)), ':', RIGHT(STRING(MINUTE(created_at)), 3)) AS hour_min
Where the created_at is your timestamp column. Hope this helps!

Show date with time when querying oracle DATE data type [duplicate]

This question already has answers here:
Oracle. How to output date and time?
(5 answers)
Closed 6 years ago.
When querying an oracle date, I see only the date - not the time. Example
select D from ALIK_TZ
Result:
D
01-JUN-16
How can I see the time as well?
Use TO_CHAR with time format. Example below.
Consider the date being saved in column named D, than the following query would get the time as well:
select to_char(D, 'DD-MON-YYYY HH24:MI:SS') D_TIME, D from ALIK_TZ;
Result:
D_TIME D
01-JUN-2016 06:14:04 01-JUN-16
Since you have answered it yourself. Just to add the reason as why you are getting the result in the format DD-MON-YY format. Oracle docs says:
For input and output of dates, the standard Oracle date format is
DD-MON-YY
Also note that Oracle uses the TO_CHAR function internally whenever a DATE value is displayed, Oracle will call TO_CHAR automatically using the default DATE format which is DD-MON-YY.
Hence you need to change it using the TO_CHAR function to get the date in the format which you want.

Convert Date format while retaining datatype - Oracle [duplicate]

This question already has an answer here:
Formatting DATE in oracle
(1 answer)
Closed 7 years ago.
Need to acquire YYYY-MM of SYSDATE and load to column (Data_Type = Date). The below query gives the required result, however, being convert to string datatype in the process.
SELECT TO_CHAR(SYSDATE, 'YYYY-MM') FROM DUAL;
Any way to retain the Date datatype & acquire SYSDATE as YYYY-MM by purely using oracle
Dates don't have formats, so you can't set the format when it is saved. Formats only come into play when converting to or from a string.
If you want to save just the month year with day and time being 1 and midnight, trunc(date_value, 'month'). Fomat models for round and trunc date.
Convert it back to a date.
SELECT to_date(TO_CHAR(SYSDATE, 'YYYY-MM'),'YYYY-MM') FROM DUAL
Of course, to be a valid date there must be a day component. This will be set to 1.
Alternatively, why not just store the sysdate? Next time you look at that value you will only be interested in the year and month.