Question: I have a list of ID’s Shown below.

```
AT0920130004
AT0920130005
AT0920130006
AT0920130007
AT0920130008
AT0920130009
AT0920130010
AT1020130001
AT1020130002
AT1020130003
AT1020130004
AT1020130005
AT1120130003
AT1120130004
AT1120130005
AT1120130006

```

Here an example record has the format `ATmmyyyyxxxx`.

where 

`AT` represents `location`,

`mm` represents `month` eg 10 would be `October`

`yyyy` represents `year` eg. `2013`

`xxxx` represents the `increasing seed of numbers`.

Now I need to select an ID which is generated in the `end of the month`.
For ex: last id of September i.e. AT0920130010.
Any suggestions are appreciated.

Answer: 
try this :

```
select MAX(t.ID) from TableName t
group by SUBSTRING(t.ID, 3, 2)

```
You basically want to get the max id that matches a location, a month and a year. Something like this should do it:

```
SELECT max(id)
FROM myTable
WHERE substring(id, 0, 2) = [location]
  AND susbstring(id, 2, 2) = [month]
  AND substring(id, 4, 4) = [year]

```