I have a table in postgres
one of the columns is year which is calculated as :
date_part('year',current_date) + generate_series(-2,4)
so if I take today's date , the output will be :
year
2021
2025
2019
2024
2023
2022
2020
How to achieve the above in snowflake environment.
Thanks in advance
Below snowflake community article will give more in depth info about using functions like SEQ1(),SEQ2(),SEQ4(),SEQ8() and generator functions.
Generate gap free seq and dates
Using GENERATOR and DATEADD:
SELECT DATEADD(year, (ROW_NUMBER() OVER(ORDER BY seq8())-3, current_date) AS y,
FROM TABLE(GENERATOR(ROWCOUNT => 7));
And year only:
SELECT YEAR(CURRENT_DATE) + (ROW_NUMBER() OVER(ORDER BY seq8())-3 AS y,
FROM TABLE(GENERATOR(ROWCOUNT => 7));
Related
I'm creating a report in SQL Server 2012. The report is pulling out discharge patients information from database. The report only needs to show patients the third admission day information.
For example, patient A was admitted into the hospital on January 01/2016, (stayed for 4 days) January 02, January 03, January 04 and was discharged on January 05/2016.
Patient B was admitted into the hospital on January 15/2016, (stayed for 6 days) January 16, January 17, January 18, January 19, January 20 and was discharged on January 21/2016.
The report only needs to show the third day's (January 03 and January 17) information.
How to write the function to filter out the data only show necessary data? Any suggestions?
Greatly appreciate your big help!
Thanks!
Rose
You can use a combination of Row_Number() and Partition by. Basically you partition by the patient and you row_number based on the date ascending.
Select ... ROW_NUMBER() over(PARTITION BY patientId Order by TheDate)
Then you can use that above as a sub query and filter where t.theRow = 3.
Sorry I'm on an iPod so it's difficult to provide more info. I'll try to clean this up or format it tomorrow.
Edit
Now that I'm on a PC, here's what you can do per above
SELECT
t.PatientID,
t.TheNumber
FROM
(
Select
PatientID,
ROW_NUMBER() over(PARTITION BY PatientID ORDER BY YourDateField) AS TheNumber
FROM
YourTable
) t
WHERE
t.TheNumber = 3
I have one table in SQL Server where I want to add a column that will show this year's date for the exact transition dates for European Summer Time.
European Summer Time is observed across three time zones, beginning at 01:00 UTC/WET on the last Sunday in March and ending at 01:00 UTC on the last Sunday in October each year.
How do you change this into a SQL function?
On Wikipedia I was able to find this:
The formula used to calculate the beginning of European Summer Time is
Sunday (31 − ((((5 × y) ÷ 4) + 4) mod 7)) March at 01:00 UTC
https://en.wikipedia.org/wiki/Summer_Time_in_Europe
Be careful. DST is a non-technical, but political policy and can vary over time (countries change DST adjustments all the time) which requires an up-to-date timezone database.
SQL Server itself is not concerned with timezones: it does not perform any timezone conversion itself, only UTF-offset conversion (when using datetimeoffset when the instantaneous local-to-UTC conversion is known). It cannot tell you anything about zone locations or future offsets, as those are arguably business rules that belong outside the domain of the persistence layer (i.e. SQL Server). When storing values in a database always used either UTC datetime or datetimeoffset values. Use UTC datetime when zone information is irrelevant to the user (i.e. don't invent your own datetime+int system, use datetimeoffset).
Along those lines, SQL Server does not provide any API or functionality for accessing the system (the Windows OS') timezone database from T-SQL code.
You would need to do this from application code. In .NET you can use the TimeZoneInfo class which uses Windows' timezone database to get the DST dates:
TimeZoneInfo.AdjustmentRule.DateStart
TimeZoneInfo.AdjustmentRule.DateEnd
TimeZoneInfo.AdjustmentRule.DaylightTransitionStart
TimeZoneInfo.AdjustmentRule.DaylightTransitionEnd
https://msdn.microsoft.com/en-us/library/system.timezoneinfo.adjustmentrule(v=vs.110).aspx
Depending on what the criteria you're inputting into whatever function you want to produce here is, the specific code is going to be different.
The following, for example, produces a list of the dates/times for the next 100 years in the timezone this is run in.
; WITH C1(N) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) T(N))
, C2(N, RN) AS (SELECT 1, ROW_NUMBER() OVER (ORDER BY NEWID()) FROM C1 a CROSS JOIN C1 b)
, TYears AS (SELECT YEAR(GETDATE()) + RN - 1 [Years] FROM C2)
SELECT DATEADD(hh
, DATEDIFF(hh, GETUTCDATE(), GETDATE())
, DATEADD(dd
, 0 - (1 + DATEDIFF(dd, 0, CONVERT(DATE, CONVERT(NVARCHAR, [Years]) + '-03-31')) % 7) % 7
, CONVERT(DATETIME, CONVERT(NVARCHAR, [Years]) + '-03-31 01:00:00'))) [StartDateTime]
, DATEADD(hh
, DATEDIFF(hh, GETUTCDATE(), GETDATE())
, DATEADD(dd
, 0 - (1 + DATEDIFF(dd, 0, CONVERT(DATE, CONVERT(NVARCHAR, [Years]) + '-10-31')) % 7) % 7
, CONVERT(DATETIME, CONVERT(NVARCHAR, [Years]) + '-10-31 01:00:00'))) [EndDateTime]
FROM TYears
The basic premise being find the last Sunday of March for that year, find the UTC +/- value, add it to 1AM of the last Sunday of March.
SELECT * FROM [MarkTSK]
WHERE [MonthlyDt] IS NOT NULL
--AND
--SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
Hello folks. Does anyone know how to correctly write this statement to
display the FIRST of each month? I have an Excel file that I am importing
into a Server using MS SSIS (VisualStudio 2008). The dates are monthly, but the last few months were the 15th June, May, April etc. My intent is to make
all of them show 1st of the month. All the months before January 2015 have been on the 1st of the month.
The SQL Query above is what I wrote in the Excel source Editor.
Thank You
This should always give you the first of the current Month
SELECT DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),0)
to pull back the first of the month for dates in your table something like this should do it
SELECT DATEADD(MONTH,DATEDIFF(MONTH,0,[MonthlyDt]),0) FROM [MarkTSK]
WHERE [MonthlyDt] IS NOT NULL
SELECT *, DATEADD('d',-DAY([MonthlyDT])+1, [Date]) AS [MonthD]
FROM [MarkTSK]
WHERE [MonthlyDT] IS NOT NULL
Thank you everyone. The SCRIPT above worked for me. All the dates that were not on the 1st of the month, now shows as the 1st.
Use DatePart to extract the Month and Year adding in the day manually.
Something like:
SELECT Convert(date, DatePart('yyyy', [MonthlyDt]) + DatePart('mm', [MonthlyDt]) + '01' ) FROM [MarkTSK]
WHERE [MarkTSK] IS NOT NULL
(Disclaimer: untested code)
i have a table calculating the installments. in that table i'm saving all the data recording to that. For example if i'm calculating for 60 installments and saving all the data,so it is like 60 months. so now i need to sum up the value of one column for every 12 months. sometimes v start paying the installments from the middle of the year also.
my DB looks like this.the highlighted column must sum up for every 12 months. two images are one table only
suppose i have 30 installments from starting on jun 2012.suppose i started paying installment from jun 2012 then should sum up the installments from jun 2012 to may 2013. v can't use group by year. i must sum up like this ................................................................................
sum jun 2012 to may 2013
sum jun 2013 to may 2014
sum jun 2014 to nov 2014 ( only 6 months left)
You can use ROW_NUMBER to generate a group of 12 months:
WITH Cte AS(
SELECT *,
RN = (ROW_NUMBER() OVER(ORDER BY InstallmentMonth) - 1)/ 12
FROM your_table
)
SELECT
SUM(InteresetPerInstallment)
FROM Cte
GROUP BY RN
I am working with SQL Server and a table containing DOB [Date Of Birth] column.
I have saved a value in table against that column, that is = 'May 5 1988 12:00AM', but I need 'May 5 1900 12:00AM' when I select the column.
I have this basic query:
SELECT CAST(dbo.contact.dob AS VARCHAR) AS DOBFROM dbo.contact
The result is May 5 1988 12:00AM
Any help?
If you are going to replace the custom year then this might help you. Here GETDATE() is your datecolumn.
DECLARE #CustomYear = '1900'
SELECT REPLACE(CAST(GETDATE() AS VARCHAR(MAX)),DATEPART(YEAR,GETDATE()),#CustomYear)
This gives me output as,
Mar 17 1900 4:12AM
A simple date math:
select dateadd(year, -datediff(year, '19000101', getdate()), getdate())
(where getdate() stands for your table's column).
You might have a problem with leap years, though.
I hope I can help you out, i think what you need is:
SELECT DATEADD(YEAR, (1900-(DATEPART(YEAR, dbo.contact.dob))), dbo.contact.dob ) AS DOB FROM dbo.contact
DATEADD and DATEPART will do the trick
Try to add 2 years like
SELECT CAST(DATEADD(year,2,dbo.contact.dob) AS VARCHAR) AS DOB
FROM dbo.contact
This will add you years so you will get for May 5 1988 12:00AM -> added 2 years like May 5 1990 12:00AM