Convert oracle date string to SQL Server datetime - sql-server

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

Related

convert sql server AlphaToDate4 to oracle date which has 5 numbers

Hi good day I have the following code I am trying to convert from sql server to oracle, It works perfectly on sql but not on oracle. How can I convert the ff?
This is the code from sql
Select CONVERT(VARCHAR, RLL.dbo.AlphaToDate4(A.DATE_R), 103) CaptureDate
from Employees
This is the format of Date_R (78684)
I am trying to write the following code in oracle but gives me an error
select TO_DATE((A.DATE_R),'DD/MM/YYYY') CAPTUREDATE
Assuming that your number represents the number of days from 31/12/1799 (are you sure?), you may need to add the column value to this date, that is
select date '1799-12-31' + date_R
If you need a formatted string:
select to_char( date '1799-12-31' + date_R, 'dd/mm/yyyy')

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.

SQL Server 2008: how to format the output as a currency

I have a query string which returns a value that has several decimal places. I want to format this to a currency $123.45.
Here is the query:
SELECT COALESCE(SUM(SUBTOTAL),0)
FROM dbo.SALESORD_HDR
where ORDERDATE = datediff(d,0,getdate())
and STATUS NOT IN (3,6)
I want the result in a currency with 2 decimal places.
If you are looking for a "true" Currency format, similar to what can be achieved via the FORMAT function that started in SQL Server 2012, then you can achieve the exact same functionality via SQLCLR. You can either code the simple .ToString("C" [, optional culture info]) yourself, or you can download the SQL# library (which I wrote, but this function is in the Free version) and use it just like the T-SQL FORMAT function.
For example:
SELECT SQL#.Math_FormatDecimal(123.456, N'C', N'en-us');
Output:
$123.46
SELECT SQL#.Math_FormatDecimal(123.456, N'C', N'fr-fr');
Output:
123,46 €
This approach works in SQL Server 2005 / 2008 / 2008 R2. And, if / when you do upgrade to a newer version of SQL Server, you have the option of easily switching to the native T-SQL function by doing nothing more than changing the name SQL#.Math_FormatDecimal to be just FORMAT.
Putting this into the context of the query from the original question:
SELECT SQL#.Math_FormatDecimal(COALESCE(SUM(SUBTOTAL),0), N'C', N'en-us') AS [Total]
FROM dbo.SALESORD_HDR
where ORDERDATE = datediff(d,0,getdate())
and STATUS NOT IN (3,6)
EDIT:
OR, since it seems that only en-us format is desired, there is a short-cut that is just too easy: Converting from either the MONEY or SMALLMONEY datatypes using the CONVERT function has a "style" for en-us minus the currency symbol, but that is easy enough to add:
SELECT '$' + CONVERT(VARCHAR(50),
CONVERT(MONEY, COALESCE(SUM(SUBTOTAL), 0)),
1) AS [Total]
FROM dbo.SALESORD_HDR
where ORDERDATE = datediff(d,0,getdate())
and STATUS NOT IN (3,6)
Since the source datatype of the SUBTOTAL field is FLOAT, it first needs to be converted to MONEY and then converted to VARCHAR. But, the optional "style" is one reason I prefer CONVERT over CAST.
Here's a suggestion of the syntax to use to future-proof the search query.
select format(123.56789,'C2','en-US') --$123.57 ;
select format(123.56789,'C3','en-US') --$123.568;
select format(123.56789,'C0','en-US') --$124

How to convert MySQL to SQL Server query to display proper date from timestamp?

I need the following query below converted for SQL Server 2005, so please let me know what I should change since SQL Server doesn't support DATE_FORMAT()
How it should look converted in SQL Server so I can display proper date on my website instead of timestamp (example: 1382016108 to be 2013-06-22 10:53:22) ?
SELECT DATE_FORMAT(TimeTransfer,'%Y-%m-%d %h:%i:%s')
FROM PREMIUM where strAccountID = ?, $_SESSION['strAccountID']);
EDIT: I checked the CONVERT() function but couldn't find out how it should be exactly in my case with my query. Help is greatly appreciated.
This will work:
SELECT CONVERT( VARCHAR(20) -- Make sure the output string is long enough
, CAST('1970-1-1' As DateTime) -- The Base Date
+ CAST(TimeTransfer As Float)/(24*60*60)
-- Scale UTC (seconds) to TSQL datetime (days)
-- by dividing by the number of seconds in a day
, 120) -- The format that you want
Check the Convert function. It allows you to give format to a datetime value
SELECT CONVERT(VARCHAR(20),TimeTransfer,120)
FROM PREMIUM
WHERE strAccountID = ?, $_SESSION['strAccountID']);
Use CONVERT() function
SELECT CONVERT(varchar(20),TimeTransfer,120)
FROM PREMIUM where strAccountID = ?, $_SESSION['strAccountID']);
Note : This gives time in 24 hour format
See this link for Date formats in SQL Server

How do I convert eg '22/03/2005' to a datetime in SQL Server

Its SQL Server 2000.
I am starting with a character string in the format DD/MM/YYYY
Here's the table: http://msdn.microsoft.com/en-us/library/ms187928.aspx
Sample:
select convert(datetime,'22/03/2005', 103)
SET DATEFORMAT dmy
SELECT CAST('22/03/2005' AS datetime)
or
SELECT convert(datetime,'22/03/2005', 103)
It depends your the context.
SQL Server understands '2010-06-21' as a date without requiring any convert/cast, so I would just use the string in the format 'yyyy-mm-dd' if that suits your needs.
Otherwise, the other responses using cast may be better if you need to compare with date fields containing hours as well.

Resources