1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | Alter Function [dbo].[fn_format_date] (@Datetime DateTime, @FormatMask NVarChar(32)) Returns NvarChar(32) As Begin Declare @StringDate NVarChar(32) Set @StringDate = @FormatMask If (CharIndex (N'YYYY',@StringDate) > 0) Begin Set @StringDate = Replace(@StringDate, N'YYYY', DateName(YY, @Datetime)) End If (CharIndex (N'YY',@StringDate) > 0) Begin Set @StringDate = Replace(@StringDate, N'YY', Right(DateName(YY, @Datetime),2)) End If (CharIndex (N'MM',@StringDate) > 0) Begin Set @StringDate = Replace(@StringDate, N'MM', Right(N'0'+Convert(VARCHAR,DatePart(MM, @Datetime)),2)) End If (CharIndex (N'M',@StringDate) > 0) Begin Set @StringDate = Replace(@StringDate, N'M', Convert(VARCHAR,DatePart(MM, @Datetime))) End If (CharIndex (N'DD',@StringDate) > 0) Begin Set @StringDate = Replace(@StringDate, N'DD', Right(N'0'+DateName(DD, @Datetime),2)) End If (CharIndex (N'D',@StringDate) > 0) Begin Set @StringDate = Replace(@StringDate, N'D', DateName(DD, @Datetime)) End Return @StringDate End Go Set Nocount On Declare @lDateTime Datetime = '20110506' Print [dbo].[fn_format_date] (@lDateTime, 'MM/DD/YYYY') Print [dbo].[fn_format_date] (@lDateTime, 'DD/MM/YYYY') Print [dbo].[fn_format_date] (@lDateTime, 'M/DD/YYYY') Print [dbo].[fn_format_date] (@lDateTime, 'M/D/YYYY') Print [dbo].[fn_format_date] (@lDateTime, 'M/D/YY') Print [dbo].[fn_format_date] (@lDateTime, 'MM/DD/YY') Print [dbo].[fn_format_date] (@lDateTime, 'YYYY/MM/DD') Print [dbo].[fn_format_date] (@lDateTime, 'YYYYMMDD') Print [dbo].[fn_format_date] (@lDateTime, 'YYYY-MM-DD') /* 05/06/2011 06/05/2011 5/06/2011 5/6/2011 5/6/11 05/06/11 2011/05/06 20110506 2011-05-06 */ |
Archive for the ‘MS SQL Server’ Category
@@RowCount returns the number of rows affected by the last statement (BOL).
The documentation does not specify behaviour for FOR XML clause in the SELECT statement. When FOR XML clause is used, the behaviour is the same. The FOR XML clause converts the result into one row, but @@RowCount contains number of all affected rows in the SELECT statement.
1 2 3 4 5 6 7 8 9 10 | SELECT Top 100 [number] FROM [master].[dbo].[spt_values] ORDER BY [number] Asc FOR XML PATH('') SELECT @@ROWCOUNT --100 |
One of the possible ways to paste UniqueIdentifier into text string is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Declare @lId UniqueIdentifier = NewId() Declare @x NVarChar(Max) = N' Select [id] From [table] Where [column] = '+IsNull(N'N'''+Cast(@lId As NVarChar(64))+'''', 'Null') Print @x /* Select [id] From [table] Where [column] = N'094FDFB4-7665-4E6A-89E0-B6A6C231D617' */ |
If Null is in the variable, the output will be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Declare @lId UniqueIdentifier Declare @x NVarChar(Max) = N' Select [id] From [table] Where [column] = '+IsNull(N'N'''+Cast(@lId As NVarChar(64))+'''', 'Null') Print @x /* Select [id] From [table] Where [column] = Null */ |
Today I have read an interesting article from Itzik Ben Gan (MVP) who describes REPLACE() puzzle.
It is all about these two lines:
1 2 3 | DECLARE @s AS VARCHAR(1000) = '.x.x.x.'; SELECT REPLACE(@s, '.x.', '.y.'); |
The result of these commands is .y.x.y. instead of expected .y.y.y.
The reason is:
“Given the expression REPLACE(@s, @find, @replacement), the REPLACE function was designed to replace in @s nonoverlapping occurrences of @find with @replacement, scanning the string from left to right.”
I have tested this behaviour at http://codepad.org in another languages and the results were same.
http://codepad.org/wMbZOHno – PHP version
http://codepad.org/bO488THh – Python version
If you use namespaces in XML, you should specify schemaLocation like in this example:
1 2 3 4 5 | <com:car xsi:schemaLocation="http://yourserver.com/example http://yourserver.com/example.xsd" xmlns:com="http://yourserver.com/example"> <com:name>Text</com:name> </com:car> |
SQL Server Management Studio load this XSD schema and validates XML which should correspond with this schema. So in this case schema should be accessible at http://yourserver.com/example.xsd.
There is a probability that you change this schema and reupload it at http://yourserver.com/example.xsd. After this SQL Server Management Studio can remember old XSD schema and can underline XML items which do not correspond with old XSD schema.
You can correct it when you go on the Internet Explorer to http://yourserver.com/example.xsd and hit Control + F5. This will load fresh XSD schema from server and SQL Server Management Studio will use it for the next execution of the query.