Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Function to Convert Current Date to SQL Sever Date format. Expand / Collapse
Author
Message
Posted Friday, August 29, 2014 6:32 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 7:44 PM
Points: 4,240, Visits: 4,284
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/

Post #1608571
Posted Friday, August 29, 2014 6:50 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 12, 2014 8:53 AM
Points: 7,120, Visits: 6,978
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.

Post #1608580
Posted Friday, August 29, 2014 8:10 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 7:44 PM
Points: 4,240, Visits: 4,284
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/

Post #1608618
Posted Friday, August 29, 2014 8:17 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:18 PM
Points: 3,667, Visits: 8,002
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1608625
Posted Friday, August 29, 2014 8:17 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 12, 2014 8:53 AM
Points: 7,120, Visits: 6,978
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.

Post #1608626
Posted Friday, August 29, 2014 8:26 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 7:44 PM
Points: 4,240, Visits: 4,284
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/

Post #1608630
Posted Friday, August 29, 2014 8:27 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 7:44 PM
Points: 4,240, Visits: 4,284
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/

Post #1608631
Posted Tuesday, September 2, 2014 7:02 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 7:44 PM
Points: 4,240, Visits: 4,284
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/

Post #1609608
Posted Tuesday, September 2, 2014 7:18 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 12, 2014 8:53 AM
Points: 7,120, Visits: 6,978
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.

Post #1609616
Posted Tuesday, September 2, 2014 7:56 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 7:44 PM
Points: 4,240, Visits: 4,284
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/

Post #1609628
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse