migrate dateadd and datepart from sybase to oracle - database

I tried to migrate this sentence sql from Sybase to Oracle but I don't now how to implement
dateadd and datepart.
select #v_date = convert(char,convert(char(4),datepart(year, dateadd(month, -13, getdate()))) + "/" +
right(( "00" + convert(varchar(2), datepart(month, dateadd(month, -13, getdate())))) , 2) + "/01", 112)
I have searched and know Oracle uses interval but I still don't understand how.

I'm not a Sybase person, but I'm working on the assumption from the code that you want the start of the month that was 13 months ago.
In which case, its:
SQL> select trunc(add_months(sysdate,-13),'MM') from dual;
TRUNC(ADD
---------
01-OCT-20

Related

Migrate Oracle view to SQL Server

I had used this as Where Condition in Oracle
(ProjectDate between trunc(sysdate-1)+15/24 and trunc(sysdate)+8/24)
I was trying to convert view to SQL Server and I used to try
ProjectDate between (GetDate()-1)+15/24 and (GetDate())+8/24
I am not sure if using right function or not?
You need to use following expression:
ProjectDate between
DATEADD(HOUR,15,CONVERT(DATETIME, CONVERT(DATE, GETDATE())) - 1)
and DATEADD(HOUR,8,CONVERT(DATETIME, CONVERT(DATE, GETDATE())))
DATEADD: The DATEADD() function adds a time/date interval to date and then returns the date.
CONVERT(DATETIME, CONVERT(DATE, GETDATE())) returns the same as TRUNC(SYSDATE) in oracle

slice data between the range of dates SQL Server

I am having a miserable time in achieving a simple task. I want to slice data for every 6-months every month. I am not getting any output. My SQL skills are very bad. I searched for the solution a lot and they gave me some idea but I am not able to any output.
Below is my attempt:
SELECT TOP 10000 CONVERT(VARCHAR,DATEADD(HOUR,-4,DATEADD(s, ch.dateTimeOrigination, '19700101')), 121) as CallDate
FROM [dbo].[CallData] AS ch
WHERE LEN(ch.callingPartyNumber) = 4 AND
CONVERT(VARCHAR,DATEADD(HOUR,-4,DATEADD(s, ch.dateTimeOrigination, '19700101')), 121) BETWEEN CONVERT(VARCHAR(10), GETDATE(), 110) AND CONVERT(VARCHAR(10), DATEADD(month, -6, GETDATE()), 110)
The table definitely has the data for the time period I am trying to query. So I am not sure why this is not giving me any output. I will really appreciate your help. Thank you.
I believe your BETWEEN values are backwards.
EDIT: Your CONVERT statements in the WHERE clause are also different formats, so when you compare your varchars, the comparison fails. You should instead use DATETIME.
Try this:
SELECT TOP 10000
CONVERT(VARCHAR,DATEADD(HOUR,-4,DATEADD(s, ch.dateTimeOrigination, '19700101')), 121) as CallDate
FROM [dbo].[CallData] AS ch
WHERE LEN(ch.callingPartyNumber) = 4 AND
CAST(DATEADD(HOUR,-4,DATEADD(s, ch.dateTimeOrigination, '19700101')) AS DATETIME) BETWEEN DATEADD(month, -6, GETDATE()) AND GETDATE()

Get hour:minutes from DATETIME2

I have a problem in SQL Server 2008 R2. I want to get HH:mm from a DATETIME2 column. When I write this statement:
SELECT CONVERT(VARCHAR(5), getdate(), 8)
I am getting perfect result that is 11:19, but when I try to get time from:
SELECT (CONVERT(VARCHAR(5), '1900-01-01 01:10:00.0000000', 8))
I am getting result 1900 but I need 01:10.
How can I get the result from this statement? I know there there are multiple questions/answers like this but I have not found any answer that matches my question.
On SQL Server side (in stored procedure, for example), you can use this kind of conversion:
declare #dt datetime2(0) = sysdatetime();
select #dt as [Date], left(convert(time(0), #dt, 108), 5) as [HH:mm];
If you need to display your data on the client side, such as a report or your app, it's better to use client' formatting capabilities. This way, you may be able to take such factors into account as preferred calendar system, 12- or 24-hour clock, etc.
Try this:
SELECT RIGHT('0' + CONVERT(VARCHAR(2),
DATEPART(HOUR, '1900-01-01 01:10:00.0000000')), 2) hh,
DATEPART(minute, '1900-01-01 01:10:00.0000000') mm
Or
SELECT LEFT(CONVERT(TIME(0), '1900-01-01 01:10:00.0000000'), 2) hh , DATEPART(minute, '1900-01-01 01:10:00.0000000') mm
For many format of 24H datetime length is 19 so you can use the following
SELECT RIGHT('0' + CAST(DATEPART(HOUR, Substring('1900-01-01 01:10:00.0000000',1,19)) AS VARCHAR(2)), 2) + ':' + CAST(DATEPART(minute, substring('1900-01-01 01:10:00.0000000',1,19)) AS VARCHAR(2)) HHmm
Hope it helps
Try this, I think this is what you want.
this will give answer: 1:10
SELECT CONVERT(VARCHAR(5),CONVERT(DATETIME,'1900-01-01 01:10:00.000'),8)
You can also try this
select convert(VARCHAR,DATEPART(HOUR,'1900-01-01 01:10:00.0000000'))+':'+CONVERT(VARCHAR,DATEPART(MINUTE,'1900-01-01 01:10:00.0000000'))

Format date as 'DD Month YYYY'

Just have a mssql query that has a date in the format:
'2016-03-22 00:00:00.000'
I need to format it as:
'22 March 2016'
I'm using SQL Server 2012. I've tried googling and the usual 106, 112 codes don't seem to work.
Is there a specific code format I can use?
Try the FORMAT function:
SELECT FORMAT(GETDATE(), 'D', 'en-gb')
If your version does not support the FORMAT function, you can do it by concatenating the date parts:
SELECT
RIGHT('00' + CAST(DATEPART(DAY, GETDATE()) AS VARCHAR(2)), 2) + ' ' +
DATENAME(MONTH, GETDATE()) + ' ' +
CAST(DATEPART(YEAR, GETDATE()) AS VARCHAR(4))
Use Format Function in SQL Server 2012
SELECT FORMAT(GETDATE(),'dd-MMMM-yyyy')
See Image for your datatype ref
Try this query:
SELECT CONVERT(VARCHAR, CONVERT(DATETIME, '2016-03-22 00:00:00.000'), 106)
You need to convert into DATETIME then you will get your desired output
More Datetime Sql formate
SQLFiddle
This may help you
Try Convert function for SQL server:
select convert(varchar(11),cast('2016-03-22 00:00:00.000' as datetime), 106)
How to change day, month, year in PHP:
echo $purchase_date=date('F-Y',strtotime('2022-03-22 00:00:00.000'));
Answer:
March-2022

Format date as Month dd, yyyy?

In SQL Server, I'd like to select a date from a DateTime column in Month dd, yyyy format.
I currently am using the following code:
SELECT CONVERT(VARCHAR(12), DateColumnHere, 107) 'Formatted Date'
Which returns:
Feb 15, 2013
Is there any way to get the full month name, like:
February 15, 2013
Thanks!
Here is a site with a ton of formats and little date hacks for SQL:
http://www.sql-server-helper.com/tips/date-formats.aspx
And here is their query for your format (Month dd, yyy):
SELECT DATENAME(MM, GETDATE()) + RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY]
SQL Server has a limited number of date formats, but you can use the DATENAME function to concatenate the peices:
SELECT DATENAME(MM, GETDATE()) + ' ' +
DATENAME(DD, GETDATE()) + ', ' +
DATENAME(YYYY, GETDATE())
AS 'Formatted Date'
However in general it's often better practice to leave dates in date format rather than converting to strings, and only converting to strings in the display layer (Web site, report, application, wherever) where the formatting capabilities are much richer. Plus you lose the ability to do date math, sorting, etc. if you convert to string.
SELECT DATENAME(MM, GETDATE()) + RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY]
Try with this:
SELECT STUFF(CONVERT(VARCHAR(12), GETDATE(), 107), 1,3,DATENAME(month,GETDATE()))

Resources