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)
Related
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)
Any way to convert this varchar -> "18-Jan-2015 12:43:51" to datetime in SQL database?
Thanks.
Just like everybody said above, the best way to do it is either with convert or cast, besides, this is very basic sql
here are both cases:
SELECT CAST('18-Jan-2015 12:43:51' AS DATETIME) AS DATE
SELECT CONVERT (DATETIME, REPLACE('18-Jan-2015 12:43:51', '-', ' '))
Use convert with replace:
Select convert(datetime, replace('18-Jan-2015 12:43:51', '-', ' '), 113)
Try this:
SELECT (CONVERT(DATETIME,LEFT('18-Jan-2015 12:43:51',23),101))
You can use statement like below
Select convert(datetime, replace('18-Jan-2015 12:43:51', '-', ' '))
SELECT CAST('18-Jan-2015 12:43:51' AS DATETIME)
I'm trying to get my EntryDate column in this format 'YYYY_m' for example '2013_04'.
This code has been unsuccessful
DATENAME (YYYY, EntryDate) + '_' + DATEPART (M, EntryDate)
Attempts using DATEFORMAT have also been unsuccessful, stating there was syntax error at ',' after the M. What code would work instead?
Thank you.
How about date_format()?
select date_format(EntryDate, '%&Y_%m')
This is the MySQL way. Your code looks like an attempt to do this in SQL Server.
EDIT:
The following should work in SQL Server:
select DATENAME(year, EntryDate) + '_' + RIGHT('00' + DATEPART(month, EntryDate), 2)
Personally, I might use convert():
select replace(convert(varchar(7), EntryDate, 121), '-', '_')
select DATENAME (YYYY, EntryDate)
+ '_'
+ right('0' + convert(varchar(2),datepart (MM, EntryDate)), 2)
You have to convert the result of DATEPART() to a character string in order for the + to perform an append.
FYI - in the future "unsuccessful" doesn't mean anything. Next time post the actual error you are receiving.
For example:
SELECT concat(EXTRACT(YEAR FROM '2015/1/1'), '_',
LPAD(extract(month from '2014/1/1'),2,'0')) AS OrderYear
This uses
concat to combine strings
lpad to place a leading 0 if month is one digit
and uses extract to pick of the part of date needed.
working fiddle
http://sqlfiddle.com/#!2/63b24/8
DATEPART
It is a Datetime function which extract information from date. This function always returns result as integer type.
SELECT DATEPART(month, '2009-01-01 00:00:00:000') as month
it is return "1" as an integer:
DATENAME
It is also another Datetime function which to extract information from date. This function always returns result as varchar
SELECT DATENAME(month, '2009-01-01 00:00:00:000') as month
it is return "January".
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
I need to convert a date in SQL. The date as is 15-May-2019 and it should display as 2019/05/15.
This is the code I have so far
CASE WHEN WHEN LEN(AgeGroup) > 1 THEN PATINDEX ('%[A-Z]%', date)
I'm not completely sure how to use Patindex. Can someone please help me to fix this?
I would recommend just converting to the date data type:
select try_convert(date, '15-May-2019')
If you want it with slashes, you can produce a string instead:
select replace(convert(varchar(10), try_convert(date, '15-May-2019'), 120), '-', '/')
Use the below code and you can refer the link for other formats.https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/
SELECT CONVERT(varchar,(cast('15-may-2019' as date)), 111)
PATINDEX is slightly overkilling.
Try:
SELECT FORMAT(
TRY_PARSE('15-May-2019' AS DATE USING 'en-US')
, 'yyyy/MM/dd')
TRY_PARSE parses the string to DATE using English culture.
FORMAT is to precisely control the output