Day and month swapping only for single digits in SSIS - sql-server

I have written an SSIS integration which fetches an employee and his expiry date. All the data is flowing correctly; however when a single digit day and month is present in expiry date, the destination column swaps the month and day (when the date has double unit for months or days its fine).
Example:
07/08/2016 to 2016-07-08 WRONG
15/03/2016 to 2016-03-15 CORRECT

since your date are in dd/MM/yyyy and you are in trying to convert it into datetime,so problem occurs.
I think this more safe,
declare #jk varchar(20)='07/08/2016'
select right(#jk,4)+'-'+substring(#jk,charindex('/',#jk)+1,2)+'-'+substring(#jk,1,2)

Related

casting INT to Datetime date is 2 days off

So I am parsing out a large text field with several dates in it. the date format comes out like "44445" which should be "9/6/2021" but when I convert to datetime in Microsoft SQL Server 2019 i get "9/8/2021" I found that every date that I convert is 2 days off. I can of course just do a -2 before converting to make sure I get the right date but I found this really odd and wondered if anyone knew why this was happening and if I am doing something wrong
cast(44445 as datetime) result 2021-09-08 00:00:00.000
if I put the same date in excel I get "9/6/2021" which is the correct date
I found something online about excel calculating 1900 as a leap year and therefore having a different date but excel seems to be correct and SQL statement seems to be wrong so I don't know...

How to get a YTM calculation instead of YTD in Google Data Studio

I have an earnings table in my Google Data Studio report from January 21 - YTD.
But what i want is a table that shows me YTM (so this year until last month) Jan-21 til Sep-21 and that dynamicly on 1st Nov, Okt-21 is added to the table, and on 1st dec Nov-21
is added etc.
Because the Total YTD value is other wise too high, due to cost being booked at the end of the month and earnings through the month.
Filters wont let you do it dynamicly,
date range filters same,
always possible to do it manualy with date range but this i cannot ask from my client.
any suggestions??
Kind regards
I found the answer,
i left the question for when someone else wants to do it,
it is actualy simpel fixed by using advanced date filter and then leaving the start date on 1-jan, and end date to today minus 1 month.
this gave me the exact result i needed yt last month

Power BI: Week #'s not sorting properly

I am having trouble with a line graph visual, where the data is organized by week number and by year number. However when I put the information into the visual and try viewing both 2020 & 2021, it rearranges the data in the order of 2021 & 2020. How do i get it to properly see the data in the correct order of week number by year?
I tried sorting the week # by an index value, also by year, also by week... with no luck
From the images it looks like there is no sort on the year and week, just by the week.
You need to add a column that has a year week key, that you can sort by.
For example 202101 for the week one of 2021.
Assuming you have a date like dd/mm/yyyy format, for example 11/04/2021 in DAX you can use:
YearWeek = YEAR('Table'[Date]) & WEEKNUM('Table'[Date])
This should now sort the data correctly. If you want you can add another column, that is more user friendly like WK01-2021, if you wish, you can then sort by that column, or use the new key column to sort the textual one.
If you just have a year and week column, create a new column that concatenates the two.
For this you should have a Calendar table, that contains a the date groupings that you you need. For example using CALENDARAUTO or you can do it in Power Query here or here.
This actually does not give the correct sequence, when you are dealing with single digit week numbers. For example when dealing with the first ten weeks of 2020, the sequence would be 20201, 202010,20202, 20203... which is obviously wrong.
Here you need a double digit Week number, so a small change in the suggested formula should do it:
YearWeek = YEAR('Table'[Date]) & FORMAT(WEEKNUM('Table'[Date]),"00")
The sequence should now work.

last day of last month date expression in ssis

I have to create a derived column to upload a date in OLEDB destination because my source file doesn't contain this date. The date i want to get through derived column is last day of last month. Does anyone know how to get it?
Try the following expression, just substract the current day from the current date using DATEADD Function.
DATEADD("d", -DAY(GETDATE()), GETDATE())
If you want to remove time you have two choices:
convert to string
LEFT((DT_STR,50,1252)DATEADD("d", -DAY(GETDATE()),GETDATE()),10)
convert to string then to date (it will generate time 12:00 AM)
(DT_DATE)LEFT((DT_STR,50,1252)DATEADD("d", -DAY(GETDATE()),GETDATE()),10)
The simplest way if you don't need time is :
(DT_DBDATE)(DATEADD("d",-DAY(GETDATE()),GETDATE()))

Sql server and Excel float to date offset

I'm working with date and float conversion on sql server and excel.
This is my query:
select getdate(),convert(float, getdate())
I get:
2014-11-21 16:38:49.973 41962,6936339506
If I copy this float number to Excel and I change cell type as date I get this value:
19-nov-2014
What's that? Why there is an offest of two days?
SQL server simply calculates the conversion of a date time to a float as the number of days since midnight on 01-Jan-1900 (i.e. select convert(DATETIME, 0) gives 1900-01-01 00:00:00.000)
Excel calculates a similar number, but the zero date is "00/01/1900". This is probably related to the fact that excel uses one based indexing, rather than the more common zero based indexing. The second day of difference comes from a well known bug whereby excel considers 1900 to have been a leap year.
Takeaway message: if you assume that excel is always behind by two days you'll be ok, except for dates on or before the 28th of February 1900.

Resources