convert sql server AlphaToDate4 to oracle date which has 5 numbers - sql-server

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')

Related

Convert datetime to date using a function

I have one column in my database that I need to convert from datetime to the date data type using a SQL Server function. I can't figure out how to get the correct syntax. Could I get some help with the syntax so IntelliSense will stop yelling at me and I can get the query to run?
CREATE FUNCTION fChangeDateFormat (#date01 date)
RETURNS DATE
AS
RETURN(
SELECT
Convert(DateTime, OrderDate, 101)
FROM
Orders
WHERE
OrderDate = #date01
)
You can use convert as,
select CONVERT(date,#date01,101) as dd from Orders
If you are using SQl server 2012 + use FORMAT,
'd' is to get the short date format.
SELECT FORMAT(#date01,'d','en-US') as dd from Orders

Extract only month from SQL Query

I've a SQl Query. I'm using dropdownlist to display the dates. I'd like to display the month in MMMM format.
SELECT DISTINCT[drdates] (CONVERT(CHAR(4), [drdates], 100) + CONVERT(CHAR(4), [drdates], 120)) FROM [DRReceive_20141229]
SELECT DISTINCT UPPER(LEFT(DATENAME(MONTH,MONTH([drdates])),4))
+ CONVERT(CHAR(4), [drdates], 120)
FROM [DRReceive_20141229]
On a side note I have never seen date values being formatted as MMMMyyyy, a rather strange format to show date values.
But if you wanted something rather simple or usual format like MMMyyyy and if you are using sql server 2012 or later version you can do the following ...
SELECT DISTINCT UPPER(FORMAT ( [drdates], 'MMMyyyy' ))
FROM [DRReceive_20141229]
This will help you to retrieve months first four characters :
select CONVERT(CHAR(4),DATENAME(MONTH, [drdates])) + CONVERT(CHAR(4),[drdates], 120)) [DRReceive_20141229];

Inserting CDate to SQL Server 2012 Flip Day and Month

I have a problem inserting a date from a VB.net Program to a SqlServer2012 instance.
First here is how i generate the data (Vb.net)
ExitTime = CDate("1.1.1970 00:00:00").AddSeconds(currentField).ToLocalTime
We add this value to a stored procedure (Vb.net)
With comsql5.Parameters.AddWithValue("#ExitTime", ExitTime)
In the Sql Server stored procedure
#ExitTime datetime, [...]
[...]
Insert into [table] ([ExitTime]) VALUES (#ExitTime)
Here is the output of the exit time in the vb.net
Exit Time : 08/07/2014 2:06:31 PM
Here is the same row in the Sql server database
2014-08-07 14:06:31.000
What I would like to see in the database is 2014-07-08 14:06:31.00
Because another part in the program does a check on the field but as a String... and it does not match because it flip the month and day
EDIT: TO be clear, I can't change the other part that does the comparison as a string. I know this is a poor way to compare datetime.
Thank for your time
Have you tried using the Convert function?
SELECT CONVERT (VARCHAR, getdate(), 121);
Check this links for more information MSDN - CAST and CONVERT and SQL Server Datetime Format

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

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

Resources