How to change datetime format in query? - sql

My datetime column is in YYYYMMDDHHmmss format. Datasource of Grafana is SQL Server 2014 with read only access (TRIM() not valid).
How could I filter results in my query using Grafana time filter options?
I thought in using $__timeFrom() and $__timeTo(), but Grafana uses 2022-01-21T06:29:28Z format or unixepoch.
My query needs to convert:
2022-01-21T06:29:28Z -> 20220121062928
[EDIT]
Try 1 (it works):
WHERE s.zeitpunkt
BETWEEN
CAST(REPLACE(REPLACE(REPLACE(REPLACE($__timeFrom(), '-', ''), 'T', ''), ':', ''), 'Z', '') AS VARCHAR(25))
AND CAST(REPLACE(REPLACE(REPLACE(REPLACE($__timeTo(), '-', ''), 'T', ''), ':', ''), 'Z', '') AS VARCHAR(25))
Try 2 (it does not work):
WHERE s.zeitpunkt
BETWEEN CONCAT(
CONVERT(varchar, $__timeFrom(), 112)
, REPLACE(CONVERT(varchar, $__timeFrom(),108),':','')
)
AND CONCAT(
CONVERT(varchar, $__timeTo(), 112)
, REPLACE(CONVERT(varchar, $__timeTo(),108),':','')
)

If you are having trouble with date format of sql server you can do a number of things to change date format. For Example SSMS and SQL Server date format are decided by Your language and region set
You can use CAST(),CONVERT() to change the format of date or set your format from ssms.
You Can follow this link Date and Time Format in SQL
for datetime to datetimewith different formats
set language 'British'
cast(convert(varchar, getdate(), 103) as datetime)
This will change 2022-01-24 00:38:54.840 to 24/01/2022

This is what finally worked:
I Casted the datetime and used FORMAT() with a custom format. The other solutions I tried where a little bit slower.
WHERE s.zeitpunkt
BETWEEN
CAST(
FORMAT(CAST($__timeFrom() AS DATETIME),'yyyyMMddHHmmss')
AS VARCHAR)
AND CAST(
FORMAT(CAST($__timeTo() AS DATETIME),'yyyyMMddHHmmss')
AS VARCHAR)

Related

convert date yyyy-mm-dd to mmm-yy SQL Server 2016

I want to convert date yyyy-mm-dd (stored as a date format) to mmm-yy format.
There are no exact matches in the prior questions on the site.
I have tried substrings and convert function, was considering creating a scalar function but its taking me a while and hoping someone has an easy solution.
You can construct the format using string operations:
select left(datename(month, datecol), 3) + '-' + right(datename(year, datecol), 2)
Or using format():
select format(datecol, 'MMM-yy')
Try this
select replace(right(convert(varchar(9), getdate(), 6), 6), ' ', '') asDate

T-SQL - Convert datetime to unseparated ISO value

I'm trying to convert the following DATETIME into ISO format:
-- Today's Date
2018-04-16 2:04PM
I plan on parsing this into a char value as NVARCHAR so that I can concatenate it as part of a string.
Desired result should be:
-- Date and time unseparated
20180416140422
After some research I discovered here that
CONVERT(datetime, GETDATE(), 112)
using the 112 format code should get me the format I want but somehow I get the following sample output:
--Formatted using 112 format
Apr 16 2018 2:04PM
Why is this? I simply want to format the DATETIME object unseparated.
Also how would I do this with or without the time tagged onto the end?
Using SQL Server 2008R2
It should be :
select CONVERT(varchar(20), GETDATE(), 112)
Output
20180416
If you want date & time both, then can try using this :
select CONVERT(varchar(20), GETDATE(), 112) + replace(CONVERT(varchar(20), GETDATE(), 108), ':', '')
Output
20180416193327
Something like:
--20180416152520
SELECT REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(19), GETDATE(), 121), '-', ''), ':', ''), ' ', '') AS DesiredFormat
Output:
20180416152520
Check this out:
SELECT CONVERT(varchar(20), GETDATE(), 112) AS ISODate
, CONVERT(varchar(50), GETDATE(), 126) AS ISOWithTime_WorksWithAnyLanguageSetting;
I believe the names of the columns are self-explanatory.
Output example:
ISODate | ISOWithTime_WorksWithAnyLanguageSetting
------------------------------------------------------
20180416 | 2018-04-16T15:26:50.607
For information on more formats of the CONVERT function with examples check this source.
These all seem a bit overly cumbersome when all you need is format():
select format(getdate(), 'yyyyMMddHHmmss') as ISODateTime;

Azure Stream Analytics - Split date and time

I am trying to split up the date and time components for a ASA query and am having some difficulties. I did try and use the concat, datepart and dateadd functions to achieve this but it comes up with a Query compilation failed error. Any ideas on what I am doing wrong or another better way of achieving this?
ConCat(DatePart(hh,DateAdd(Hour,11,System.Timestamp)),':',DatePart(mm,DateAdd(Hour,11,System.Timestamp))) as Time,
Thanks
Concat function takes string as input, you will have to cast the output of datepart() to string. Below should work.
select
concat(
cast( datepart(yy,System.Timestamp) as nvarchar(max)),
'-',
cast( datepart(mm,System.Timestamp) as nvarchar(max)),
'-',
cast( datepart(dd,System.Timestamp) as nvarchar(max))) [Date],
concat(
cast( datepart(hh,System.Timestamp) as nvarchar(max)),
':',
cast( datepart(ss,System.Timestamp) as nvarchar(max))) [Time]
into outputStore from inputSource
You have two possible solutions for this:
1. Use T-SQL like syntax
DATEADD(dd, DATEDIFF(dd, '0001-01-01 00:00:00', [TimeStamp]), '0001-01-01 00:00:00')
2. Use build in DateTime builder
DATETIMEFROMPARTS(DATEPART(year,[TimeStamp]),DATEPART(month,[TimeStamp]),DATEPART(day,[TimeStamp]),00,00,00,00)

SQL convert datetime statement

Does anyone know how do I convert the following SQL Statement into ddmmyy without has any delimiter?
convert(varchar(10), '2015-06-01 00:00:00.000', ??)
For example 2015-06-01 (yyyy-mm-dd) want to convert as 010615 (ddmmyy).
You can use Convert function providing it with the style:
SELECT REPLACE(CONVERT(varchar(10), GETDATE(), 5), '-', '')
You could use something like this
SELECT (REPLACE(CONVERT(nchar(8), GETDATE(), 3), '/', ''))
SELECT FORMAT(GETDATE(), 'ddMMyy')
Tested with MS SQL Server 2012. You can use a custom format of your choice.

how to format getdate into YYYYMMDDHHmmSS

In SQL Server how do I format getdate() output into YYYYMMDDHHmmSS where HH is 24 hour format?
I've got the YYYYMMDD done with
select CONVERT(varchar,GETDATE(),112)
but that is as far as I got.
Thanks.
Just for anyone searching for this functionality that has SQL Server 2012 you can use the FORMAT function:
SELECT FORMAT ( GETDATE(), 'yyyyMMddHHmmss') AS 'Custom DateTime'
This allows any .NET format strings making it a useful new addition.
select replace(
replace(
replace(convert(varchar(19), getdate(), 126),
'-',''),
'T',''),
':','')
Close but not exactly what you are asking for:
select CONVERT(varchar, GETDATE(), 126)
e.g.
2011-09-23T12:18:24.837
(yyyy-mm-ddThh:mi:ss.mmm (no spaces), ISO8601 without timezone)
Ref: CAST and CONVERT
There is no way to specify a custom format with CONVERT(). The other option is to perform string manipulation to create in the format you desire.
Try this:
select CONVERT(varchar, GETDATE(), 120)
e.g.
2011-09-23 12:18:24
(yyyy-mm-dd hh:mi:ss (24h) ,ODBC canonical).
Hth.
Another option!
SELECT CONVERT(nvarchar(8), GETDATE(),112) +
CONVERT(nvarchar(2),DATEPART(HH,GETDATE())) +
CONVERT(nvarchar(2),DATEPART(MI,GETDATE())) +
CONVERT(nvarchar(2),DATEPART(SS,GETDATE()));
converting datetime that way requires more than one call to convert. Best use for this is in a function that returns a varchar.
select CONVERT(varchar,GETDATE(),112) --YYYYMMDD
select CONVERT(varchar,GETDATE(),108) --HH:MM:SS
Put them together like so inside the function
DECLARE #result as varchar(20)
set #result = CONVERT(varchar,GETDATE(),112) + ' ' + CONVERT(varchar,GETDATE(),108)
print #result
20131220 13:15:50
As Thinhbk posted you can use select CONVERT(varchar,getdate(),20) or select CONVERT(varchar,getdate(),120) to get quite close to what you want.
select CONVERT(nvarchar(8),getdate(),112) +
case when Len(CONVERT(nvarchar(2),DATEPART(HH,getdate()))) =1 then '0' + CONVERT(nvarchar(2),DATEPART(HH,getdate())) else CONVERT(nvarchar(2),DATEPART(HH,getdate())) end +
case when Len( CONVERT(nvarchar(2),DATEPART(MI,getdate())) ) =1 then '0' + CONVERT(nvarchar(2),DATEPART(MI,getdate())) else CONVERT(nvarchar(2),DATEPART(MI,getdate())) end