How to Convert Date '2022-12-01' to 'December 2022' - sql

Like the title asks, how do you convert Date '2022-12-01' to 'December 2022' in Microsoft SQL? Or vice versa, which one is easier? I'm trying to do an INNER JOIN between two data sources that use the different date formatting.

select format(CAST('2022-12-01' AS DATE),'MMMM yyyy')

Related

Date_Trunc and To_Date Questions SQL

Can we use date_trunc for a date (not date-time) that we are trying to "truncate" (not sure if the term can be applied here) to e.g. the start of the week? So if I have date_trunc(week, 28/10/2020) and I want to get the start of the week that 28th of October lies in (so 26th of October)? I tried this in my SQL command line but I get error messages.
If I am doing: SELECT to_date ('02 Oct 2001', 'DD Mon YYYY'); How can I ensure the resulting format is in a date format I specify (rather than the default date format)? For example if I want it in format DD-MM-YYYY?
select to_char(date '2017-06-02', 'MM') < in this example, why do we need "date" for this to work? The general format for to_char should be TO_CHAR (timestamp_expression, 'format'). I don't see in this general format that we need "day".
if I have a WHERE filter like to_char(order_date, '20-10-2020'), and there are indeed some rows with this order date, will these rows still show in my results (after running query) if these rows are in DATE format (so 20 Oct is in date format) as opposed to string (which is what I am filtering by as I am doing to_char). I know there would be no need to use to_char in this case but just asking..
yes, you can use date in text form but you have to cast it to a correct type
these queries will work
select date_trunc('week', '2020-10-28'::date);
select date_trunc('week', '10/28/2020'::date);
-- as well as
select date_trunc('week', '2020-10-28'::datetime);
and return timestamp 2020-10-26 00:00:00.000000
note, next query
select date_trunc('week', '28/10/2020'::date);
will fail with error date/time field value out of range: "28/10/2020";
You can use to_char, it returns text, if you need a date format you have to case it again
select to_char( to_date ('02 Oct 2001', 'DD Mon YYYY'), 'DD-MM-YYYY')::date;
select to_char('02 Oct 2001'::date, 'DD-MM-YYYY')::date;
'2017-06-02' is a text and it can't be automatically converted to timestamp. Actually I don't know a text format which can.
No, you need to explicitly cast into date type to use it as a filter
where order_date = date_stored_as_a_text::date
I am answering the questions in a different order as there are some wrong assumptions:
Question 3
There is a general difference between '2017-06-02' and date '2017-06-02' - the first one is just a varchar, a string, NOT handled as a date by Redshift, the 2nd one tells Redshift to handle the string as date and therefore works.
Question 2
A date data type column has no format - you may an sql client that can display date columns in different formats, however, this is not a functionality of redshift. SELECT to_date ('02 Oct 2001', 'DD Mon YYYY'); tells redshift to convert the string '02 Oct 2001' to date.
Question 1
DATE_TRUNC('datepart', timestamp) also supports week as datepart - see Date parts for date or timestamp function (Also shown in the example of AWS). You should also be able to provide a date instead of a timestamp.
Question 4
to_char(order_date, '20-10-2020')is not a filter and you are using it wrong.
AWS TO_CHAR
TO_CHAR converts a timestamp or numeric expression to a character-string data format.
I guess you are rather looking for:
where to_char(order_date, 'YYYY-MM-DD') = '20-10-2020'

Oracle Sql convert FROM short date TO string or long date

I'm reading a column that's a short date that I need to convert to a long date or a string.
Excel can do this readily , but I can't find out how to do it with Oracles SQL
Examples of the DATETIME column:
41590.6753233101
41593.7843996875
41593.7844002199
41593.7844007638
I would like to convert this to a human readable date.
Hate to have to direct someone move the out put to excel if you want to see the date.
Excel stores dates as the number of days since January 1, 1900, so to convert an Excel date, you just add them.
with mydates as (select 41590.6753233101 as datetime from dual
union select 41593.7843996875 from dual
union select 41593.7844002199 from dual
union select 41593.7844007638 from dual)
select datetime, DATE '1899-12-30' + datetime
from mydates;
Output:
41590.6753233101 11/12/2013 4:12:28 PM
41593.7843996875 11/15/2013 6:49:32 PM
41593.7844002199 11/15/2013 6:49:32 PM
41593.7844007638 11/15/2013 6:49:32 PM

Extract Month, Year (Netezza SQL)

My data type is a date formatted as "YYYY-MON-DD" and I would like to extract the month and year to be formatted as "MON YYYY" while keeping the data type as date so that I will be able to use it with the ADD_MONTHS function. Is there a way to do so? I extract the date from the data field called date_process.
This is what I thought of but it doesnt seem to be working.
SELECT TO_DATE(TO_CHAR(PROCESS_DATE,'YYYY-MON'), 'MON YYYY') AS PERIOD,
Thank you.
Dates are stored in an internal format, not as a string.
If you want to see the value in a particular format, then you need to convert it to a string. Hence, drop the final to_date():
SELECT TO_CHAR(PROCESS_DATE, 'MON YYYY') AS PERIOD,

Convert timestamp from numerical to date in full using SQL and/or Python

I'm trying to use an SQL query/clause to covert numerical timestamp data in a table i.e. 2016-07-31 19:54:22+00 into a written date i.e. July 31, 2016.
I only need the data, I don't want the time. Help with this would be appreciated. Thanks.
In case it's any help to anyone I just did:
SELECT to_char(time, 'Month DD, YYYY') AS date
*'time' being the name of the timestamp column in my database table.

How to format date in DD-MMM-YYYY format eg 29-JAN-2015?

I want the date in DD-MMM-YYYY format eg 29-JAN-2015.
I have tried with:
SELECT TRIM(TO_DATE('29 Jan 2015'
,'DD MON YY')) FROM DUAL
I got result as: 29-JAN-15
But I am expecting: 29-JAN-2015 in date format not in char format
Im assuming Oracle DB:
select to_char(SYSDATE, 'dd-Mon-yyyy') from dual
Returns
29-Jan-2015
Thanks for answers.
I got the solution. First we need to alter the session as below:
alter session set nls_date_format='DD-MON-YYYY';
then run the query:
SELECT TRIM(TO_DATE('29 Jan 2015'
,'DD MON YYYY'))
FROM DUAL
Now I got result as:29-JAN-2015
What you are doing is take the string '29 Jan 2015' and make it a date using the format 'DD MON YY'. This should fail of course for '2015' not matching 'yy', but Oracle is lenient here.
Then you use TRIM on the date. But TRIM is for strings. What happens is that you get shown '29 Jan 15'. I am getting shown '29.01.15' instead of the usual '29.01.2015'. However the behavior: Don't use TRIM on dates, its behavior is nowhere specified as far as I am aware. Use TO_CHAR to format a date in output.
If you only select a date without TO_CHAR you get the date shown in some standard format, which can be '29 Jan 2015' or '29 Jan 15' or '29.01.2015' or '01/29/2015' depending on the app you are using and possibly some setting therin.
For completeness sake:
TO_DATE takes a string ('29 Jan 2015' in your case) and converts it to a date. If the string contains names, make sure that you specify the appropriate language setting:
TO_DATE('29 Jan 2015', 'dd mon yyyy', ''NLS_DATE_LANGUAGE=AMERICAN')
To get a formatted string from a date, use TO_CHAR:
TO_CHAR(sysdate, 'dd MON yyyy', 'NLS_DATE_LANGUAGE=AMERICAN')
Usually you don't do that, however. You select a date as is and have your app (written in PHP, Java or whatever) care about how to display it appropriately to the user's computer's settings.
In SQL Server the query
select CONVERT(nvarchar, GETDATE(), 106) as [Converted Date]
returns:
29 Jan 2015
Manu is correct. Oracle publish a full list of date format specifiers.
http://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements004.htm#CDEHIFJA
Use CONVERT(VARCHAR(11),GETDATE(),106)
See detailed explanation here:
http://www.w3schools.com/sql/func_convert.asp
Can you try:
SELECT TRIM(TO_DATE(SYSDATE ,'DD MON YYYY')) FROM DUAL
YY will only show 2 ciphers, instead of YYYY that will show 4.