Konwertuj varchar na uniqueidentifier w SQL Server
Tabela, dla której nie kontroluję schematu, zawiera kolumnę zdefiniowaną jako varchar (50), która przechowuje uniqueidentifiers w formacie 'a89b1acd95016ae6b9c8aabb07da2010' (bez myślników)
Chcę przekonwertować je na uniqueidentifiers w SQL do przekazywania do. Net Guid. Jednak poniższe linie zapytań nie działają dla mnie:
select cast('a89b1acd95016ae6b9c8aabb07da2010' as uniqueidentifier)
select convert(uniqueidentifier, 'a89b1acd95016ae6b9c8aabb07da2010')
I wynik:
Msg 8169, Level 16, State 2, Line 1 Conversion failed when converting from a character string to uniqueidentifier.
Te same zapytania za pomocą podzielonego uniqueidentifier działają dobrze, ale dane nie są przechowywane w tym format.
Czy istnieje inny (efektywny) sposób konwersji tych łańcuchów na uniqueidentifiers w SQL. -- Nie chcę tego robić w kodzie. Net.
6 answers
DECLARE @uuid VARCHAR(50)
SET @uuid = 'a89b1acd95016ae6b9c8aabb07da2010'
SELECT CAST(
SUBSTRING(@uuid, 1, 8) + '-' + SUBSTRING(@uuid, 9, 4) + '-' + SUBSTRING(@uuid, 13, 4) + '-' +
SUBSTRING(@uuid, 17, 4) + '-' + SUBSTRING(@uuid, 21, 12)
AS UNIQUEIDENTIFIER)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-09-07 16:36:11
To byłaby przydatna funkcja. Zauważ również, że używam rzeczy zamiast podłańcucha.
create function str2uniq(@s varchar(50)) returns uniqueidentifier as begin
-- just in case it came in with 0x prefix or dashes...
set @s = replace(replace(@s,'0x',''),'-','')
-- inject dashes in the right places
set @s = stuff(stuff(stuff(stuff(@s,21,0,'-'),17,0,'-'),13,0,'-'),9,0,'-')
return cast(@s as uniqueidentifier)
end
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-25 23:52:15
Twój varchar C:
SELECT CONVERT(uniqueidentifier,LEFT(C, 8)
+ '-' +RIGHT(LEFT(C, 12), 4)
+ '-' +RIGHT(LEFT(C, 16), 4)
+ '-' +RIGHT(LEFT(C, 20), 4)
+ '-' +RIGHT(C, 12))
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-09-07 16:43:39
SELECT CONVERT(uniqueidentifier,STUFF(STUFF(STUFF(STUFF('B33D42A3AC5A4D4C81DD72F3D5C49025',9,0,'-'),14,0,'-'),19,0,'-'),24,0,'-'))
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-19 10:15:22
Jeśli twój łańcuch znaków zawiera znaki specjalne, możesz go zahaszować do md5, a następnie przekonwertować do guid / uniqueidentifier.
SELECT CONVERT(UNIQUEIDENTIFIER, HASHBYTES('MD5','~öü߀a89b1acd95016ae6b9c8aabb07da2010'))
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-26 12:39:42
Podane guid nie jest poprawnym formatem (. NET pod warunkiem guid).
Begin try select convert (uniqueidentifier,'a89b1acd95016ae6b9c8aabb07da2010') end try begin catch print "1" end catch
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-13 09:52:44