Convert String into Date Google Big Query - sql

I am trying to convert the String "181201" into a Date field in Google Big Query using standard SQL.
Ex. 181201 -> 2018-12-01
I have tried to use the following which worked because the original date I inputted had the entire year spelt out as "2018". However, I do not have the entire year written out in my data set.
SELECT PARSE_DATE('%YY%m%d', '20181201') AS DATE
How can I update my query to use only yymmdd to give me yyyy-mm-dd?
THANK YOU!

SELECT SAFE.PARSE_DATE('%y%m%d', '181201')

Related

How to convert DD-MMM-YY to YYYY/MM/DD in Big Query SQL

I've one date format as DD-MMM-YY in one of my Big Query table as a STRING column (e.g 31-OCT-20).
Now I need to convert the format to YYYY/MM/DD in Date format to insert data to another table (e.g 2020/10/31).
Please help with the required format for Google Big Query.
Thanks in advance.
It worked after trying a combination of PARSE_DATE and FORMAT_DATE:
SELECT FORMAT_DATE("%Y/%m/%d",PARSE_DATE('%d-%b-%y','31-OCT-20'))
Use PARSE_DATE:
SELECT PARSE_DATE('%d-%b-%y','31-OCT-20')

Extract year from timestamp in hive

I am writing the query to show the data entries for a specific year. Date is stored in dd/mm/yyyy hh:mm:ss.(Date TIMESTAMP - e.g. 12/2/2014 0:00:00).
I am trying to display the two columns(name, orderdate) filtered by a specific year(year from orderdate). The requirement is to enter the specific year(2010 or 2020 etc) not the entire date. I tried using date_format() and regexp_replace() with WHERE but nothing helped.
Can someone help me?
If your are storing the date -- incorrectly -- as a string, then you can use string functions to do what you want:
where orderdate like '__/__/2010%'
However, you should really put your effort into storing the date using a correct format -- YYYY-MM-DD for strings at least.

Convert YYYYMMDD to MM/DD/YYYY in Snowflake

I need help in figuring out the date conversion logic in Snowflake. The documentation isn't clear enough on this.
In SQL Server, I would try
SELECT CONVERT(DATE, '20200730', 101)
and it gives me '07/30/2020'.
If I try the following in Snowflake,
to_varchar('20200730'::date, 'mm/dd/yyyy')
it gives me '08/22/1970'. Why would it give an entire different date? Need help in getting the logic with the correct date.
The issue with what you are doing is that you are assuming that Snowflake is converting your string of '20200730'::DATE to 2020-07-03. It's not. You need to specify your input format of a date. So, 2 options based on your question being a bit vague:
If you have a string in a table and you wish to transform that into a date and then present it back as a formatted string:
SELECT TO_VARCHAR(TO_DATE('20200730','YYYYMMDD'),'MM/DD/YYYY');
--07/30/2020
If the field in the table is already a date, then you just need to apply the TO_VARCHAR() piece directly against that field.
Unlike SQL Server, Snowflake stores date fields in the same format regardless of what you provide it. You need to use the TO_VARCHAR in order to format that date in a different way...or ALTER SESSION SET DATE_OUTPUT_FORMAT will also work.
Try select to_varchar(TO_DATE( '20200730', 'YYYYMMDD' ), 'MM/DD/YYYY'); which produces 2020-07-30
You may need to refer to https://docs.snowflake.com/en/user-guide/date-time-input-output.html#timestamp-formats

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 ...

MS Access - Select Char as Date and doing a date diff

I have two columns. ColA and ColB contains char(10) with data "20090520" and "20090521".
I want to select and get the date difference in days. I have tried using Format() and CDate()
but MS Access always display as #ERROR.
Access prefers its dates in this format:
#2009-12-01#
You can convert your date to something Access understands with:
CDate(Format([ColA], "0000-00-00"))
Or alternatively:
DateSerial(Left([ColA],4),Mid([ColA],5,2),Right([ColA],2))
And to display the result in your preferred format:
Format(<date here>, "dd-mm-yyyy")
Try using DateSerial() to convert the dates:
DateSerial(Left([FieldName],4),Mid([FieldName],5,2),Right([FieldName],2))
If at all possible, change the datatype to a date datatype. You should not store dates as character data.
I am connecting to another database which I have no control on. That is why this problem occurred. Thanks for the feedback.