I have to buid a timestamp from 3 different fields.
Refer_DT is a TimeStamp that contains the Date.
Refer_Time is a TimeStamp that contains The Time.
REFER_TIME_TYP_CD is an Integer Field which designates the AM or PM.
I need to build a Timestamp from all three fields.
Thanks in Advance.
Select REFER_ID, REFER_DT, REFER_TIME, REFER_TIME_TYP_CD, VarChar_Format(Refer_Time, 'AM') as Indicator,
Case When REFER_TIME_TYP_CD = 2858
Then Case When VarChar_Format(REFER_TIME, 'AM') = 'AM'
Then TimeStamp(Date(REFER_DT), Time(REFER_TIME))
Else TimeStamp(Date(REFER_DT), Time(REFER_TIME - 12 Hours))
End
When REFER_TIME_TYP_CD = 2859
Then Case When VarChar_Format(REFER_TIME, 'AM') = 'PM'
Then TimeStamp(Date(REFER_DT), Time(REFER_TIME))
Else TimeStamp(Date(REFER_DT), Time(REFER_TIME + 12 Hours))
End
End as "Intake Time"
From TREFERRAL
Where REFER_ID = 32351883
With UR
So your timestamp field for date has a date with 00:00:00.0000 for the time?
And the timestamp field with the time has a time with 0001-01-01 for a date?
And apparently the TS with the time only has 0-12 hrs...
select
timestamp(date(Refer_DT)
, case
when REFER_TIME_TYP_CD = 1
then time(refer_time) + 12 hours
else
time(refer_time)
end
)
from mytable
You can simplify your query somewhat:
Select REFER_ID, REFER_DT, REFER_TIME, REFER_TIME_TYP_CD, VarChar_Format(Refer_Time, 'AM') as Indicator,
Case When REFER_TIME_TYP_CD = 2858 And VarChar_Format(REFER_TIME, 'AM') = 'PM'
Then TimeStamp(Date(REFER_DT), Time(REFER_TIME - 12 Hours))
When REFER_TIME_TYP_CD = 2859 And VarChar_Format(REFER_TIME, 'AM') = 'AM'
Then TimeStamp(Date(REFER_DT), Time(REFER_TIME + 12 Hours))
Else TimeStamp(Date(REFER_DT), Time(REFER_TIME))
End as "Intake Time"
From TREFERRAL
Where REFER_ID = 32351883
With UR
Note, however, that you've lost whatever sub-second information you previously had (there are ways to recover this, but you haven't specified if you need it). I'm also curious whether subtracting/adding 12 hours should also subtract/add a day.
And if you do it:
select REFER_ID, REFER_DT, REFER_TIME, REFER_TIME_TYP_CD,
TIMESTAMP(
VARCHAR_FORMAT(REFER_DT, 'YYYY-MM-DD-') ||
VARCHAR_FORMAT(REFER_TIME, case REFER_TIME_TYP_CD = 2859 then 'HH24' else 'HH12' end || '.MI.SS.NNNNNN')
) "Intake Time"
From TREFERRAL
Where REFER_ID = 32351883
With UR
Related
This seems like it should be fairly simple and straight forward but I haven't yet found the right combination. I have a column called last_assess_yr that is an integer. I am trying to find all rows from my_table where '01-01' + 'year' < current_date and give them a value in a new column. I have the following:
SELECT last_assess_yr,
CASE
WHEN format('01-01-%s'::text, last_assess_yr)::timestamp without time
zone < current_date
THEN YES
ELSE NO
END AS assess_value
FROM my__table
but, the results are not correct
You don't actually need to convert the integer into a timestamp, you could just compare the extracted year to the integer.
select
case when last_assess_yr < extract('year' from current_date)::int
then 'YES' else 'NO'
end
However for reference the following will work:
select
case when format('01-01-%s'::text, 2017)::timestamp < current_date
then 'YES' else 'NO'
end
i.e. you do not need to remove time (of day) when you convert a string of '01-01-2017' to a timestamp.
and: I assume YES and NO also need to be treated as literals: 'YES' and 'NO'
In case someone else is looking, this worked for me:
WHEN last_assess_yr > 0 AND format('%s-05-05'::text, w.last_assess_yr)::timestamp <
current_date THEN 'yes'
ELSE 'no'
END
I have a column adetdate which has date in the format '2015-05-01 00:00:00.000'
is it possible to write a case statement like
case adetdate
adetdate between '2015-05-01 00:00:00.000' and '2015-05-01 23:59:59.999' then print 'MAY 1st'
adetdate between '2015-05-02 00:00:00.000' and '2015-05-02 23:59:59.999' print 'MAY 2nd'
If correctly understood. You could convert your DATETIME column to DATE in this case time will be ignored. Something like:
PRINT CASE CAST(adetdate AS DATE)
WHEN '2015-05-01' THEN 'MAY 1'
WHEN '2015-05-02' THEN 'MAY 2'
--........
END AS Something
Another, more dynamic way is to use CONVERT in following:
declare #datetime datetime = '2015-05-01 00:00:00.000'
select upper(convert(varchar(6), #datetime, 7))
OUTPUT
Will be in following format:
MAY 01
Maybe a generic approach like this:
SET LANGUAGE English
DECLARE #d DATETIME=GETDATE();
SELECT FORMAT(#d,'MMM d') + CASE DAY(#d)
WHEN 1 THEN 'st'
WHEN 2 THEN 'nd'
WHEN 3 THEN 'rd'
ELSE 'th' END
If you are using a SQL Server <2012 you might find this helpful:
DECLARE #d DATETIME=GETDATE();
SELECT UPPER(DATENAME(MONTH,#d)) + ' ' + CAST(DAY(#d) AS VARCHAR(2))
+ CASE DAY(#d)
WHEN 1 THEN 'st'
WHEN 2 THEN 'nd'
WHEN 3 THEN 'rd'
ELSE 'th' END
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!!!!
How to find out Number of Workdays(Monday to Friday) between two dates in SAP HANA ? We do not have to consider holidays.
We cant use WORKDAYS_BETWEEN() as we do not have TFACS table.
Here is how you can so ist in sql:
Calculate the number of whole weeks, multiply by 5
Add the remaining days: subtract weekday start date from weekday end date, correct for weekends (least...), Correct for carry-over (+5)
The second part can be simplified a little so that you don't have to write the subtraction twice.
Here an example with start date '2015-12-04' and end date '2015-12-19):
SELECT ROUND( DAYS_BETWEEN (TO_DATE ('2015-12-04', 'YYYY-MM-DD'), TO_DATE('2015-12-19', 'YYYY-MM-DD')) / 7, 0, ROUND_DOWN) * 5
+ ( case
when WEEKDAY (TO_DATE('2015-12-19', 'YYYY-MM-DD') ) - WEEKDAY (TO_DATE('2015-12-04', 'YYYY-MM-DD')) >= 0
then least( WEEKDAY (TO_DATE('2015-12-19', 'YYYY-MM-DD')), 5) - least( WEEKDAY (TO_DATE('2015-12-04', 'YYYY-MM-DD')), 5)
else
least( WEEKDAY (TO_DATE('2015-12-19', 'YYYY-MM-DD')), 5) - least( WEEKDAY (TO_DATE('2015-12-04', 'YYYY-MM-DD')), 5) + 5
end )
"Workdays" FROM DUMMY;
--> 11
I preferred to create a user defined function here to use in HANA SQLScript codes as follows
Create Function CalculateWorkDays (startdate date, enddate date)
returns cnt integer
LANGUAGE SQLSCRIPT AS
begin
declare i int;
cnt := 0;
i := 0;
while :i <= days_between(:startdate, :enddate)
do
if WEEKDAY( ADD_DAYS(:startdate,:i) ) < 5
then
cnt := :cnt + 1;
end if;
i := :i + 1;
end while;
end;
Please note that the above code block seems to contain an unnecessary loop.
On the other hand, if you have additional tables like department holidays, or personal holidays, etc. It might be useful to check these tables in the WHILE loop. Please refer to following SQL tutorial Calculate the Count of Working Days Between Two Dates where I created a similar SQL function checking custom work calendars or holiday calendars.
Here is how you call the function as sample
select CalculateWorkDays('20170101', '20170131') as i from dummy;
I hope it helps,
I have tried Num((today()-I_TRAN_DATE)/90 + 1,0) individually and it will return integer, but it seems not working when I try to combined it with pick function. I know it's not finished but should at least return result for 1-3
pick(
Num((today()-I_TRAN_DATE)/90 + 1,0)
,'less than 3 months'
,'3-6 months'
,'6-12 months'
,'greater than 1 year'
)
This looks like an issue with passing the number from the expression to the pick function. When using the num function, this does not change the underlying value - including a few more brackets and a round function resolves the issue as per the below script which generates a list of dates and then applies the pick function at the end.
Let varMinDate = Num(31350); //30/10/1985
Let varMaxDate = Num(42400); //31/01/2016
TempCalendar:
LOAD
$(varMinDate) + Iterno()-1 As Num,
Date($(varMinDate) + IterNo() - 1) as TempDate
AutoGenerate 1 While $(varMinDate) + IterNo() -1 <= $(varMaxDate);
TestCalendar:
Load
TempDate AS I_TRAN_DATE,
week(TempDate) As Week,
Year(TempDate) As Year,
Month(TempDate) As PeriodMonth,
Day(TempDate) As Day,
if(Year2Date(TempDate),1,0) as CurYTDFlag,
if(Year2Date(TempDate,-1),1,0) as LastYTDFlag,
inyear(TempDate, Monthstart($(varMaxDate)),-1) as RC12,
date(monthstart(TempDate), 'MMM-YYYY') as MonthYear,
Week(weekstart(TempDate)) & '-' & WeekYear(TempDate) as WeekYear,
WeekDay(TempDate) as WeekDay,
if(TempDate>=MonthStart(AddMonths(Today(),-12)) and(TempDate<=MonthStart(Today())),1,0) as Rolling12Month
,'Q' & Ceil (Month(TempDate)/3) as Quarter
,Year(TempDate)&'-Q' & Ceil (Month(TempDate)/3) as YearQuarter
,if(Year(TempDate)=(Year(Today())-1),1,0) as LastYear
, if(MonthStart(TempDate)=MonthStart(Today()),null(),'Exclude Current Period') As ExcludeCurrentPeriod
Resident TempCalendar
Order By TempDate ASC;
Drop Table TempCalendar;
Let varMinDate = null();
Let varMaxDate = null();
PeriodTable:
Load
Pick(round((num((((today()-I_TRAN_DATE)/90) + 1),0)),1)
,'less than 3 months'
,'3-6 months'
,'6-12 months'
,'greater than 1 year') as Period
,I_TRAN_DATE
Resident TestCalendar;