Usuwanie wiodących zer z pola w instrukcji SQL
Pracuję nad zapytaniem SQL, które odczytuje z bazy danych SQLServer w celu wytworzenia pliku rozpakowującego. Jednym z wymogów usuwania zer wiodących z określonego pola, które jest prostym polem VARCHAR(10)
. Na przykład, jeśli pole zawiera '00001A', instrukcja SELECT musi zwrócić dane jako '1a'.
Czy istnieje sposób w SQL, aby łatwo usunąć wiodące zera w ten sposób? Wiem, że istnieje funkcja RTRIM
, ale wydaje się to usuwać tylko spacje.
13 answers
select substring(ColumnName, patindex('%[^0]%',ColumnName), 10)
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
2008-09-18 13:07:56
select replace(ltrim(replace(ColumnName,'0',' ')),' ','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
2013-12-31 18:14:18
select substring(substring('B10000N0Z', patindex('%[0]%','B10000N0Z'), 20),
patindex('%[^0]%',substring('B10000N0Z', patindex('%[0]%','B10000N0Z'),
20)), 20)
Zwraca N0Z
, czyli pozbędzie się zer wiodących i wszystkiego, co przed nimi stoi.
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
2012-10-02 11:54:04
Miałem taką samą potrzebę i użyłem tego:
select
case
when left(column,1) = '0'
then right(column, (len(column)-1))
else column
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
2015-04-14 19:24:51
Jeśli chcesz, aby zapytanie zwróciło 0 zamiast ciągu zer lub jakiejkolwiek innej wartości, możesz przekształcić to w instrukcję case w następujący sposób:
select CASE
WHEN ColumnName = substring(ColumnName, patindex('%[^0]%',ColumnName), 10)
THEN '0'
ELSE substring(ColumnName, patindex('%[^0]%',ColumnName), 10)
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-12-31 18:15:21
Możesz użyć tego:
SELECT REPLACE(LTRIM(REPLACE('000010A', '0', ' ')),' ', '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-04-18 09:10:56
Możesz tego spróbować - szczególną uwagę należy zwrócić na to, aby Tylko usunąć początkowe zera w razie potrzeby:
DECLARE @LeadingZeros VARCHAR(10) ='-000987000'
SET @LeadingZeros =
CASE WHEN PATINDEX('%-0', @LeadingZeros) = 1 THEN
@LeadingZeros
ELSE
CAST(CAST(@LeadingZeros AS INT) AS VARCHAR(10))
END
SELECT @LeadingZeros
Lub możesz po prostu zadzwonić
CAST(CAST(@LeadingZeros AS INT) AS VARCHAR(10))
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
2018-02-01 12:16:36
Aby usunąć początek 0, można pomnożyć kolumnę liczbową przez 1 Np: Select (ColumnName * 1)
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
2018-05-17 15:01:50
Usunięcie wiodącego 0 z miesiąca następującego po instrukcji na pewno zadziała.
SELECT replace(left(Convert(nvarchar,GETDATE(),101),2),'0','')+RIGHT(Convert(nvarchar,GETDATE(),101),8)
Wystarczy zastąpić GETDATE()
polem daty w tabeli.
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-09-02 17:04:31
Możesz spróbować tego
SELECT REPLACE(columnname,'0','') FROM table
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
2018-01-10 01:40:12
select ltrim('000045', '0') from dual;
LTRIM
-----
45
To powinno wystarczyć.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-20 08:14:49
Zapożyczyłem z powyższych pomysłów. To nie jest ani szybkie, ani eleganckie. ale to jest dokładne.
CASE
WHEN left(column, 3) = '000' THEN right(column, (len(column)-3))
WHEN left(column, 2) = '00' THEN right(a.column, (len(column)-2))
WHEN left(column, 1) = '0' THEN right(a.column, (len(column)-1))
ELSE
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
2016-02-12 22:17:36
select CASE
WHEN TRY_CONVERT(bigint,Mtrl_Nbr) = 0
THEN ''
ELSE substring(Mtrl_Nbr, patindex('%[^0]%',Mtrl_Nbr), 18)
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
2016-11-29 15:35:48