How to get only the last 10 characters in a column

mo boy 396 Reputation points
2022-10-06T04:02:09.57+00:00

Dear Experts,

I have a table and the column is called Description. This column has entries with snapshot of a particular sale item and the date is included as the last 10 characters. My requirement is I need to fetch this date value (last 10 characters).

But the issue is this description column contents is not uniform. I see some values that are quite long and some that are not so long and some that are short (like less than 20 characters).

How can I retrieve only the last 10 characters and take out the date value?

Please provide suggestions.

Thanks

Developer technologies | Transact-SQL
SQL Server | Other
0 comments No comments
{count} vote

Accepted answer
  1. Raphael Liam Cal 76 Reputation points
    2022-10-06T04:34:02.367+00:00

    I assume the column data type would be a character string type like a varchar or text.
    You should be able to use the RIGHT function in your select query as explained in this link https://www.w3schools.com/sql/func_sqlserver_right.asp

    SELECT RIGHT ([Description], 10) AS [Description]
    FROM tablename

    Notes:
    Square brackets [] added because Description would be a keyword in SQL Server.
    The "As columnanme" would be needed or else the the column would not be assigned a name (when I tried it showed "(No column name)"
    If the column data is less than X (number specified), it will return the data. Ex column has String123, String123 will be returned.
    If the column data is empty '', it will return empty.
    If the column data is NULL, it will return NULL.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.