I have three column as Date1 in format(2016-12-31) then another column Term is (3 Years) then i want to do operation on date to add three years in Date1 field and result would be Dummy Column(2019-12-31)
I have syntax logic stagement from datastage, same i need to put into SQL
datastage syntax - DecimalToString(StringToDecimal(DSLink17.Date1 [1, 4],'ceil') + StringToDecimal(DSLink17.Term [1, 1], 'ceil'), 'suppress_zero') : DSLink17.Date1 [5, 6]
If you want to add year to the date,you can try something like this
select dateadd(year, Term::int, to_date(Date1)) as dummy_column ;
However its not very clear from the question , is column Term present as just 3 or 3 Years in the database, if its just 3 go with the above query else use something like this to split the string
select dateadd(year, split(Term,' ')[0]::int, to_date(Date1)) as dummy_column ;
You might do better to ask this question in a Snowflake forum.
Related
I am using the following query to retrieve snapshot values from 4 different dates: -1 day (latest), -2 days, -3 days and -40 days (not yet implemented).
SELECT [SnapshotDate]
,[SnapshotKey]
,[info1]
,[info2]
,[info3]
FROM [Database].[dbo].[Values]
WHERE [SnapshotDate] >= DATEADD(day,-3, GETDATE())
AND [SnapshotKey] = 'Some text here'
This results in the following graph:
The query is not quite right firstly since it is showing 4 values and should only be showing 3 at this point. Secondly I would like to show the last snapshot from 40 days ago as shown in the graph.
I have tried a few different queries but have not managed to figure out how to do this properly.
[SnapshotKey] = SELECT DATEADD(day,-40,getdate())
The above query gives me the correct answer in theory. However, when I use this in my query there is no result. I believe this might be due to not having a date conversion or the fact that I'm using "day" in my query. I'm not sure.
Any suggestions?
EDIT:
I also tried using the following with no luck (no result):
CONVERT(date, [SnapshotDate]) = CONVERT(date, DATEADD(day,-40, GETDATE()))
I'm not sure what your date values are but I'm guessing this report was run on 2nd May.
If this is correct then you need to change the range to exclude where the difference in dates is zero. Personally I use DATEDIFF in situations like this as it's easier to visualise for me.
Try changing the where clause to something like this.
WHERE (DATEDIFF(day, getdate(),[SnapshotDate]) BETWEEN -3 AND -1
OR DATEDIFF(day, getdate(), [SnapshotDate]) = -40)
AND [SnapshotKey] = 'Some text here'
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())
I am trying to run sql written in Access on sql server. The query is throwing a syntax error on line 4:
UPDATE mytablename SET
table.[Specimen Collection Date 1] =
IIf(
[Specimen Collection Date 2] Is Not Null, //incorrect syntax near is
[Specimen Collection Date 2],
IIf([Specimen Collection Date 2] Is Null,[Specimen Collection Date 3] Is Not Null))
It seems like a valid column name [Speciment Collection Date] seems like a valid expression.
What am I missing?
There is no IIf in sql. Look up the case when tsql convention.
http://msdn.microsoft.com/en-us/library/ms181765.aspx
You don't need to include the TABLE in your column definition. The DB knows what table it is from your "Update TABLE" statement.
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
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'