SQL script to bulk update a field depending on a condition - sql

I have a table with 2 date fields - OperativeTo and OperativeFrom. I have to check for the records for which OperativeTo is missing, then update OperativeTo as follows
OperativeTo = OperativeFrom [Year+1] - 1 day
That is, the operative date is equal to the OperativeFrom date of the same record in the following year minus 1 day. e.g. if a record has OperativeFrom date as 1/07/2015 then
OperativeTo will be 30/06/2016.

Try this:
BEGIN TRAN
SELECT * FROM YourTable
UPDATE YourTable
SET OperativeTo = DATEADD( DAY, -1, DATEADD( YEAR, 1, OperativeFrom))
WHERE OperativeTo IS NULL
SELECT * FROM YourTable
ROLLBACK
If it gives you desired result, get rid of BEGIN TRAN/ROLLBACK and run the query again. Also read about DATEADD() function.

Related

SSIS Expression to get julian date from yesterday

Can anyone help me with SSIS Expression
I have this query in the expression:
Select period, * from table
I want to add a where clause to get period = yesterday
But, period column is in julian date format.
at the end i want the same result like this query
Select period, * from table
Where getdate() - 1
Thank you
you only can use you table date time column to compare with the getdate
Select period,
dataDate, *
from table
Where dataDate = getdate() - 1

Retrieve records between a current date and previous date

I need to get a count of the # of rows resulting from a query which needs to have the below logic:
Assume the table includes 3 columns for now; ID, VALUE and INSERT DATE
Records inserted on
Current day-1
Minus
records inserted on
the latest business day prior to the (current day-1)
To add more details:
****I am looking for 'number of records' inserted between 2 dates i.e. if 200 records were inserted between Thursday and Friday then when I run the query on Monday my result should show me '200 records'.
Assumption: Business days = Mon-Fri
USE DATEADD Function:
Select
*
FROM
TableName
WHERE CreatedDate Between CAST(DATEADD(d,-1,GETDATE() AS Date) AND CAST( GETDATE() AS Date)
Please try this
Select * FROM TableName WHERE AddedDate Between DATEADD(day,-1,GETDATE()) and GETDATE()

Select the value for each first day of the month

I have the below table
I want to select the value of [UsedSpace(MB)] for each first day of the month.
For example 1/5/2015 the value of [UsedSpace(MB)] should be 10.
I tried the below query, but without success.
Select Cast(DATEADD(mm,DATEDIFF(mm,0,ExecuteTime),0) AS DATE) AS [Monthly],
[UsedSpace(MB)]
from tbl_Test
group by DATEADD(mm,DATEDIFF(mm,0,ExecuteTime),0), [UsedSpace(MB)]
Order by DATEADD(mm,DATEDIFF(mm,0,ExecuteTime),0)
Please any suggestions.
If you know that you will always have a single record for each date then you could limit the results in the WHERE statement. If there may be multiple records on the same date or no record on a date then you should use a MIN function.
SELECT
CAST(ExecuteTime as Date) AS Date,
UsedSpace
FROM tbl_Test
WHERE Day(ExecuteTime) = 1
For the sample data provided, you can simply make a where clause for the first day of the month. If you need a more generic solution, you need to get the lowest date for each month, and then join this with the original table.
WITH cte AS
(
SELECT MIN(ExecuteTime) AS Monthly
FROM tbl_Test
GROUP BY DATEADD(m, DATEDIFF(m, 0, ExecuteTime), 0)
)
SELECT t.ExecuteTime, t.[UsedSpace(MB)]
FROM tbl_Test AS t
JOIN cte AS m
ON t.ExecuteTime = m.Monthly
ORDER BY t.ExecuteTime

SELECT STATEMENT WITH SUBQUERY date - 30 days

In SQL Server 20112 I need to select the row ID from the dbo.table where the date is 30 days older than the date of the last record (the last record does not have to be today's date!).
SO....
The child query to get the right date is as follows:
SELECT cast(max(table_time) - 30 as datetime)
FROM dbo.table
WHERE column_value = 105
This returns exactly one [date] value as string '2014-02-03 ....'
Next I use this child query as a sub-query for the parent SELECT statement:
SELECT max(row_id)
FROM dbo.table
WHERE table_time = (SELECT cast(max(table_time) - 30 as datetime)
FROM dbo.table
WHERE column_value = 105)
...but this does not return the row_id I am looking for (it returns NULL). I tried to cast the date as follows but got the same NULL result
SELECT max(row_id)
FROM dbo.table
WHERE table_time = cast((SELECT cast(max(table_time) - 30 as datetime)
FROM dbo.table
WHERE column_value = 105) as datetime)
I suppose that if the child query returns a valid date value I should be able to use it in the parent select statement as a value I could compare/validate against.
Where I am wrong ?
---Use the 'cast' function in the 'where' clause as well
SELECT max(row_id) FROM dbo.table
WHERE cast(table_time as datetime)=
(SELECT cast(max(table_time) - 30 as datetime)
FROM dbo.table WHERE column_value = 105)

Set week number based on first Monday of Year

I have a requirement to set the week number of a table from the first day to the first Monday to the next Monday and so on. I can easily get the first Day and first Monday of year but I do not know how to increment trough the table in 7 days intervals from the first Monday so that I can set the week number.
I have something like this:
UPDATE table
SET weeknumberofyear = #WeekNumber + 1
WHERE datefield = DATEADD(Day,7,(SELECT DATEADD(DAY, (##DATEFIRST - DATEPART(WEEKDAY, #Date) + (8 - ##DATEFIRST) * 2) % 7, #Date)))
Since the datepart(week,datefield) function gets week number based on Sunday as the first day of the week, all you have to do is check datepart(weekday,datefield) and if it is 1 (Sunday) or 2 (Monday), subtract 1 from the datepart(week,datefield) function:
update table
set weeknumberofyear = datepart(week,datefield) -
case when datepart(weekday,datefield) in(1,2) then 1 else 0 end
EDIT This doesn't account for Years when Sunday or Monday are the first day of the year. In those cases, you would get 0 for weeknumberofyear. To fix this, perform a second update to your table. Even though this takes two updates, I still think it is more efficient than cycling through all the records.
update table
set weeknumberoftheyear = weeknumberoftheyear + 1
where year(datefield) in(
select distinct year(datefield)
from table
where weeknumberoftheyear = 0
)
EDIT WeekNumberOfTheMonth Update - Now that we have the WeekNumberOfTheYear value, we can use a ranking function on that field to update the WeekNumberOfTheMonth column without any recursion.
update t
set t.weeknumberofthemonth = u.weeknumberofthemonth
from table t
inner join (
select distinct weeknumberoftheyear,
dense_rank() over(partition by month(datefield)
order by weeknumberoftheyear) weeknumberofthemonth
from table ) u
on u.weeknumberofyear = t.weeknumberofyear
I'm not sure what you mean by all the talk about "Monday", but if you're looking to get the week number from a date, you can do something like this:
UPDATE table
SET weeknumberofyear = DATEPART(wk, datefield)