How to remove trailing 0s and offset from Oracle db - database

I'm working with an Oracle database (version 12c) containing a date column with the dates in the format YYY-MM-DD H23:Mi:SS.
All the dates are stored in EST.
I'm converting these dates to UTC so 2021-09-23 09:12:22 would appear as 2021-09-23 14:12:22.
I have the following query:
SELECT my_date,
FROM_TZ(CAST(my_date AS TIMESTAMP), 'America/New_York') AT TIME ZONE 'UTC' AS my_date_utc
FROM my_table
Here is a sample of my results:
my_date my_date_utc
------------------- -------------------
2018-04-28 21:21:26 2018-04-29 01:21:26.000000 +00:00
2018-12-18 07:42:25 2018-12-18 12:42:25.000000 +00:00
How do I remove the ".000000 +00:00" from the results? So the result would appear as:
my_date my_date_utc
------------------- -------------------
2018-04-28 21:21:26 2018-04-29 01:21:26
2018-12-18 07:42:25 2018-12-18 12:42:25

To display a timestamp with time zone in whatever (valid) desired format, you need to use to_char. Something like this (not tested):
SELECT my_date,
to_char(
FROM_TZ(CAST(my_date AS TIMESTAMP), 'America/New_York')
AT TIME ZONE 'UTC', 'yyyy-mm-dd hh24:mi:ss') AS my_date_utc
FROM my_table

Related

How to get a weekly view from date field in snowflake

I have a small query mentioned below.
Need to break down a date field "DAY" to monthly and weekly wise in snowflake.
Input:
DAY
-------
2022-06-09
2022-04-04
Output
DAY_MONTH
----------
2022-06-01
2022-04-01 Monthly wise--- Its done
Here I have used
DATE_FROM_PARTS( YEAR(DAY), MONTH(DAY), 1) AS DAY_MONTH
DAY_WEEK
----------
2022-06-06
2022-04-04
They should be first day of working days like (Monday). How to do that for a weekly view?
I think you're looking for the date_trunc function:
set ts = '2022-07-07 11:14:00'::timestamp;
select date_trunc('DAY', $TS);
select date_trunc('WEEK', $TS);
select date_trunc('MONTH', $TS);
This is showing for a timestamp to show how it truncates to the day, but it works the same way for date types. Truncating to the week will start by the week_start parameter that's in effect (it will default to Monday as the start for this function):
https://docs.snowflake.com/en/sql-reference/parameters.html

Serilog - Timestamp in log table has timezone, but ignored by SQL Server

When I cast TimeStamp as a smalldatetime, T-SQL seems to ignore the timezone, so it appears that +00:00 events happened in the future.
What is the proper way to handle the timezone in a timestamp field in a SQL statement?
Example:
2021-09-16 13:05:00.3432122 +00:00 returns 2021-09-16 13:05:00 when cast as smalldatetime, when it actually happened at 9:05am.
2021-09-16 09:05:08.7375111 -04:00 returns the correct time.
SELECT TOP(20)
Id, TimeStamp,
CAST(TimeStamp AS smalldatetime) AS TimeStampx
FROM
SerilogLogs
WHERE
Level = 'Error'
ORDER BY
Id DESC;
Results show the +00:00 timestamps haven't had their hour adjusted when cast as smalldatetime: https://i.imgur.com/MweCHGt.png
Looks like that Timestamp colum uses the DATETIMEOFFSET type. So you may want to use the SWITCHOFFSET function before casting it to SMALLDATETIME:
SELECT TOP(20)
Id, TimeStamp,
CAST(SWITCHOFFSET(TimeStamp, '-04:00') AS smalldatetime) AS TimeStampx
FROM
SerilogLogs
WHERE
Level = 'Error'
ORDER BY
Id DESC;

How to convert string to date format when string has only two digits for a year (Oracle, Netezza)

I have this issue databases: on Oracle and Netezza.
ISSUE: I have a string: 16101211213. This string means: YYMMDDHH24MMSS. I need to convert it to date format YYYY-MM-DD HH24:MM:SS. So on the way I need to add the two digits (in front of the string). I know that dates are for XXI century. So I need to 20 at the begining.So in result I should get 2016-10-12 21:12:13
Can anybody help me with it? I have tried many options (mainly on Netezza) but could not figure it out.
Thanks a lot in advance!
RR might be your savior.
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> with test as (select '16101211213' datum from dual)
2 select to_date(datum, 'rrmmddhh24miss') result
3 from test;
RESULT
-------------------
12.10.2016 11:21:03
SQL>
Oh, and - be careful! You used wrong format mask for minutes; it is "MI", not "MM" (which is a month).
[EDIT: displaying result using a different format mask]
SQL> with test as (select '16101211213' datum from dual)
2 select
3 to_date(datum, 'rrmmddhh24miss') result_1,
4 to_char(to_date(datum, 'rrmmddhh24miss'), 'mm/dd/yyyy hh:mi:ss am') result_2
5 from test;
RESULT_1 RESULT_2
------------------- ----------------------
12.10.2016 11:21:03 10/12/2016 11:21:03 AM
SQL>
You can either reply on Oracle inferring the year by converting with the YY or RR format model elements, or concatenate the century value and use YYYY.
If you are really sure that the dates are all in the 21st century then using concatenation and YYYY:
to_date('20' || tistamp, 'yyyymmddhh24miss')
will behave the same as using YY (which uses the current date to decide the century):
to_date(tistamp, 'yymmddhh24miss')
and if all the years are below 50 then RR (which uses the current date's century or the last century depending on the supplied 2-digit year) will also get the same result:
to_date(tistamp, 'rrmmddhh24miss')
But if any of the values are 50 or above RR and YY/YYYY behave differently. As these seem to be event timestamps it's unlikely they will be in the future, but the difference may still matter one day. (But then, eventually, assuming 21st century might not be valid either...)
Quick demo of the difference, using your sample value and a couple of others, supplied via a CTE:
alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';
with your_table(tistamp) as (
select '16101211213' from dual
union all select '491231235959' from dual
union all select '500101000000' from dual
)
select to_date('20' || tistamp, 'yyyymmddhh24miss') as yyyy,
to_date(tistamp, 'yymmddhh24miss') as yy,
to_date(tistamp, 'rrmmddhh24miss') as rr
from your_table;
YYYY YY RR
------------------- ------------------- -------------------
2016-10-12 11:21:03 2016-10-12 11:21:03 2016-10-12 11:21:03
2049-12-31 23:59:59 2049-12-31 23:59:59 2049-12-31 23:59:59
2050-01-01 00:00:00 2050-01-01 00:00:00 1950-01-01 00:00:00
All of these would also work with to_timestamp() of course; as you don't have fractional seconds or time zone info using dates should be fine as long as your client knows that Oracle dates have a time component.

SQL Server: Find the number of substring occurrences of converted data

I have a huge amount of data and I want to count the number of occurrence for a certain column. What makes this confusing is that it is not just finding the same value of the column, but only part of it.
My table contains TimeStamp in format of (YYYY-MM-DD HH:MM:SS), and I want to apply this SQL command only to YYYY-MM-DD part and count the number of occurrence of each based on that. For example, if my data are as follows:
ID|TimeStamp
--+--------------------
00|2017-08-31 09:00:00
01|2017-08-31 11:00:00
02|2017-08-31 16:30:00
03|2017-08-31 22:00:00
04|2017-09-01 09:00:00
05|2017-09-01 23:40:00
06|2017-09-02 10:30:00
07|2017-09-02 13:00:00
08|2017-09-02 23:00:00
then I want my SQL command to output something like
TimeStamp | Occurrences
-----------+------------
2017-08-31 | 4
2017-09-01 | 2
2017-09-02 | 3
I have been trying to get there from what I have so far but I haven't had luck.
I have this SQL :
SELECT
TimeStamp, COUNT(*)
FROM
myTableName
GROUP BY
TimeStamp
ORDER BY
COUNT(*) DESC -- to sort the occurrence count
but this only counts exactly same timestamps, so this doesn't output what I want it to output. I am also thinking TimeStamp has DateTime type, so I had to convert it to varchar or something first and get the substring of it.
I tried converting the TimeStamp data type to Varchar, and then get the first 10 letters of the string, so
SELECT
COUNT(*) TimeStamp
FROM
myTableName
GROUP BY
(SELECT LEFT(CONVERT(Varchar, TimeStamp, 120), 10))
ORDER BY COUNT(*) DESC
but this causes an error:
Error: Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause
Can someone please help me with this? Thank you.
SELECT CAST(TimeStamp as date), COUNT(*)
FROM myTableName
GROUP BY CAST(TimeStamp as date)
ORDER BY 2 DESC

filter a sql statement using its column values

I have a table named 'Table1'.
It has 3 columns namely
1. vehicle Number
2. startdatetime
3. enddatetime
the table is as below->
vehicleno | startdatetime | enddatetime |
1 2013/07/16 00:00:00 2013/07/17 00:00:00
2 2013/07/16 00:00:00 2013/07/18 14:00:00
3 2013/07/17 12:19:00 2013/07/20 17:35:00
4 2013/07/19 10:24:56 2013/07/19 20:14:00
5 2013/07/15 08:10:00 2013/07/18 09:10:00
Now i want a o/p such that ,At the time of executing the query if the present datetime is between the startdatetime and enddatetime then the record should be displayed in my o/p table.
I have tried with the query..
select * from Table1 where startdatetime between '2013/07/17 00:00:00' and '2013/07/17 23:59:59' or enddatetime between '2013/07/17 00:00:00' and '2013/07/17 23:59:59'
but i didn't get the result i want.
Please help me out..
If you are looking for the current date why not use GETDATE?
Returns the current database system timestamp as a datetime value
without the database time zone offset. This value is derived from the
operating system of the computer on which the instance of SQL Server
is running.
Something like
select *
from Table1
where GETDATE() BETWEEN startdatetime AND enddatetime
SQL Fiddle DEMO
I would think you'd want to do:
select vehicleno ,startdatetime, enddatetime
from table1
where getUTCDate() between startdatetime and enddatetime
Then if you aren't in GMT you can do a dateAdd(hour, 5, getUTCDate()) if you are say EST.
It's always more correct to specify the columns you want to use in the select statement.
And at this point since we live in a global community I feel it's more appropriate to use getUTCDate() instead of just regular getDate() because getDate() just does the timezone that the server is located in and it will depend on where the server is if this works for your situation.
You can see the definition of GetUTCDate()
Try the following:
select *
from Table1
where GetDate() between startdatetime and enddatetime
GetDate() gets the current datetime
Just for the record, if you want to use literal dates in SQL, use ISO date format (YYYY-MM-DD hh:mm:ss). That is the only format guaranteed to be interpreted correctly regardless of your locale settings.

Resources