Function to Convert Current Date to SQL Sever Date format.

  • Does anyone have a function to Convert Current Date to SQL Sever Date format?

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • SYSDATETIME() will give you current date and time

    CAST(SYSDATETIME() as date) will give you current date only

    What are you referring to with 'Current Date'?

    What do you mean by SQL Server Date format?

    What sort of 'Convert'?

    Far away is close at hand in the images of elsewhere.
    Anon.

  • I want to do the Equivalent of the following SQL Select and return the result listed below:

    SELECT CONVERT(DATE,GETDATE()) AS CurrentDate;

    CurrentDate

    2014-08-29

    Thank you.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Welsh Corgi (8/29/2014)


    I want to do the Equivalent of the following SQL Select and return the result listed below:

    SELECT CONVERT(DATE,GETDATE()) AS CurrentDate;

    CurrentDate

    2014-08-29

    Thank you.

    Equivalent in Oracle?

    SELECT SYSDATE AS Datetype,

    TRUNC(SYSDATE) AS NoTimeDate,

    TO_CHAR(SYSDATE, 'YYYY-MM-DD') AS StringDate

    FROM Dual;

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Your query is returning a date datatype, the gui interface (SSMS) is displaying as characters formatted as to the language of the login.

    So

    SELECT CONVERT(DATE,GETDATE()) AS CurrentDate;

    and

    SELECT CONVERT(varchar(10),GETDATE(),120) AS CurrentDate;

    Both show 2014-08-29 in SSMS but the first is a date datatype and the second is varchar (string) and will be treated differently depending on what software is used.

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Luis Cazares (8/29/2014)


    Welsh Corgi (8/29/2014)


    I want to do the Equivalent of the following SQL Select and return the result listed below:

    SELECT CONVERT(DATE,GETDATE()) AS CurrentDate;

    CurrentDate

    2014-08-29

    Thank you.

    Equivalent in Oracle?

    SELECT SYSDATE AS Datetype,

    TRUNC(SYSDATE) AS NoTimeDate,

    TO_CHAR(SYSDATE, 'YYYY-MM-DD') AS StringDate

    FROM Dual;

    That is exactly what I was looking for. Thanks.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • David Burrows (8/29/2014)


    Your query is returning a date datatype, the gui interface (SSMS) is displaying as characters formatted as to the language of the login.

    So

    SELECT CONVERT(DATE,GETDATE()) AS CurrentDate;

    and

    SELECT CONVERT(varchar(10),GETDATE(),120) AS CurrentDate;

    Both show 2014-08-29 in SSMS but the first is a date datatype and the second is varchar (string) and will be treated differently depending on what software is used.

    Thanks for the tip.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • My requirements have changed.

    What I need to do is have a .NET Script execute a Stored Procedure against an Oracle Table.

    It looks for a record with the current Date that indicates an Oracle Job has completed.

    It executes every 15 minutes until it finds the record.

    I'm leaning in favor of an OpenQuery embedded in a Stored Procedure.

    In the stored Procedure I get the current Date and store it in a variable

    DECLARE @CurrentDate Date

    SET @CurrentDate = CONVERT(DATE, GETDATE());

    SELECT @CurrentDate AS CurrentDate

    Then I need to convert the date a string that will work with an Oracle Date format. I'm not sure what the syntax would be?

    The following works:

    SELECT *

    FROM OPENQUERY (LS_RDB_DWH, 'SELECT * FROM RDB_DWH.ENTITY_DIMENSION')

    My intent is to create dynamic SQL and pass the Oracle Date in the where clause.

    I tried querying the table based on the Entry Date using the following:

    SELECT *

    FROM OPENQUERY (LS_RDB_DWH, 'SELECT * FROM RDB_DWH.ENTITY_DIMENSION WHERE PD_RDB_ENTRY_DATE = '2012-10-31 03:10:37.0053840'')

    But I get the following error:

    Msg 102, Level 15, State 1, Line 2

    Incorrect syntax near '2012'.

    Any ideas would be greatly appreciated. 🙂

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • The syntax error is due to insufficient quotes. You have use two single quotes to get one when using them inside OPENQUERY

    ie

    SELECT *

    FROM OPENQUERY (LS_RDB_DWH, 'SELECT * FROM RDB_DWH.ENTITY_DIMENSION WHERE PD_RDB_ENTRY_DATE = ''2012-10-31 03:10:37.0053840''')

    Far away is close at hand in the images of elsewhere.
    Anon.

  • I got it to work.

    Thanks.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Format the date in SQL Server as a date and use that value in your Oracle query. I cannot remember if Oracle dates will query as SQL Server dates, as it's been way too long since I've worked with Oracle.

  • I do not oracle but after a quick google search, try this

    SELECT *

    FROM OPENQUERY (LS_RDB_DWH, 'SELECT * FROM RDB_DWH.ENTITY_DIMENSION WHERE PD_RDB_ENTRY_DATE >= TRUNC(SYSDATE)')

    Far away is close at hand in the images of elsewhere.
    Anon.

  • That should certainly work as long as the query pulls for everything today only. If the requirement is to go back in time, then date math in the Oracle query will suffice. If needed, perhaps the SQL Server procedure could accept a parameter to determine today (0) or days backward in time (integer > 0) and then pick an appropriate Oracle SQL statement to execute.

  • David Burrows (9/2/2014)


    I do not oracle

    You're lucky 😀

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • I got the following to work. I tried executing against the table in my original post but it took forever so I cancelled the query.

    SELECT CAST(CAE_RDB_ENTRY_DATE as Date), *

    FROM OPENQUERY(LS_RDB_DWH, 'SELECT * FROM RDB_DWHY.ASSOCIATE_ENTITY WHERE CAE_RDB_ENTRY_DATE > TO_DATE(''2014-08-08'',''YYYY.MM.DD'') ORDER BY 1')

    Thanks.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic. Login to reply