Need help writing an expression for time extraction in Qlik - qlikview

I am new to Qlik and my project requires me to create a time field of format HH:MM (AM/PM) from existing time field of format HH:MM:SS (AM/PM).
I am having trouble finding the expression I should write in the data manager to make it happen.
Example:
Existing time: 12:42:23 PM
Format needed: 12:42 PM

You can use an expression like this:
time( time#(SomeField, 'hh:mm:ss TT'), 'hh:mm TT') )
Im using time# function first to "tell" Qlik that the data in SomeField is in format hh:mm:ss TT and then wrapping the result in time function to convert/display the result as time but passing the second (optional) parameter to specify the output format. In our case hh:mm TT (without :ss) part.
(If the second parameter for time function is not provided then Qlik will use the default time format, specified in the TimeFormat variable at the beginning at the script)
Example script:
RawData:
Load
SomeField,
time( time#(SomeField, 'hh:mm:ss TT'), 'hh:mm TT') as TransformedField
;
Load
'12:42:23 PM' as SomeField
AutoGenerate(1)
;
and the result:

Related

Convert date to ISO 8601 in SQL Server FOR JSON PATH?

I am converting the data into json (using FOR JSON PATH) in SQL Server, one of the column in that data has a datetime datatype which I need to convert to ISO8601
Correct format: 2018-11-13T20:20:39+00:00
When I tried CONVERT(NVARCHAR(33), datetimecolumn, 127), the result is: 2019-10-24T12:35:12.870
When I tried TODATETIMEOFFSET(datetimecolumn, '+00:00'), the result is: 2015-03-31T00:00:00Z
In both cases I am not getting +00:00.
It appears that SQL Server represents a zero offset or UTC time with Z.
If you were to run SELECT TODATETIMEOFFSET(GETDATE(),60), which grabs the current time and applies a 1 hour offset, you would get the format you are expecting. However, I believe that Z denoting UTC is perfectly acceptable in ISO8601, but could be mistaken.
This command seems to output the JSON as expected.
SELECT TODATETIMEOFFSET(GETDATE(),60) as T FOR JSON PATH
If it is vital that the zero offset be represented as 00:00, I believe you will be force to work with strings.
select CAST(CONVERT(NVARCHAR(33), getdate(), 127) as NVARCHAR(35)) + '+00:00' as T
FOR JSON PATH
JSON (as a data-interchange format) doesn't specify the format for date and time values. FOR JSON implicitly converts date and time values to strings, but you can cast the datetime values in your statement with the appropriate type and style before using FOR JSON:
Statement:
SELECT CONVERT(varchar(30), CONVERT(datetimeoffset(0), GETDATE()), 126) AS T
FOR JSON PATH
Result:
[{"T":"2020-04-22T08:02:58+00:00"}]
If you want to convert to a specific time zone, you may try to use AT TIME ZONE:
Statement:
SELECT CONVERT(nvarchar, CONVERT(datetime2(0), GETDATE()) AT TIME ZONE 'Central European Standard Time', 126) AS T
FOR JSON PATH
Result:
[{"T":"2020-04-24T08:33:49+02:00"}]

Convert 2017-01-13T06:00:00Z time format to yyyy-mm-dd hh:mm:ss

I'm trying to convert time formats. The database table has time stored in a VARCHAR(50) column in the format of "yyyy-mm-ddThh:mi:ssZ". When running a query, I want the time formatted as "yyyy-mm-dd hh:mi:ss". (I don't care about time zone adjustment.) I don't understand why GETDATE with the style 120 returns what I want, but when I use the field name it does nothing.
SELECT
PlateEffectiveFrom as Value_In_Table,
STUFF(CONVERT(VARCHAR(33),PlateEffectiveFrom, 120),20,4,'') as 'Gets_Rid_of_Z',
CONVERT(varchar(33), PlateEffectiveFrom, 120) AS Does_Nothing,
CONVERT(varchar(33), GETDATE(), 120) AS Format_I_Want
FROM dbo.TVLTagDetails13
WHERE ID = 5
Returns:
Value_In_Table Gets_Rid_of_Z Does_Nothing Format_I_Want
2008-03-12T06:00:00Z 2008-03-12T06:00:00 2008-03-12T06:00:00Z 2019-01-08 11:16:41
The solution to this problem would be to change the data type of the column to DateTime2 if you don't need the time zone data, or DateTimeOffset if you need the time zone data.
Assuming this can't be done, what you do is a simple string manipulation to get from "yyyy-mm-ddThh:mi:ssZ" to "yyyy-mm-dd hh:mi:ss":
SELECT LEFT(REPLACE(PlateEffectiveFrom, 'T', ' '), 19) AS Format_I_Want
FROM dbo.TVLTagDetails13
WHERE ID = 5
Also, following the conversation in the comments - a must read: Aaron Bertrand's Bad habits to kick : choosing the wrong data type

How to convert YYYY-MM-DD HH:MM:SS TO MM-DD-YYYY HH:MM:SS IN SQL Server?

How to convert yyyy-mm-dd hh:mm:ss (in 24 hours format) to dd-mm-yyyy hh:mm:ss (in 24 hours format? I am using Sql Server 2008.
Given Date Format: 2017-12-18 18:16:49 - Its in DateTime format
Required Date Format: 18-12-2017 18:16:49
This Would work in Older SQL Server Versions Also, Converted to datetime first if it's VARCHAR otherwise you can skip that conversion.
SELECT convert(varchar,convert(datetime,'2017-12-18 18:16:49'),105) + ' ' +
convert(varchar(8),convert(datetime,'2017-12-18 18:16:49'),14);
Use this Link as Reference for Date & Time conversion Formats
Try this
SELECT FORMAT(CAST('2017-12-18 18:16:49' AS datetime) , 'dd-MM-yyyy HH:mm:ss')
DECLARE #date DATETIME = '2017-12-18 18:16:49'
SELECT FORMAT(#date,'dd-MM-yyyy HH:mm:ss')
I would advice not change directly in SQL formate, instead you could change your php code where query fetch the date data
for example you could assign your $yourDate object to new dateTime, and use formate function to define the date format:
<?php
$yourDate = new DateTime();
$timestring = $yourDate->format('m-d-Y h:i:s');
echo $timestring;
the output will be: 05-18-2018 05:00:34
You could take reference in this older discussion
This will prevent some bug error might be occurred if your data table has some other date format relative to, and also it is more useful once you need to have change the date format again
Hope this will be help

Datetime issue in odoo10?

in my custom model I defined the fields
time_from = fields.Datetime(string="Time From", default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
time_to = fields.Datetime(string="Time To", default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
tot_time = fields.Char("Time Difference", compute='_get_time')
this is my compute function
#api.depends('time_from', 'time_to')
def _get_time(self):
t1 = datetime.datetime.strptime(self.time_from, '%Y-%m-%d %H:%M:%S')
t2 = datetime.datetime.strptime(self.time_to, '%Y-%m-%d %H:%M:%S')
if t2<t1:
raise ValidationError('Time To must greater than Time From')
time_diff = (t2-t1)
self.tot_time = time_diff
This is success fully prints time difference.
Time from and time to are mm/dd/yyyy hh:mm:ss format.
How to change this to mm/dd/yyyy hh:mm:ss format
I changed the format like this '%Y-%d-%m %H:%M:%S' .
but it is not getting correct result.
How to change the format?
is it possible to calculate time difference in this format?
The Date and Datetime are saved as strings in the database in a certain format (see the class definition on fields.py. You cannot change the format that is used for the fields in the ORM. If you want to change the format of the date or datetime when you show these fields you can do it not from the code but from:
1) Settings -> Translations -> Find your language and inside you can change the way the fields Date and Datetime are rendered on the client side.
2) If you have a template/report you can use for example<p t-esc="formatLang(time.strftime('%Y-%m-%d %H:%M:%S')" /> or another expression you want to change how the date or datetime will be formed.
3) In the field definition in your xml files you can use custom javascript/widget that will do the rendering.

How to convert time to hh:mm:ss tt in SQL Server

I want to convert time to format like 01:05:33 PM which is saved in database like 13:05:33. What should be the query for that. I want to get 12 hour format basically. In the database it may be saved in 24 hr. It's only time field.
I have used FORMAT function but it gives error that FORMAT() function is not valid.
Use Convert function with style 109
declare #time time ='13:05:33'
select convert(varchar(20),#time,109)
Result : 1:05:33.0000000PM
Note: FORMAT function is introduced in SQL SERVER 2012
The full help on CAST and CONVERT functions is here:
Using your specific example, this should work:
DECLARE #testTime time;
SET #testTime = '13:05:33';
SELECT CONVERT(varchar(20), #testTime, 130);