Frage

This is not a question although, just a query.

While I was playing with SQL queries, I found something I was unable to know how it was happening.

I used the following query:

select Replace(getdate(),'-','')

And I got a result like

May  9 2014  4:51PM

Now, I thought to change the query to:

Select REPLACE(Getdate(),'-','/') 

or used any other character in place of '/', I got the same output.

Would appreciate if someone could explain how this was happening. And yes, one more thing, could you please guide me how to format the question too, since I know this question's gonna need formatting after I have posted it...:P

War es hilfreich?

Lösung

Replace function has a definition as:

REPLACE ( string_expression , string_pattern , string_replacement )

where

  1. string_expression: Is the string expression to be searched.
  2. string_pattern: Is the substring to be found.
  3. string_replacement: Is the replacement string.

Now what's happening here is Getdate() returns current date as a datetime datatype which is implicitly converted to varchar or to say a string expression to evaluate the function. Try casting getdate() to varchar as below and you should get your confusion resolved:

select CAST( getdate() AS nvarchar(25))

You can read more about Implicit conversions here: http://msdn.microsoft.com/en-us/library/ms187928.aspx

Andere Tipps

REPLACE() function is converting datetime value to string representation of date

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top