Thursday, October 27, 2011

STUFF is like SUBSTRING with a replace capability

-- delete b_c_d_ and replace it with i-j-k-l-m-n

SELECT STUFF('a_b_c_d_e_f', 3, 5, 'i-j-k-l-m-n') AS [STUFF(''a_b_c_d_e_f'', 2, 3, ''i-j-k-l-m-n'')]

-- result:   a_i-j-k-l-m-n_e_f

DECLARE @value nvarchar(max)

SELECT @value = SUBSTRING(
(SELECT ',' + l.name
FROM sys.syslanguages l
FOR XML PATH('')), 2, 200000)
SELECT @value AS [CSV from Rows]

SELECT @value = STUFF(
(SELECT ', ' + cast(l.lcid as nvarchar(10))
FROM sys.syslanguages l
FOR XML PATH('')),1, 3, '')  --Replace the 1st two characters with nothing. 
--You don't have to worry about the length of the string like you do with substring's end parameter (200000) as demo'd above

SELECT @value AS [LCIDs in sys.syslanguages]
-- shows the comma separated values in a single row and single column for the Locale IDs in SQL Server.

033, 1031, 1036, 1041, 1030, 3082, 1040, 1043, 2068, 2070, 1035, 1053, 1029, 1038, 1045, 1048, 1050, 1051, 1060, 1032, 1026, 1049, 1055, 2057, 1061, 1062, 1063, 1046, 1028, 1042, 2052, 1025, 1054

SQL Server's sys.syslanguages is only a small subset of the Windows culture info:

Now get sys.syslanguages months and shortmonths.  Don't use #T temporary tables because Entity Framework will barf, saying there are no results from the stored procedure --use variable tables @t and @t2 then alias them in the inner join update.


SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ForeignMonths]
 @LCID int = 0 -- English by default, 6 = Italian

AS
BEGIN

DECLARE @t table ([ID] int, ShortMonth nvarchar(6), Month nvarchar(20))
DECLARE @t2 table ([ID] int, [Month] nvarchar(20))
-- use row_number without an order by using (select 0) ...
-- needed for dropdownlist select option where value = rownum

INSERT INTO @t
SELECT row_number() over(order by (select 0)) AS [ID]
, item AS [ShortMonth]
, CAST(N'' AS nvarchar(20)) AS [Month]
FROM [dbo].[CSV2Table]( (SELECT shortmonths
FROM sys.syslanguages
WHERE lcid= @LCID)
, ',')
--SELECT * FROM @t

INSERT INTO @t2
SELECT row_number() over(order by (select 0)) AS [ID]
, item AS [Month]
FROM dbo.[CSV2Table]( (SELECT months
FROM sys.syslanguages
WHERE lcid= @LCID)
, ',')
--SELECT * FROM @t2

UPDATE @t SET [Month] = T2.[Month]
FROM @t2 T2 INNER JOIN @t T1 ON T2.ID = T1.ID

SELECT * FROM @t
RETURN 0

END

GO

Young Roofers

Young Roofers
Men Making

Fly. Be Free.

Fly. Be Free.
Man Ready to Hang ... Glide

Burke

Burke
A Man in the Making - 12 years old

Blackwater

Blackwater
A Man in the Mud