Alternative to using FORMAT in SQL Server 2008 R2 - sql-server

I created a SELECT using the following in SQL Server 2012:
SELECT
CAST(FORMAT(CONVERT(DATETIME, date_time, 127), 'yyyy-MM-ddTHH:mm:ssZ') AS NVARCHAR(20)) TimeStamp,
FROM myTable
This will result in a date formatted like 2019-03-15T13:25:19Z
How can I achieve the same result using SQL Server 2008 R2 or older?

You can achieve this far more easily by just using CONVERT:
SELECT CONVERT(varchar(19),GETDATE(),127) + 'Z';
As I mentioned in my comment FORMAT is actually an awful function, it performs terribly. I posted an answer to another question earlier today on just how badly it does compared to a CONVERT. Don't just use this expression on your 2008- servers, replace the FORMAT expression on your 2012+ servers with this one too.

I think this does what you want:
select replace(convert(varchar(255), getdate(), 120), ' ', 'T') + 'Z'
Code 127 returns milliseconds, which you do not seem to want, so 120 seems more appropriate.

Related

Need Help Converting Oracle Query to SQL Server

Several weeks ago I made a post to get help with converting a comma delimited list of values into a format that it could be used as part of an IN clause in Oracle. Here is a link to the post.
Oracle invalid number in clause
The answer was to split up the list into an individual row for each value. Here's the answer that I ended up using.
SELECT trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str
FROM ( SELECT '1,2,3,4' str FROM dual )
CONNECT BY instr(str, ',', 1, LEVEL - 1) > 0
Is there a way that I can do something similar in SQL Server without having to create a custom function? I noticed that there's a STRING_SPLIT function, but I don't seem to have access to that on this SQL Server.
Any advice you might have would be greatly appreciated. I've been trying to take a stab at this for the majority of the day.
String_split function is available in MS SQL Server starting from version 2016. If you use older version you can write a few lines of code which do the same.
declare #str varchar(100)='1,2,3,4' --initial string
;with cte as (--build xml from the string
select cast('<s>'+replace(#str,',','</s><s>')+'</s>' as xml) x
)
--receive rows
select t.v.value('.[1]','int') value
from cte cross apply cte.x.nodes('s') t(v)

Convert Access query to SQL

I am converting Access query to SQL Server.
I want to convert below lines to SQL
1. Format (210.6, "Standard")
2. Format (210.6, "#,##0.00")
How do i convert it to SQL query.
I have tried with below, but still not able to find the solution.
For the first query, i found below solution, which is correct
1. CONVERT(varchar, CAST(tSRO.OutputF11 AS money), 1)
Now, for second query, i do not know what i have to do.
From SQL Server 2012+ you can use FORMAT:
SELECT FORMAT(210.6, '#,##0.00') -- 210.60
SELECT FORMAT(1210.6, '#,##0.00') -- 1,210.60
LiveDemo
SQL Server before 2012:
SELECT REPLACE(CONVERT(VARCHAR,CONVERT(MONEY, 1210.6),1),'.00','') -- 1,210.60
LiveDemo2
Warning:
This operation is pure for presentation layer and should be done in application.

How to convert GetDate() to "YYYY-MM-DD-HHMM" format?

I've been looking for the answer and could not find the exact one satisfying my needs.
I'm looking for the way to convert Datetime value into YYYY-MM-DD-HHMM format.
I tried that one:
select CONVERT(varchar(20),GETDATE(), 120) + replace(convert(varchar(5),getdate(),108),':','')
But it did not give me the right results
Is there any practical way to do it?
In SQL Server 2012 or later you can use the built-in Format() function for this:
Select Format(GetDate(), N'yyyy-MM-dd-HHmm')
This works for me:
select CONVERT(varchar(10),GETDATE(), 20) + '-' + replace(convert(varchar(5),getdate(),108),':','')
I'm not sure if this is efficient or not, though

Convert oracle date string to SQL Server datetime

In a SQL Server 2000 DB, I have a table which holds string representations of Oracle DB dates. They are formatted like "16-MAY-12". I need to convert these to datetime. I can not seem to find a conversion style number that matches, nor can I find a function that will allow me to specify the input format. Any ideas?
This seems to work for me:
SELECT CONVERT(DATETIME, '16-MAY-12');
You can also try using TO_CHAR() to convert the Oracle values to a more SQL Server-friendly format (best is YYYYMMDD) before pulling them out of the darker side.
Follow Aaron's advice and cast to string on the Oracle side and then did a check/recast on the MS SQL side. See example below:
;WITH SOURCE AS (
SELECT * FROM openquery(lnk,
'SELECT
TO_CHAR(OFFDATE , ''YYYY-MM-DD HH24:MI:SS'') AS OFFDATE,
FROM
ORACLE_SOURCE')),
SOURCE_TRANSFORM AS
(
SELECT
CASE
WHEN ISDATE(OFFDATE) = 1 THEN CAST(OFFDATE AS DATETIME)
ELSE NULL END AS OFFDATE
FROM
SOURCE
)
SELECT * FROM SOURCE_TRANSFORM

Help to convert PostgreSQL dates into SQL Server dates

Hello I'm doing some data conversion from PostgreSQL to Microsoft SQL Server. So far it has all went well and I almost have the entire database dump script running. There is only one thing that is now messed up: dates.
The dates are dumped to a string format. These are two example formats I've seen so far: '2008-01-14 12:00:00' and the more precise '2010-04-09 12:23:45.26525'
I would like a regex (or set of regexs) that I could run so that will replace these with SQL Server compatible dates. Anyone know how I can do that?
The first is compatible with datetime, but the second is too precise. It will fit in sqldatetime2, which is available from SQL Server 2008:
select cast('2008-01-14 12:00:00' as datetime)
, cast('2010-04-09 12:23:45.26525' as datetime2)
For an earlier version, you can use substring to chop off the unstorable precision:
select cast(substring('2010-04-09 12:23:45.26525',1,23) as datetime)
For a regex to remove any additional digits (using perl regex syntax):
(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3})\d*
And replace with:
$1
Which is matches the regex part between () brackets.

Resources