简体   繁体   中英

SQL Oracle to Microsoft SQL Server trunc(current_date, 'DD') converting to trunc(current_date, 'DD')

I'm converting from SQL Oracle to Microsoft SQL Server

>= trunc(current_date, 'DD') converting to >= CONVERT(DATE, GETDATE())

But it's not giving me the same result.

My question is what does trunc(current_date, 'DD') look like (What format?)? I'm not able to test it as the old database has been deleted.

Should the conversion be convert(varchar, getdate(), 105) instead?

if you are going to store this as string type in mssql, this is the nearest datetime format that I can think of.

SELECT CONVERT(VARCHAR(50),DATEADD(day, DATEDIFF(day, '19000101', GETDATE()), '19000101'), 126) + 'Z'

Trunc in oracle will always return date as an output.

Trunc by DD will truncate time portion and give the floor date, not the nearest date.

So consider that your_date is 24-sep-2019 05:30:00 PM.

Select trunc(your_date, 'DD') from dual

Will give following result

24-sep-2019 
-- see time portion is truncated 
-- and return type is also date

Now, you can convert this query accordingly in mssql.

Cheers!!

In Sql Server for following query you get the out put as ' 25 Sep 2019 '

select convert(varchar, getdate(), 106)

In Oracle to get the same date output as ' 25 Sep 2019 ' you can use the below query.

SELECT
TO_CHAR(
    TRUNC(TO_DATE( '25 Sep 2019 15:35:32', 'DD Mon yyyy HH24:MI:SS' )),'DD Mon yyyy')
FROM
  dual; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM