SQL SSIS Derived Column formula/syntax issues - sql-server

I have a formula that I need to be able to put into my SSIS Derived Column... Below it is..
if MONTH(Sls.SlsDt) + DAY(Sls.SlsDt) > MONTH(TODAY) + DAY(TODAY) then
dYdtYr = 0.
else
dYdtYr = YEAR(Sls.SlsDt).
How would I put the above business logic into a SQL SSIS ETL Derived Column Transformation?
Is Derived Column the right option to accomplish this?
The ETL package is an import from CSV to SQL staging table, during this import I need to output a new column (or replace existing) that has the logic above implemented.
The input rows contain the MONTH and DAY and dYdtYr columns.. In a sense, I would need to override the dYdtYr value with new value based on the above logic.

Derived column set as replace column have it replace dYtdYr. Put the following into the expression:
(MONTH(Sls.SlsDt) * 100 + DAY(Sls.SlsDT) > MONTH(getdate())*100 + DAY(getdate())) ? 0 : YEAR(Sls.SlsDt)
This takes the month value (03 for march) multiplies it by 100 to get 300 and adds it to the day value (04 for today) and yeilds a value like 0304. This will work so that any day of the year before or equal to this value will be less than that number and any day of the year after today will be greater. Note this does not take into account years. This means that December 31 from 3 years ago would get a 0 in dYtdYr but March 3 from 3 years ago would (today) get the year for SlsDt for it. I would guess that if this is your desired outcome you are trying to build a YTD Year over Year comparison.

If you can use TSQL then you can use a CASE statement
CASE
WHEN MONTH(Sls.SlsDt) + DAY(Sls.SlsDt) > MONTH(getDate()) + DAY(getDate())
THEN dYdtYr = 0
ELSE
dYdtYr = YEAR(Sls.SlsDt)
END AS ColumnName

Related

How to use - within like '%'% query

I have a table column in SQL 2012 called transitiontime that contains dates and times in the format 2017-02-02 21:00:34.847. I'm trying to query all results within the last 3 months, which I would think would just require me to look for 2017-02, or something to that effect. However if I leave the hyphen in, the query fails.
Examples:
WHERE transitiontime like '%2017%' <- works but returns all values from this year
WHERE transitiontime like '%2017-02%' <- Does not work at all
WHERE transitiontime like '%2017%02%' <- Works but pulls in anything with 2017 and 02 in it, even if 02 is just in the time.
I would love to get the past 3 months in one query, but right now I'd like to just be able to pull from 1 month.
I'm assuming, since you are using the keyword "like", that the field "transitiontime" is a char oder varchar field?
Change the type of the field to DateTime and use the following query, to get all results for the last three months (applying to SQL):
SELECT * FROM table WHERE transitiontime >= DATEADD(month, -3, GETDATE())

Concatenate 2 x fields to make a Date

I have 2 x fields T1.period and T1.year both have data type smallint
Using SQL Management Studio 2014 how may I Concatenate them AND return result as a DATE type?
Also, T1.period has values 1 to 12 how may I pad this out to 01 to 12 ... or will changing to date type sort this out?
Much appreciated!
Sample data ...
period yr
1 2015
2 2009
12 2009
11 2010
10 2011
Result will be ...
Date
01/01/2015
01/02/2009
01/12/2009
01/11/2010
01/10/2011
Thanks!
Looks terrible struggling to get it into lists - sorry :(
Converting Your Values The Old Fashioned Way
Assuming that your t1.period value actually just represents a month, you could consider just converting your values to strings and then converting that concatenated result into a date via the CAST() function :
SELECT CAST(CAST(t1.year AS VARCHAR) + '-' + CAST(t1.period AS VARCHAR) + '-1' AS DATE)
This will build a string that looks like {year}-{month}-1, which will then be parsed as a DATE and should give you the first date of the given month/year.
Using The DATEFROMPARTS() Function
SQL Server 2012 and above actually support a DATEFROMPARTS() function that will allow you to pass in the various parts (year, month, day) to build a corresponding DATE object, with a much easier to read syntax :
SELECT DATEFROMPARTS(t1.year,t1.period,1)
Additionally, if you needed a DATETIME object, you could use DATETIMEFROMPARTS() as expected.

Update query to find and change portion of MS SQL DateTime

I have various datetime stamps on a expiration field (datetime). The datetime in that field for each record varies. For example,
2015-12-31 04:59:00:000, 2014-12-31 17:00:00:000, 2020-12-15 04:00:00:000
Trying to write a query to find all datetime that have an ENDING that is not equal != to 05:00:00:000.
Then an update query to update the end of the stamp to 05:00:00:000, leaving the front as is.
Some example changes, from - to:
From:
2015-12-31 04:59:00:000, 2014-12-31 17:00:00:000, 2020-12-15 04:00:00:000
To:
2015-12-31 05:00:00:000, 2014-12-31 05:00:00:000, 2020-12-15 05:00:00:000
Here is what I'd like to see:
UPDATE table_name
SET table_name.expire_field = keep_front_date_portion + '05:00:00.000'
WHERE table_name.expire_field = date_portion_ignore and time_portion != '05:00:00.000'
Not sure how this would be written correctly in MS SQL 2008 syntax?
I found this post but from what I can tell it needs a timestamp that does not vary.
We can use DATEADD/DATEDIFF to reset just the time portion of a datetime value
UPDATE table_name
SET expire_field =
DATEADD(day,DATEDIFF(day,'19000101',expire_field),'1900-01-01T05:00:00')
WHERE DATEPART(hour,expire_field) != 5 or
DATEPART(minute,expire_field) != 0 or
DATEPART(second,expire_field) != 0
But you may find it easier to just skip the WHERE clause and let it update the entire table - you're not going to be able to benefit from indexes here anyway.
In the DATEADD/DATEDIFF pair, the date is arbitrary. First, the inner DATEDIFF asks "how many whole days have passed since some date", and then the DATEADD adds that same number of days onto some date at 5am.

SQL query to search

i am new at sqlserver and i just started learning it , and i have a question .
**i want an SQL query that can bring up all rows that were added in the past 6 months. in table 'users' the date is in field 'joined' but its in UNIX time stamp format
**and i want then like another SQL query the same as above but it also with the results sets the field 'activate' in each row to 'yes'
i tried that code :
select * from users where time_of_post is like '07/**/2011'
** = anyday but i can`t implement it right .
Thank you a lot for your help in advance .
select *
from users
where datediff(mm, time_of_post, getdate()) <= 6
What you want to use is the DATEDIFF() function, and get the difference between today's day (GETDATE()) and the stored dates (time_of_post). If that is less than or equal to 6 months (as denoted by the first parameter, mm) then that should be what you're looking for.
EDIT:
If you're looking to use this logic for an UPDATE, you'd do something like this:
update users
set activation = 'yes'
where datediff(mm, time_of_post, getdate()) <= 6
and activation <> 'yes'

What's the best way to perform math on a floored date in SQL Server?

This is related to Floor a date in SQL server, but I'm taking it one step further:
I'm writing a query on a SQL Server table and I want to get all records for the current calendar year and the previous calendar year. So, right now, I'd want a query to return all records between January 1st, 2008 and today. But come January 1st, 2010, I want it to return records no older than 2009.
Essentially, I want to floor the current date to the beginning of the year and then subtract 1.
After rummaging through some SQL Server documentation, I came up with this:
WHERE create_date >= CAST((CAST(YEAR(GETDATE()) AS int) -1) AS varchar)
but it feels kind of ugly. Is there a better way?
Why not just use the year function on create_date as well?
WHERE YEAR(create_date) >= (YEAR(GETDATE()) -1)
This assumes (as you did) that there are no records in the database greater than today's date
I would suggest assigning a variable with the date lastyear-01-01, either by making an UDF for it, or something like
DECLARE #startOfLastYear DATETIME
SET #startOfLastYear = CAST(YEAR(GETDATE()) - 1 AS VARCHAR) + '-01-01'
Then do the query:
WHERE create_date >= #startOfLastYear
Because of two reasons:
Using YEAR() or any other function
on data from tables (i.e.
YEAR(create_date)) makes indices
unusable and decreases the
performance of the query
The variable name tells exactly what it is, and reading the code is easier.

Resources