sys.time_zone_info's current_utc_offset in minutes format - sql-server

In sys.time_zone_info of MS-SQL server current_utc_offset is stored in Hour Format(HH:MM).
There is any possible way to convert that time into Minutes format?
Actual Result
name | current_utc_offset
---------------------------------------------------------
Syria Standard Time | +02:00
----------------------------------------------------------
Expected Result
name | current_utc_offset | Minute
----------------------------------------------------------------
Syria Standard Time | +02:00 | +120
----------------------------------------------------------------

Simplest way (avoiding creative string manipulation) is to (ab)use SWITCHOFFSET:
SELECT
name,
current_utc_offset,
DATEPART(TZOFFSET, SWITCHOFFSET('', current_utc_offset)) AS current_utc_offset_minutes
FROM sys.time_zone_info
If your really must have the sign with that in all cases and you can't handle that client-side (which I'd consider a better option), then FORMAT is your friend:
SELECT
name,
current_utc_offset,
FORMAT(DATEPART(TZOFFSET, SWITCHOFFSET('', current_utc_offset)), '+0;-0') AS current_utc_offset_minutes
FROM sys.time_zone_info
There are other approaches (AT TIME ZONE could be used on the time zone name, for example).

Related

How to construct timestamp_tz from timestamp_ntz using time zone names

In Snowflake how do I combine a time zone name with a timestone_ntz field to create a timestamp_tz value?
In Oracle it's:
SELECT from_tz(<timestamp_col>, <timezone_name>)
FROM <my_table>;
All the Snowflake examples I have seen seem to require me to specify the time zone as an offset in terms of hours and/or set a session parameter.
I don't want to have to specify time zone offsets in hours. I don't wish to use session parameters as I want to control the loading at field/record, not session, level.
I think something along the lines of this does what I want but it seems there must be a better way:
SELECT convert_timezone('Europe/London', 'Europe/London', <timestamp_ntz_col>);
I also am aware of the timestamp_tz_from_parts() function, but that seems a bit 'heavy'.
Is there a simpler way?
I also am aware of the timestamp_tz_from_parts() function, but that
seems a bit 'heavy'.
I don't think it's heavy, and you can create your own function to reduce the complexity of your main queries:
create or replace function mergetz( ts varchar, tz varchar )
returns timestamp_tz as
$$
TIMESTAMP_TZ_FROM_PARTS(
YEAR( ts::date ),
MONTH( ts::date ),
DAY( ts::date ),
HOUR( ts::datetime ),
MINUTE( ts::datetime ),
SECOND( ts::datetime ),
0, -- nanoseconds
tz )
$$
;
SELECT mergetz( ts, tz)
from values
('2019-01-01 12:00:00','America/New_York'),
('2019-01-01 12:15:00','Europe/London') tmp (ts, tz );
+-------------------------------+
| MERGETZ( TS, TZ) |
+-------------------------------+
| 2019-01-01 12:00:00.000 -0500 |
| 2019-01-01 12:15:00.000 +0000 |
+-------------------------------+

how to convert the time type of 2020-06-02 10:40:28.001 to 1591065628 in TDengine database?

how to convert the time type of 2020-06-02 10:40:28.001 to 1591065628 in TDengine database?
I want to convert the time in a certain format and can see it in the shell. Take an example, I want to convert the time 2020-06-02 10:40:28.001 to 1591065628, what should I do?
you can use taos -r option, it will output time as uint64_t
ubuntu#taos ~ $ taos -r
taos> use test;
Database changed.
taos> select * from tb;
ts | speed | desc |
================================================================
1644216894189 | 1 | test |
Query OK, 1 row(s) in set (0.006103s)

Extract a value from a ScheduledTask array in Powershell

everyone. I am newbie to Powershell. So your help is much appreciated.
My question is the following. I have a powershell small script that provides me with server Start, Stop, Kill ... tasks.
Here's the command
PS P:\> Get-ScheduledTask -TaskPath "*$someString*" | Get-ScheduledTaskInfo | Select TaskName,LastRunTime,NextRunTime
TaskName LastRunTime NextRunTime
-------- ----------- -----------
Archive 10/14/2017 7:00:00 AM 10/21/2017 7:00:00 AM
Kill 10/15/2017 12:50:50 PM 10/22/2017 12:50:50 PM
Restart 10/20/2017 12:00:00 AM 10/23/2017 12:00:00 AM
Start 10/15/2017 1:00:00 PM 10/22/2017 1:00:00 PM
Stop 10/13/2017 5:30:30 PM 10/20/2017 5:30:30 PM
I would like to extract the value of LastRunTime that corresponds to Start and the value of NextRunTime that corresponds to stop, and have them converted to a String.
Thank you.
Not very useful in the requested OP format, but here you go
C:\> $TaskNames = "Start","Stop"
C:\> $data = Get-ScheduledTask -TaskName $TaskNames | Get-ScheduledTaskInfo
C:\> [string]"$(($data | sort)[0].LastRunTime) $(($data | sort)[1].NextRunTime)"
10/20/2017 08:15:15 10/21/2017 09:16:16
this will only work right if your task names are actually in alphabetical order. Change the indexes (numbers in []) if the sort comes out in a different order.

Update Access database (multiple rows)

I am totally newbie to Access, I used to use Excel to handle my needs for a while.
But by now Excel has become too slow to handle such a big set of data, so I decided to migrate to Access.
Here is my problem
My columns are:
Number | Link | Name | Status
1899 | htto://example.com/code1 | code1 | Done
2 | htto://example.com/code23455 | code23455 | Done
3 | htto://example.com/code2343 | code2343 | Done
13500 | htto://example.com/code234cv | code234cv | Deleted
220 | htto://example.com/code234cv | code234cv | Null
400 | htto://example.com/code234cv | code234cv | Null
So I want a way to update Status of my rows according to numbers list.
For example I want to update Status column for multiple numbers to become Done
Simply I want to update "Null status" to become "Done" according to this number list
13544
17
13546
12
13548
13549
16000
13551
13552
13553
13554
13555
12500
13557
13558
13559
13560
30
13562
13563
Something like this
I tried "update query" but I don't know how to use criteria to solve this problem
In Excel I did that by "conditional formatting duplicates" -with my number list which I wanted to update-
Then "sort by highlighted color" then "fill copy" the status with the value
I know that Access is different but I hope that there is a way to do this task as Excel did.
Thanks in advance
From my understanding, You can try
Update TblA
Set TblA.Status="Done"
where Number in (13544,17,13546,....)
Or alternatively easy method is to pull these numbers in IN clause into its own table and use it like this
Update TblA
Set TblA.Status="Done" where Number in (select NumCol from NumTable )
or this solution may help you Here

How to get last access/modification date of a PostgreSQL database?

On development server I'd like to remove unused databases. To realize that I need to know if database is still used by someone or not.
Is there a way to get last access or modification date of given database, schema or table?
You can do it via checking last modification time of table's file.
In postgresql,every table correspond one or more os files,like this:
select relfilenode from pg_class where relname = 'test';
the relfilenode is the file name of table "test".Then you could find the file in the database's directory.
in my test environment:
cd /data/pgdata/base/18976
ls -l -t | head
the last command means listing all files ordered by last modification time.
There is no built-in way to do this - and all the approaches that check the file mtime described in other answers here are wrong. The only reliable option is to add triggers to every table that record a change to a single change-history table, which is horribly inefficient and can't be done retroactively.
If you only care about "database used" vs "database not used" you can potentially collect this information from the CSV-format database log files. Detecting "modified" vs "not modified" is a lot harder; consider SELECT writes_to_some_table(...).
If you don't need to detect old activity, you can use pg_stat_database, which records activity since the last stats reset. e.g.:
-[ RECORD 6 ]--+------------------------------
datid | 51160
datname | regress
numbackends | 0
xact_commit | 54224
xact_rollback | 157
blks_read | 2591
blks_hit | 1592931
tup_returned | 26658392
tup_fetched | 327541
tup_inserted | 1664
tup_updated | 1371
tup_deleted | 246
conflicts | 0
temp_files | 0
temp_bytes | 0
deadlocks | 0
blk_read_time | 0
blk_write_time | 0
stats_reset | 2013-12-13 18:51:26.650521+08
so I can see that there has been activity on this DB since the last stats reset. However, I don't know anything about what happened before the stats reset, so if I had a DB showing zero activity since a stats reset half an hour ago, I'd know nothing useful.
PostgreSQL 9.5 let us to track last modified commit.
Check track commit is on or off using the following query
show track_commit_timestamp;
If it return "ON" go to step 3 else modify postgresql.conf
cd /etc/postgresql/9.5/main/
vi postgresql.conf
Change
track_commit_timestamp = off
to
track_commit_timestamp = on
Restart the postgres / system
Repeat step 1.
Use the following query to track last commit
SELECT pg_xact_commit_timestamp(xmin), * FROM YOUR_TABLE_NAME;
SELECT pg_xact_commit_timestamp(xmin), * FROM YOUR_TABLE_NAME where COLUMN_NAME=VALUE;
My way to get the modification date of my tables:
Python Function
CREATE OR REPLACE FUNCTION py_get_file_modification_timestamp(afilename text)
RETURNS timestamp without time zone AS
$BODY$
import os
import datetime
return datetime.datetime.fromtimestamp(os.path.getmtime(afilename))
$BODY$
LANGUAGE plpythonu VOLATILE
COST 100;
SQL Query
SELECT
schemaname,
tablename,
py_get_file_modification_timestamp('*postgresql_data_dir*/*tablespace_folder*/'||relfilenode)
FROM
pg_class
INNER JOIN
pg_catalog.pg_tables ON (tablename = relname)
WHERE
schemaname = 'public'
I'm not sure if things like vacuum can mess this aproach, but in my tests it's a pretty acurrate way to get tables that are no longer used, at least, on INSERT/UPDATE operations.
I guess you should activate some log options. You can get information about logging on postgreSQL here.

Resources