Convert date to dateTtime format in SQL - sql

I am trying to convert a date column (ie. 2012-10-02) to the first day of the year with time (ie. 2012-01-01T00:00:00) in sql.
Is there a way to do so in the SELECT query?

for BigQuery use below
select timestamp_trunc('2012-10-02', year)
with output
2012-01-01 00:00:00 UTC
Note - if you column is of date type - the output will be
2012-01-01T00:00:00
and finally, you can use datetime_trunc instead of timestamp_trunc and you will get expected result - 2012-01-01T00:00:00

Look at the YEAR() function.
It would allow you to extract just the year, and then just as the date and time you need.

Related

How do I extract a date (dd-mm-yy) from a timestamp with timezone (timestamptz) in postgresql

My date column "timestamp" is currently listed as:
2020-11-16 20:27:38.033 +0000
It's formatted as timestamptz and I've tried every search on here and google to find a method to only pull the date part (in this example 2020-11-16) from the column so I can effectively start grouping data by Date.
Any help would be greatly appreciated.
Assuming (as you haven't stated) that the column is a string. This shows how to convert:
postgres=# SELECT ('2020-11-16 20:27:38.033 +0000'::timestamp)::date;
date
------------
2020-11-16
If it were already a timestamp, then just the ::date cast would work.
You can use ::DATE casting or use TO_CHAR() conversion if the aim is just to display in that format
such as
SELECT your_ts_column::DATE AS val_as_date,
TO_CHAR(your_ts_column, 'YYYY-MM-DD') AS val_as_str
FROM your_table
Demo

BigQuery convert String to Date

In my dataset, one column called timestamp was created with datatype as String.
It contains values like:
2018-05-30T12:56:27:487+0200
I want to construct a query where I can fetch all column from the dataset table based on the date in 'YYYY-MM-DD' format.
I want to use it in where clause with DATE Range between.
Can you guide?
Thank you.
convert String to Date
Below example for BigQuery Standard SQL
#standardSQL
WITH `project.dataset.table` AS (
SELECT '2018-05-30T12:56:27.487+0200' ts UNION ALL
SELECT '2018-05-30T01:56:27.487+0200'
)
SELECT ts AS ts_as_string,
PARSE_TIMESTAMP('%FT%H:%M:%E3S%z', ts) ts_as_timestamp,
DATE(PARSE_TIMESTAMP('%FT%H:%M:%E3S%z', ts)) ts_as_date
FROM `project.dataset.table`
with result
ts_as_string ts_as_timestamp ts_as_date
2018-05-30T12:56:27.487+0200 2018-05-30 10:56:27.487 UTC 2018-05-30
2018-05-30T01:56:27.487+0200 2018-05-29 23:56:27.487 UTC 2018-05-29
As you can see - first i am parsing timestamp out of the string - this is important part - this is where you can take timezone into account (see difference in dates in the result 2018-05-29 vs. 2018-05-29). Then you can get Date out of TIMESTAMP
I want to use it in where clause with DATE Range between.
So, now you can use below in your WHERE clause
WHERE DATE(PARSE_TIMESTAMP('%FT%H:%M:%E3S%z', ts)) BETWEEN date1 AND date2
Update
You can use below to avoid dealing with "wrong" format
PARSE_DATE('%F', SUBSTR(ts, 1, 10))
In case if you need to account for timezone - you can use below (which fix : to . before applying PARSE_TIMESTAMP)
DATE(PARSE_TIMESTAMP('%FT%H:%M:%E3S%z', FORMAT('%s.%s', SUBSTR(ts, 1, 19), SUBSTR(ts, 21, 8))))
If you want the date in the same timezone represented, then the simplest method is to use string operations and convert to a date:
select PARSE_DATE('%Y-%m-%d', SUBSTR('2018-05-30T12:56:27:487+0200', 1, 10))

SQL Solr query Convert date to

I'm interested in the question: how to convert date to number in millis with Solr SQL? Is it possible?
You have to use Function Queries (https://lucene.apache.org/solr/guide/6_6/function-queries.html)
For example: in the field returned by your query, just insert ms(2000-01-01T00:00:00Z) or ms(mydatefield)
http://localhost:8983/solr/job/select?fl=ms(2000-01-01T00:00:00Z)&indent=on&q=:&wt=json
result: 946684800000
Obs2: Dates are relative to midnight, January 1, 1970 UTC (you can use function queries and calculate milliseconds between to dates)
Obs1: your date field type (mydatefield in the above example) should be a TrieDateField

Hive date conversion

I intended to extend certain records in a table by adding 366 days to its date keys:
to_date(date_add(from_unixtime(unix_timestamp('20150101' ,'yyyyMMdd'), 'yyyy-MM-dd'), 366)) as new_date
2016-01-01
But how to convert this value back to format of original key i.e. 20160101 ?
Since your requested date is 2016-01-01 it seems you want to add 365 days and not 366.
select from_unixtime(unix_timestamp(date_add(from_unixtime(unix_timestamp(
'20150101','yyyyMMdd')),365),'yyyy-MM-dd'),'yyyyMMdd');
Demo
hive> select from_unixtime(unix_timestamp(date_add(from_unixtime(unix_timestamp(
> '20150101','yyyyMMdd')),365),'yyyy-MM-dd'),'yyyyMMdd');
OK
20160101
date_add gives you a date type as output.
As you have already used from_unixtime and unix_timestamp, I'll assume that you are already aware of their functionalities.
In Hive/Impala, there's no native DATE_FORMAT function like MySQL/MariaDB, so you'll have to convert the output of your date_add to unix_timestamp and then use from_unixtime on the output to achieve the desired format.
Something along the lines of:
select
from_unixtime(unix_timestamp(
date_add(from_unixtime(unix_timestamp('20150101' ,'yyyyMMdd')), 365)),
'yyyyMMdd');

Convert date value to PST for comparison:Oracle

I have 2 questions:
I want to compare a field whose data type is "Date" against a given date. The DB is oracle and being a mysql guy I'm finding it difficult to come up with simple queries.
The field("date_closed") stores date in UTC format (24-Aug-2011 18:55:11 for example) and I want to convert it to PST for comparison.
I tried this query but it returns some extra rows in the data set(obviously):
select * from table1 where trunc(date_closed)=to_date('2011-08-24','yyyy-mm-dd')
How do I covert to PST format before comparison?
In the same query how do I compare "date_closed" against the current date?
You need the NEW_TIME function
Dates don't include timezone in Oracle, and are assumed to be in the database timezone (which may by UTC but probably isn't). You should look at the TIMESTAMP WITH TIMEZONE data types.
Also, bear in mind that if you are comparing to the current date - I assume that you want to strip off the timestamp and compare only the day.
So, if new_time(date_closed,'GMT','PST') translates the date , your where clause will be comparing something like
trunc(new_Time(date_closed,'GMT','PST')) = trunc(sysdate)
to get all records with date_closed on the current day in PST.