Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 258 Vote(s) - 3.38 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Better techniques for trimming leading zeros in SQL Server?

#11
replace(ltrim(replace(Fieldname.TableName, '0', '')), '', '0')

The suggestion from Thomas G worked for our needs.

The field in our case was already string and only the leading zeros needed to be trimmed. Mostly it's all numeric but sometimes there are letters so the previous INT conversion would crash.
Reply

#12
SELECT CAST(CAST('000000000' AS INTEGER) AS VARCHAR)

This has a limit on the length of the string that can be converted to an INT
Reply

#13
If you do not want to convert into int, I prefer this below logic because it can handle nulls
IFNULL(field,LTRIM(field,'0'))
Reply

#14
If you are using Snowflake SQL, might use this:

ltrim(str_col,'0')

The ltrim function removes all instances of the designated set of characters from the left side.

So ltrim(str_col,'0') on '00000008A' would return '8A'

And rtrim(str_col,'0.') on '$125.00' would return '$125'
Reply

#15
``` lang-sql
SUBSTRING(str_col, IIF(LEN(str_col) > 0, PATINDEX('%[^0]%', LEFT(str_col, LEN(str_col) - 1) + '.'), 0), LEN(str_col))
```

Works fine even with '0', '00' and so on.
Reply

#16
In MySQL you can do this...

Trim(Leading '0' from your_column)
Reply

#17
For converting number as **varchar** to **int**, you could also use simple

```
(column + 0)
```
Reply

#18
This might help

SELECT ABS(column_name) FROM [db].[schema].[table]
Reply

#19
Very easy way, when you just work with numeric values:

SELECT
TRY_CONVERT(INT, '000053830')
Reply

#20
[Starting with SQL Server 2022 (16.x) you can do this][1]

```TRIM ( [ LEADING | TRAILING | BOTH ] [characters FROM ] string )```


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through