SQL insert first of month - sql-server

I've created an initial sql insert into a table, below.
spool_month spool_year spool_month_part spool_year_part curr_cnt prev_cnt cal_prcnt_dff
July 2020 7 2020 21069199 NULL 0
September 2020 9 2020 18072707 21069199 14
October 2020 10 2020 17284440 18072707 4
November 2020 11 2020 17791289 17284440 3
December 2020 12 2020 20148679 17791289 13
January 2021 1 2021 22543049 20148679 12
February 2021 2 2021 24234982 22543049 8
March 2021 3 2021 26458351 24234982 9
April 2021 4 2021 5946066 26458351 78
Can I create an insert that would only insert new data into this table at the first of the month? So, for example at first of every month insert previous months data.
Not sure where to start with this piece, so any feedback would be appreciated.

This is easily solved by using SqlAgent; if you are only using SqlExpress you won't have access to SqlAgent, in which case you will need to look at using the Windows Task Scheduler and Powershell, if that's the case here is a useful guide
Create your procedure that will perform your insert and schedule it to run daily either from a SqlAgent Job or Powershell from link above.
In your procedure, wrap your insert with
if DatePart(day,GetDate())=1
begin
...
end

Related

SQL Server, Incorrect syntax, Convert to datetime

Using ASP classic, I want to convert a date Tue, 21 Apr 2020 12:23:00 GMT to the format AAAA-MM-DD HH:MM:SS to insert it into a SQL Server 2008 database.
My code:
CONVERT('Tue, 21 Apr 2020 12:23:00 GMT', GETDATE(), 20)
I get this error:
Incorrect syntax to 'Tue, 21 Apr 2020 12:23:00 GMT'.
Note that dates in SQL Server don't actually have any internal string format. Rather, they are stored as binary. One option here would be to take the following substring of the your input timestamp, and then use TRY_CONVERT to marshall it over to a bona fide datetime inside SQL Server:
Tue, 21 Apr 2020 12:23:00 GMT <-- start with this input
21 Apr 2020 12:23:00 <-- convert this string to datetime
Sample code:
WITH yourTable AS (
SELECT 'Tue, 21 Apr 2020 12:23:00 GMT' AS dt
)
SELECT
dt,
TRY_CONVERT(datetime, SUBSTRING(dt, 6, LEN(dt) - 9)) AS dt_out
FROM yourTable;
Demo
Edit:
If you are using an earlier version of SQL Server, then you can use CONVERT with format mask 106, using the same substring as above:
SELECT
dt,
CONVERT(datetime, SUBSTRING(dt, 6, LEN(dt) - 9), 106) AS dt_out
FROM yourTable;

How to parse date with "mmmm d, YYYY h:M a" format in Google Data Studio

I have this date,
April 22nd 2020 12:34 am
April 21st 2020 3:14 am
April 22nd 2020 10:13 pm
April 23rd 2020 8:46 pm
April 21st 2020 2:32 am
April 21st 2020 2:22 am
I am replacing
"nd 2020" with ", 2020" and so on and bringing a date to this format using this formula,
REPLACE(REPLACE(REPLACE(Last Action At,'st 2020', ', 2020'),'th 2020', ', 2020'), 'nd 2020', ', 2020')
And storing it in "Date STR" field
April 22, 2020 12:34 am
April 21, 2020 3:14 am
April 22, 2020 10:13 pm
April 23, 2020 8:46 pm
April 21, 2020 2:32 am
April 21, 2020 2:22 am
But, further, this date is not accepted when I used the formula:
todate(Date STR,'%Y-%m-%d')
I even tried
todate(Date STR,'%m%m%m%m, %d, %Y%Y%Y%Y %h:%m %a'.'%Y-%m-%d')
0) Summary
Use either (#1 OR #2) of the below suggestions:
The updated answer makes use of the new PARSE_DATETIME function released in the 17 Sep 2020 Update to Google Data Studio;
The original post uses the Compatibility Mode Date and Time function, TODATE.
1) Update (PARSE_DATETIME)
Create the following PARSE_DATETIME Calculated Field:
PARSE_DATETIME("%B %d %Y %I:%M %p", REGEXP_REPLACE(Last Action At, "(.*\\d+)[a-z]+(.*)", "\\1 \\2"))
Added a New Page to the Report and a GIF to demonstrate:
2) Original Post (TODATE)
It can be achieved using the below combination of REGEXP_REPLACE and TODATE functions:
TODATE(REGEXP_REPLACE(Last Action At, "([A-Za-z]+ \\d+)[A-Za-z]+(.*)", "\\1 \\2"), "%B %d %Y %I:%M %p", "%Y%m%d%H%M")
Set the Date Type as required (for example):
Date YYYYMMDD
Date Hour YYYYMMDDhhmm
Date Hour Minute YYYYMMDDhhmm
Google Data Studio Report to elaborate:

Filter date from to ng-table AngularJS

I have problem with custom filtering in ng-table (date from - to).
Here's my plunker : http://plnkr.co/edit/g1t4pludTTIAJYKTToCK?p=preview
The problem is - all dates in my example array - Jul 21, 2017 17:14:00
But one date is - Jul 13, 2017 10:39:00
Now please filter date from - ex. Jul 12, 2017 to Jul 14 2017 - output is in 3'rd page. I wan't him to be in first page.
Thanks for answers in advance.

SQL 2012 Query to output to "table" column

I have prototype of SQL query (actual query is too huge to post)
SELECT Site, Risk_Time_Stamp,COMPUTER_NAME, [IP_ADDR1_TEXT],Number_of_Risks
FROM dbo.sem_computer
WHERE [dbo].[V_SEM_COMPUTER].COMPUTER_ID = SEM_COMPUTER.COMPUTER_ID
GROUP BY Site, Risk_Time_Stamp,COMPUTER_NAME, [IP_ADDR1_TEXT],Number_of_Risks
That outputs
Site Risk_Time_Stamp COMPUTER_NAME IP_ADDR1_TEXT Number_of_Risks
16K987 Aug 14, 2015 ADBF8J2 10.90.0.52 2
16K987 Aug 14, 2015 AD25N10 10.51.0.80 1
16K987 Aug 14, 2015 N20C0F8J2 10.18.0.79 1
How to create query that will output site, along with column named RISK STATISTICS that has table, i.e.
SITE RISK STATISTICS
16K987 Risk_Time_Stamp COMPUTER_NAME IP_ADDR1_TEXT Number_of_Risks
Aug 14, 2015 ADBF8J2 10.90.0.52 2
Aug 14, 2015 AD25N10 10.51.0.80 1
Aug 14, 2015 N20C0F8J2 10.18.0.79 1
#sean-lange
I'm trying to create a flat excel file to input in Tableau . Each Site will be plotted on a map and if there are any risks, a hover-over will detail these.
A Site can have zero to many risks, hence the need for column with a table value, i.e. column with array value.

Convert Date: Day Mon YYYY hh:mm AM CET

I have these dates, that I need converted to date:
Sat Nov 22 2014 01:01 AM CET
Mon Aug 18 2014 06:32 PM CEST
All the convert or cast functions I tried didn't work, maybe someone has an idea what to do?
In the end, I would need something like
YYYY-MM-DD HH:MM:SS or DD.MM.YYYY HH:MM:SS that doesn't really matter, but I would need them in the same timezone if at all possible...
Thank You for any ideas
SQL Server can convert that if you get rid of the day of the week at the beginning and the time zone at the end:
SELECT CONVERT(DATETIME, SUBSTRING('Sat Nov 22 2014 01:01 AM CET',4,LEN('Sat Nov 22 2014 01:01 AM CET')-7))
SELECT CONVERT(DATETIME, SUBSTRING('Mon Aug 18 2014 06:32 PM CEST',4,LEN('Mon Aug 18 2014 06:32 PM CEST')-7))
I'm not sure what you mean that you need them in the same time zone.

Resources