I am trying to import an xls spreadsheet into a table, and one of the columns needs to be derived from a different column in the same table. I would like to do this during the import phase rather then creating a SQL Task to do it after the import. I am horrible when it comes to creating expressions in SSIS, so this is probably an easy task - but I just can't get it right.
DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,columndata)+1,0))
That is the expression that I am trying to use.
You want the following:
DATEADD("S",-1,DATEADD("M",DATEDIFF("M", (DT_DATE) 0,DATEADD("D",[dataDate],(DT_DATE)-2))+1,(DT_DATE)0) )
1) You need to use " to surround your dateparts.
2) You need to cast your 0 date to an explicit date type.
This expression takes the column dateserial number [dataDate] adds that number of days to the date -2
DATEADD("D",[dataDate],(DT_DATE)-2)
(apparently sql day 0 and excel day 0 are different by 2 days on my versions (sql2008r2 and excel 2007)) and finds the number of months from date 0 to that date,
DATEDIFF("M", (DT_DATE) 0,DATEADD("D",[dataDate],(DT_DATE)-2))
then adds 1 to that number of months and adds it back to date 0
DATEADD("M",DATEDIFF("M", (DT_DATE) 0,DATEADD("D",[dataDate],(DT_DATE)-2))+1,(DT_DATE)0)
(to get the beginning of the next month following the column date value), Then it subtracts 1 second from the date to get the last second of the month of the column date value in the datetime format.
DATEADD("S",-1,DATEADD("M",DATEDIFF("M", (DT_DATE) 0,DATEADD("D",[dataDate],(DT_DATE)-2))+1,(DT_DATE)0) )
Related
Let’s say I have a table that has one column as timestamp ntz. I want to perform a select that calculates the number of seconds difference between that moment in the table vs the start of that date. For pure timestamp type I can just use the timediff function:
select timediff(second,
‘2022-01-01 00:00:05’::timestamp_ntz,
‘2022-01-01 00:00:00’::timestamp_ntz
)
Which returns 5.
How can I do it with a value in a table, for example:
select timediff(second, my_date, my_date_startofdate)
Where my_date_startofdate will be converted to that day but at time 00:00:00.
Use date_trunc. This function takes a timestamp or date and truncates it to whatever element you want. So, if you take your timestamp column and trunc it day, it removes the time elements and gives you the beginning of the day.
select timediff(second, date_trunc('day',my_date), my_date)
https://docs.snowflake.com/en/sql-reference/functions/date_trunc.html
I am trying to create a calendar using Power Query functions and for that I used below syntax in blank query:
Source= Duration.TotalDays(DateTime.LocalNow() - #datetime(2014,01,01,00,00,00)) * 24
Date= List.DateTimes(#datetime(2014,01,01,00,00,00), Source ,#duration(0,1,0,0))
Then I convert to a table and apply query.
Connect dimension date table to date column in fact table.
The error occurs when I’m trying to mark table as date table:
‘The date column can only gave one timestamp per day. The date column
can’t have gaps in dates’
What I have done wrong?
As the error message says:
The date column can only have one timestamp per day.
While you are trying to add 24, one for each hour. See the requirements for setting a table as a date table:
if it is a Date/Time data type, it has the same timestamp across each value
i.e. you can have only one value for each date, and if it is not a date, but datetime value, all time values should be the same.
I need to select from a table that has two fields , a start date and an end date , selecting the row that the current date is. Look at the image ...how do i select the row that encompasses the date range 91/201 - 9/15/2018 ? Somehow this was flagged as a duplicate. I am NOT attempting to find overlap. I am attempting to find the row that represents a given date. Say , one table has a field that has the value 9/5/2018 ....how do I select the row that has a start date of 9/1/2018 and end date of 9/15/2018
In my data base , i was stored one student data in the following date format is 07-03-2013(dd-mm-yyyy). For example the start date greater than the end date ,no row return in the results.
For Ex.
select student_name from general_details where join_date between '11-02-2013' and '08-03-2013'
it returns 0
if start date less than the end date
select student_name from general_details where join_date between '01-02-2013' and '08-03-2013'
it returns row value 1;
Please suggest ,thanks in advance
The dates are compared alphabetically and not as datetime stamps.
If you can influence how the dates are stored, use a format such as ISO-8601 yyyy-MM-dd or unix epoc timestamps to make the comparisons work.
If you cannot influence the data, you can convert the values in SQL by picking up the components with substr() and concatenating together with ||, like this:
substr(join_date,7,4) || substr(join_date,4,2) || substr(join_date,1,2)
Hi all i want to take record from table named Tblbatch where batch starting date should be from augest 2007 to july 2010...
I want to fetch such records which came in between this two dates
Select * from Tblbatch where startDate between '01-08-2007' and '31-07-2010'
provided you have a datetime column "startDate"
Note : that using between includes both the dates specified.
If you want to avoid the dates either change the boundary dates to + - 1 respectively or use > and < conditions