SQl CASE Statement - tsql

I want to sort column value(DateTime) on a condition(and event occurring before March 1st 2016 as 'Preseason',
event Occurring after march 31st 2016 as 'PostSeason' Else 'Season'.
--Query---
Select EventDate= Case
When EventDate <'01-03-2016' then 'PreSeason'
When EventDate >'31-03-2016' then 'PostSeason'
Else 'Season'
End,
EventName From tblEvent
--Error Message
'Msg 242,The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.'

Something like..
ORDER BY
CASE
WHEN ISDATE(DateTime) = 1 THEN
CASE DateTime
WHEN <= '2016-03-01' THEN 1 -- Preseason
WHEN >= '2016-03-31' THEN 3 -- PostSeason
ELSE 2 END -- Season
ELSE 4 END, -- Not a date, figure out how to handle
DateTime

-- ok, Was a date format issue. i used a European Date Format(dd-mm-yyyy) in my query but the system i use takes USA date Format(mm-dd-yyyy) so my query failed.
**
Solution
**
Select Cast(Case
When EventDate <='01/01/2016' then 'PreSeason'
When EventDate >='12/31/2016' then 'PostSeason'
Else 'Season'
End As Varchar(25)) as Season, EventName From tblEvent
Thanks for Support!!!!

Related

How to get the Date of the first day of a week given a time stamp in Hadoop Hive?

Besides writing a custom UDF to support this issue, is there any known methods of achieving this? I'm currently using Hive 0.13.
Starting with Hive 1.2, you can also do it like this:
select next_day(date_sub('2019-01-01', 7), 'MON')
Output:
2018-12-31
date_sub(m.invitationdate,pmod(datediff(m.invitationdate,'1900-01-07'),7))
This expression gives the exact solution to my question.
Regards,
Boris
This is the easiest and the best solution for fetching 1st day of the week's date:
For Current timstamp:
select date_sub(from_unixtime(unix_timestamp()), cast(from_unixtime(unix_timestamp(), 'u') AS int)) ;
For any given date or column:
select date_sub(from_unixtime(unix_timestamp('2017-05-15','yyyy-MM-dd')), cast(from_unixtime(unix_timestamp('2017-05-15','yyyy-MM-dd'), 'u') AS int)) ;
select date_sub(from_unixtime(unix_timestamp(colname,'yyyy-MM-dd')), cast(from_unixtime(unix_timestamp(colname,'yyyy-MM-dd'), 'u') AS int)) ;
Yes, you can do this without writing a UDF. If you look at at the Hive documentation under datetime functions, there is a function from_unixtime() that takes a unix timestamp and a string pattern. A couple of functions down on the documentation page, there is a link that explains the different patterns you can use in this function. So, from your timestamp, you can extract the day of the week and proceed accordingly.
Example Data:
1445313193
1445313100
1445313146
1445040000
1445040023
1445040111
The first three are Monday, 2015-10-19 and the last three are Friday, 2015-10-16.
Query:
select day_of_week
, date_var
, case when day_of_week = 'Sun' then date_var
when day_of_week = 'Sat' then date_sub(date_var, 6)
when day_of_week = 'Fri' then date_sub(date_var, 5)
when day_of_week = 'Thu' then date_sub(date_var, 4)
when day_of_week = 'Wed' then date_sub(date_var, 3)
when day_of_week = 'Tue' then date_sub(date_var, 2)
when day_of_week = 'Mon' then date_sub(date_var, 1)
else NULL
end as first_day_of_week_date
from (
select from_unixtime(timestamp, 'EEE') day_of_week
, from_unixtime(timestamp, 'yyyy-MM-dd') date_var
from db.table ) A
Output:
Mon 2015-10-19 2015-10-18
Mon 2015-10-19 2015-10-18
Mon 2015-10-19 2015-10-18
Fri 2015-10-16 2015-10-11
Fri 2015-10-16 2015-10-11
Fri 2015-10-16 2015-10-11
So, for today it returns yesterday, which was Sunday, and for last Friday, it returns the previous Sunday, the 11th. I am making the assumption that by "first day of a week", you mean Sunday; if not, you can adjust the code to mean Monday. Hope this helps.

Case when using future data 'out of range value '

select
sum(A.[usercount1]) as [2015-01]
,Sum(A.[usercount2]) as [2015-02]
from
(select
startdate
enddate
when '2015-01-31' > U.startdate and '2015-01-31' < U.enddate then count(username)
Else NULL
End as 'usercount1'
,
Case
when '2015-02-31' > U.startdate and '2015-02-31' < U.enddate then count(username)
Else NULL
End as 'usercount2'
FROM U
group by U.enddate, U.startdate, username, contract, u.email, region
)A
Getting Error: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
How do I use dates that are in the future?
So you have invalid dates for Feb.
I would look at EOMONTH (end of month) function https://msdn.microsoft.com/en-us/library/hh213020.aspx
So you can just pass in the 1st of the month and SQL will work out the rest.

T-sql IF Condition date evaluation

I have a simple question regarding T-SQL. I have a stored procedure which calls a Function which returns a date. I want to use an IF condition to compare todays date with the Functions returned date. IF true to return data.
Any ideas on the best way to handle this. I am learning t-sql at the moment and I am more familar with logical conditions from using C#.
ALTER FUNCTION [dbo].[monday_new_period](#p_date as datetime) -- Parameter to find current date
RETURNS datetime
BEGIN
-- 1 find the year and period given the current date
-- create parameters to store period and year of given date
declare #p_date_period int, #p_date_period_year int
-- assign the values to the period and year parameters
select
#p_date_period=period,
#p_date_period_year = [year]
from client_week_uk where #p_date between start_dt and end_dt
-- 2 determine the first monday given the period and year, by adding days to the first day of the period
-- this only works on the assumption a period lasts a least one week
-- create parameter to store the first day of the period
declare #p_start_date_for_period_x datetime
select #p_start_date_for_period_x = min(start_dt)
from client_week_uk where period = #p_date_period and [year] = #p_date_period_year
-- create parameter to store result
declare #p_result datetime
-- add x days to the first day to get a monday
select #p_result = dateadd(d,
case datename(dw, #p_start_date_for_period_x)
when 'Monday' then 0
when 'Tuesday' then 6
when 'Wednesday' then 5
when 'Thursday' then 4
when 'Friday' then 3
when 'Saturday' then 2
when 'Sunday' then 1 end,
#p_start_date_for_period_x)
Return #p_result
END
ALTER PROCEDURE [dbo].[usp_data_to_retrieve]
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF monday_new_period(dbo.trimdate(getutcdate()) = getutcdate()
BEGIN
-- SQL GOES HERE --
END
Thanks!!
I assume you are working on Sql2008. See documentation of IF and CASE keywords for more details.
CREATE FUNCTION dbo.GetSomeDate()
RETURNS datetime
AS
BEGIN
RETURN '2012-03-05 13:12:14'
END
GO
IF CAST(GETDATE() AS DATE) = CAST(dbo.GetSomeDate() AS DATE)
BEGIN
PRINT 'The same date'
END
ELSE
BEGIN
PRINT 'Different dates'
END
-- in the select query
SELECT CASE WHEN CAST(GETDATE() AS DATE) = CAST(dbo.GetSomeDate() AS DATE) THEN 1 ELSE 0 END AS IsTheSame
This is the basic syntax for a T-SQL IF and a date compare.
If you are comparing just the date portion for equality you will need to use:
select dateadd(dd,0, datediff(dd,0, getDate()))
This snippet will effectively set the time portion to 00:00:00 so you can compare just dates. So in use it will look something like this.
IF dateadd(dd,0, datediff(dd,0, fn_yourFunction())) = dateadd(dd,0, datediff(dd,0, GETDATE()))
BEGIN
RETURN SELECT * FROM SOMEDATA
END
Hope that helps!

TSQL DateTime Comparison

What I am trying to do is get a result from sql where the dates are in a certain range but its not working correctly, here is my query.
DECLARE #CurrDate DATETIME
SET #CurrDate = GETDATE()
SELECT dbo.ProductDetails.PartnerID
,dbo.ProductDetails.ProductID
,dbo.Products.ProductName
,StartDate
,EndDate
FROM dbo.ProductDetails
INNER JOIN dbo.Products
ON dbo.ProductDetails.ProductID = dbo.Products.ProductID
WHERE CONVERT(VARCHAR(10),StartDate,111) <= #CurrDate
AND CONVERT(VARCHAR(10),EndDate, 111) >= #CurrDate
but when the Enddate = #CurrDate the row does not show, but if i make that date just one day higher it gets displayed. Am i doing anything wrong? Any advice will do, thanks.
GetDate() returns date and time, while your conversion to varchar strips away the time part (I'm suspecting that's all it's actually supposed to do). So you would need to do the same conversion for #CurrDate.
If what you want is to simply consider the date only (ignoring the time part), you could use DATEDIFF instead of converting to varchar (see here); example:
DECLARE #CurrDate DATETIME
SET #CurrDate = GETDATE()
SELECT dbo.ProductDetails.PartnerID, dbo.ProductDetails.ProductID,
dbo.Products.ProductName , StartDate, EndDate
FROM dbo.ProductDetails INNER JOIN
dbo.Products ON dbo.ProductDetails.ProductID = dbo.Products.ProductID
-- where StartDate is on the same day or before CurrDate:
WHERE DATEDIFF(day, StartDate, #CurrDate) >= 0 AND
-- and where EndDate is on the same day or after CurrDate:
DATEDIFF(day, EndDate, #CurrDate) <= 0
If you want only DATE comparison, without time use the
cast(CONVERT(varchar, StartDate, 112) as datetime)
I am quite sure that the comparison takes into account the time as well as the date, in which case if the dates are the same but the current time is greater than the time being compared to you won't get that row as a result.
So, what you need to do is just extract the date part and compare those.
GETDATE() gives you date and time
if yours column have only date
then
CONVERT(VARCHAR(10),StartDate,111) <= #CurrDate
can give you unexpected result
remember
19.12.2011 14:41 > 19.12.2011 00:00
If you are using SQL 2008 or later, and wanting to compare only the date, not the time, you can also do:
Cast(StartDate as Date)
(This avoids having to convert to a string.)

Problem when extracting year and week number from string in PSQL

Let's say that I have a range of SQL tables that are named name_YYYY_WW where YYYY = year and WW = week number. If I call upon a function that guides a user defined date to the right table.
If the date entered is "20110101":
SELECT EXTRACT (WEEK FROM DATE '20110101') returns 52 and
SELECT EXTRACT (YEAR FROM DATE '20110101') returns 2011.
While is nothing wrong with these results I want "20110101" to either point to table name_2010_52 or name_2011_01, not name_2011_52 as it does now when I concanate the results to form the query for the table.
Any elegant solutions to this problem?
The function to_char() will allow you to format a date or timestamp to output correct the iso week and iso year.
SELECT to_char('2011-01-01'::date, 'IYYY_IW') as iso_year_week;
will produce:
iso_year_week
---------------
2010_52
(1 row)
You could use a CASE:
WITH sub(field) AS (
SELECT CAST('20110101' AS date) -- just to test
)
SELECT
CASE
WHEN EXTRACT (WEEK FROM field ) > 1 AND EXTRACT (MONTH FROM field) = 1 AND EXTRACT (DAY FROM field) < 3 THEN 1
ELSE
EXTRACT (WEEK FROM field)
END
FROM
sub;