Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a field with sales_start_date( values like 2014-06-17 ,2015-07-23...)
I need to do calculation based on sales_start date..
I have to take the most recent month-end date(i have to force this value to 2016-07-31) and subtract the sale_start_date to get an integer count of the days elapsed. If number of days passed is less than 2 default the value 2
sales_start_date
2016-01-01
2016-07-30
Output
Calculated field
155
2
Can any one help me in writing case statement.
Your help is much appreciated.
Thank You,
Swathi.
You basically need to use DATEDIFF
SELECT DATEDIFF(day,<enter start date column here>,<most recent month-end date column here>) AS [Calculated field]
FROM <your table here>
SQL-server 2012 + you have the EOMONTH() to help you get to the end of the month more easily.
;WITH cteData AS (
SELECT CAST('2016-01-01' AS DATE) as Start_date
UNION ALL
SELECT '2016-07-30'
)
SELECT
EOMONTH(DATEADD(MONTH,-1,GETDATE())) as EndOfPreviousMonth
,DATEDIFF(DAY,Start_date,EOMONTH(DATEADD(MONTH,-1,GETDATE()))) + 1 as DaysDifferent
FROM
cteData
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have come once more to seek your guidance.
I am not actually sure how possible this is, but I can only hope.
I have an already heavily joined and aggregated query which pulls out results as shown in the attached picture.
As mentioned, this is an already heavily editing query with multiple joins and aggregates to take half hour data and sum it hourly.
I am wondering if it is at all possible to now transpose the data so that there are 24 seperate hourly entries for each date?
So instead of having one single line for 2014-01-01 with each hourly reading in a seperate column, is it at all possible to change it so there are just two columns. One for date/time and one with the hourly total.
So it would look more like:
and then continue down for each hour of each day.
I am using SQL Server 2014 if that helps.
Thank you in advance!
Assuming your time columns are named time0, time1, ..., time23 you could write
SELECT DateAdd(HOUR, 0, date) as DateTime, time0 as total from table
UNION
SELECT DateAdd(HOUR, 1, date) as DateTime, time1 as total from table
UNION
...
SELECT DateAdd(HOUR, 23, date) as DateTime, time23 as total from table
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
select max(QTR) from A.Table;
---2020Q4
In A.Table QTR column is in 'YYYYQQ' format
An expected outcome to be: Addition of QTR
---2021Q1
(ie. 2021Q1 is the max QTR means: Expected Outcome: 2021Q2
2021Q2 is the max QTR means: Expected Outcome: 2021Q3
2021Q3 is the max QTR means: Expected Outcome: 2021Q4
2021Q4 is the max QTR means Expected Outcome: 2022Q1)
How to achieve it?
One approach is to convert the value to a date by adding the appropriate number of months to the first of the year. Then convert back to YYYYQ format:
select to_char(add_months(to_date(substr(yyyyq, 1, 4) || '-01-01', 'YYYY-MM-DD'), substr(yyyyq, -1) * 3), 'YYYY"Q"Q')
from (select '2021Q4' as yyyyq from dual)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
The question is to Produce a list of the latest movies by genres for the current month. How to find the current month from the Date???
You are looking for DATEPART()
Select
*
From YourTable
Where
datepart(month,ReleaseDate) = datepart(month,getdate())
and datepart(year,ReleaseDate) = datepart(year,getdate())
Order by Genre, ReleaseDate desc
You can try this:
SELECT SUBSTRING(TO_CHAR(now(),'YYYYMMDD'),5,2)
In which:
Format to date and you get only the month
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Okay, I have a situation when I need to compare a column from a table with today's date. If the value in a particular row is earlier than today (in other words, the date has already passed, I need to mark a virtual column with the phrase 'lapsed'
Below is the SQL (SQL Server 2012) that I have been using:
SELECT
datediff(day, sysdatetime(), policy_expiration_dt) As 'DayDiff'
,Case When 'DayDiff' < 0 Then 'Lapsed'
FROM TABLE_NAME
The first column that includes the datediff function makes the comparison and the second column marks it as lapsed if the date has passed.
basically the order of how an sql statement is executed does not allow you to use the alias. so you will need to use the datediff() function in the CASE statement.
The order of sql queries execution is:
1. FROM
2. ON
3. OUTER
4. WHERE
5. GROUP BY
6. CUBE | ROLLUP
7. HAVING
8. SELECT
9. DISTINCT
10. ORDER BY
11. TOP
you need to do this:
SELECT DATEDIFF(day, SYSDATETIME(), policy_expiration_dt) AS 'DayDiff'
, CASE
WHEN DATEDIFF(day, SYSDATETIME(), policy_expiration_dt) < 0 THEN 'Lapsed'
END
FROM TABLE_NAME;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a query in which I am calculating time. I want to make time double so that if the time is 01:40 then it should become 03:20. Here is my current query
SELECT TO_CHAR(TRUNC((ATN_INN-ACT_INN)*24),'09')||':'||
TO_CHAR(TRUNC(((ATN_INN-ACT_INN)*24-TRUNC((ATN_INN-ACT_INN)*24))*60),'09') B
FROM SML.EMP_INFO A
How should I go about this?
If you really what to work only with the time element, the easiest way to so is to employ the 'SSSSS' date mask, which returns the number of seconds after midnight. So this query will return double the number of seconds between the two columns:
select ( to_number(to_char(atn_inn, 'sssss'))
- to_number(to_char(acn_inn, 'sssss')) ) * 2 as double_diff_in secs
from sml.emp_info
/
Converting seconds into hours and minutes is left as an exercise for the reader.
Note that this query only makes sense if ATN_INN is later than ACT_INN but still on the same day. This is the clarification #Ben was trying to make (with no success). If this is not the case a different solution is required, something like ....
select ( ( extract ( hour from diff ) * 60)
+ extract ( minute from diff ) ) *2 as double_diff_in mins
from ( select to_dsinterval ( atn_inn - act_inn ) as diff
from sml.emp_info )
/
This returns the doubled different in minutes. Again, rendering the output into a display format is left to the reader.
It seems to me you are using the method described here (Subtraction between dates): http://www.akadia.com/services/ora_date_time.html
If you want to double the lenght of the period, you really only need to multiply the base time difference with 2, like this:
SELECT TO_CHAR(TRUNC((2*(ATN_INN-ACT_INN))*24),'09')
|| ':' ||
TO_CHAR(TRUNC(((2*(ATN_INN-ACT_INN))*24
-TRUNC((2*(ATN_INN-ACT_INN))*24))*60),'09') B
FROM SML.EMP_INFO A
This will give you the hour part and the minutes part of your results, you will probably want to have the days part too.