SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Function to Convert Current Date to SQL Sever Date format.


Function to Convert Current Date to SQL Sever Date format.

Author
Message
Welsh Corgi
Welsh Corgi
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25496 Visits: 4895
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/
David Burrows
David Burrows
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16159 Visits: 10103
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.


Welsh Corgi
Welsh Corgi
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25496 Visits: 4895
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/
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40135 Visits: 19802
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
David Burrows
David Burrows
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16159 Visits: 10103
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.


Welsh Corgi
Welsh Corgi
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25496 Visits: 4895
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/
Welsh Corgi
Welsh Corgi
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25496 Visits: 4895
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/
Welsh Corgi
Welsh Corgi
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25496 Visits: 4895
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/
David Burrows
David Burrows
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16159 Visits: 10103
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.


Welsh Corgi
Welsh Corgi
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25496 Visits: 4895
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/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search