﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » StrataFrame Application Framework - V1 » WinForms (How do I?)  » Query get the largest ID number of the string ?</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Sun, 10 May 2026 17:21:47 GMT</lastBuildDate><ttl>20</ttl><item><title>Query get the largest ID number of the string ?</title><link>http://forum.strataframe.net/FindPost33494.aspx</link><description>&lt;div&gt;Suppose in SQL Server 2005 I have the TABTHUCHI data table has the following fields:&lt;div&gt;IDSOPHIEUTHUCHI // Primary Key&lt;div&gt;RIGHT&lt;div&gt;...&lt;br/&gt;&lt;br/&gt;&lt;div&gt;&lt;table border="0" cellpadding="0" cellspacing="0" width="64" style="border-collapse: collapse;width:48pt"&gt;&lt;tbody&gt;&lt;tr height="17" style="height:12.75pt"&gt;  &lt;td height="17" width="64" style="height:12.75pt;width:48pt"&gt;&lt;table border="0" cellpadding="0" cellspacing="0" width="64" style="border-collapse: collapse;width:48pt"&gt;&lt;tbody&gt;&lt;tr height="17" style="height:12.75pt"&gt;  &lt;td height="17" width="64" style="height:12.75pt;width:48pt"&gt;&lt;table border="0" cellpadding="0" cellspacing="0" width="264" style="border-collapse: collapse;width:199pt"&gt; &lt;colgroup&gt;&lt;col width="182" style="mso-width-source:userset;mso-width-alt:6656;width:137pt"&gt; &lt;col width="82" style="mso-width-source:userset;mso-width-alt:2998;width:62pt"&gt; &lt;/colgroup&gt;&lt;tbody&gt;&lt;tr height="17" style="height:12.75pt"&gt;  &lt;td height="17" class="xl65" align="left" width="182" style="height:12.75pt;  width:137pt"&gt;IDSOPHIEUTHUCHI&lt;/td&gt;  &lt;td class="xl66" align="left" width="82" style="width:62pt"&gt;NGAY&lt;/td&gt; &lt;/tr&gt; &lt;tr height="17" style="height:12.75pt"&gt;  &lt;td height="17" class="xl69" align="left" style="height:12.75pt"&gt;PC-04/08/16-008&lt;/td&gt;  &lt;td class="xl67" align="right"&gt;04/08/2016&lt;/td&gt; &lt;/tr&gt; &lt;tr height="17" style="height:12.75pt"&gt;  &lt;td height="17" class="xl66" align="left" style="height:12.75pt"&gt;PC-07/06/2016-006&lt;/td&gt;  &lt;td class="xl67" align="right"&gt;07/06/2016&lt;/td&gt; &lt;/tr&gt; &lt;tr height="17" style="height:12.75pt"&gt;  &lt;td height="17" class="xl66" align="left" style="height:12.75pt"&gt;PC-08/10/2017-010&lt;/td&gt;  &lt;td class="xl67" align="right"&gt;08/10/2017&lt;/td&gt; &lt;/tr&gt; &lt;tr height="17" style="height:12.75pt"&gt;  &lt;td height="17" class="xl69" align="left" style="height:12.75pt"&gt;PC-08/12/2017-012&lt;/td&gt;  &lt;td class="xl67" align="right"&gt;08/12/2017&lt;/td&gt; &lt;/tr&gt; &lt;tr height="17" style="height:12.75pt"&gt;  &lt;td height="17" class="xl69" align="left" style="height:12.75pt"&gt;PC-10/01/2015-2&lt;/td&gt;  &lt;td class="xl67" align="right"&gt;10/01/2015&lt;/td&gt; &lt;/tr&gt; &lt;tr height="17" style="height:12.75pt"&gt;  &lt;td height="17" class="xl66" align="left" style="height:12.75pt"&gt;PC-25/09/2017-009&lt;/td&gt;  &lt;td class="xl67" align="right"&gt;25/09/2017&lt;/td&gt; &lt;/tr&gt; &lt;tr height="17" style="height:12.75pt"&gt;  &lt;td height="17" class="xl66" align="left" style="height:12.75pt"&gt;PT-01/02/2015-001&lt;/td&gt;  &lt;td class="xl67" align="right"&gt;01/02/2015&lt;/td&gt; &lt;/tr&gt; &lt;tr height="17" style="height:12.75pt"&gt;  &lt;td height="17" class="xl68" align="left" style="height:12.75pt"&gt;PT-03/11/17-011&lt;/td&gt;  &lt;td class="xl67" align="right"&gt;03/11/2017&lt;/td&gt; &lt;/tr&gt; &lt;tr height="17" style="height:12.75pt"&gt;  &lt;td height="17" class="xl68" align="left" style="height:12.75pt"&gt;PT-05/04/2015-004&lt;/td&gt;  &lt;td class="xl67" align="right"&gt;05/04/2015&lt;/td&gt; &lt;/tr&gt; &lt;tr height="17" style="height:12.75pt"&gt;  &lt;td height="17" class="xl68" align="left" style="height:12.75pt"&gt;PT-15/07/2016-07&lt;/td&gt;  &lt;td class="xl67" align="right"&gt;15/07/2016&lt;/td&gt; &lt;/tr&gt; &lt;tr height="17" style="height:12.75pt"&gt;  &lt;td height="17" class="xl66" align="left" style="height:12.75pt"&gt;PT-20/05/2016-005&lt;/td&gt;  &lt;td class="xl67" align="right"&gt;20/05/2016&lt;/td&gt; &lt;/tr&gt; &lt;tr height="17" style="height:12.75pt"&gt;  &lt;td height="17" class="xl66" align="left" style="height:12.75pt"&gt;PT-31/03/2015-003&lt;/td&gt;  &lt;td class="xl67" align="right"&gt;31/03/2015&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br/&gt;&lt;div&gt;&lt;div&gt;At the IDSOPHIEUTHUCHI field, you will see the letter "PT" or "PC", the year will be 2 characters or 4 characters and the last digit must be PT-(dd)/(MM)/(yyyy) - (***)&lt;div&gt;Use (*) is the incremental number generated by the system, the length is determined by the number of asterisks, a maximum of 9.&lt;div&gt;Example:&lt;div&gt;HD (**) includes 99 numbers running from HD01, HD02 to HD99.&lt;div&gt;HD (**) includes 999 numbers running from HD001, HD002 to HD999.&lt;div&gt;Use (MM) to get the current month.&lt;div&gt;Use (years) to retrieve the current year (last two digits of the year).&lt;div&gt;Use (yyyy) to get the current year (all 4 numbers).&lt;div&gt;Use (dd) to get the current date.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;According to the above data, you want to get the largest ID of 2015 "PT". Find the last digit of 2015: PT-05/04/2015-004, the largest ID 2015 will be 005, and assume The largest ID number of this date is 14/04/2015 will be: PT-14/04/2015-005, when taking the largest number you must also note Year can be 2 digits or 4 digits and format input.&lt;div&gt;If the input format: PT-(dd)/(MM)/(yy)-(***) output: PT-14/04/15-005&lt;div&gt;If the input format: PT-(dd)/(MM)/(yyyy)-(*****) output: PT-14/04/2015-00005&lt;div&gt;If the input format: PT-(***) output: PT-005&lt;div&gt;Similar to other years for "PT" or "PC" and the largest ID date.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;If the "PT" format of the above data:&lt;div&gt;If the input format: PT-(dd)/(MM)/(yyyy)-(***) and "14/04/2016", the output: PT-14/04/2016-008&lt;div&gt;If the input format: PT-(dd)/(MM)/(yyyy)-(***) and "14/04/2017", the output: PT-14/04/2017-012&lt;div&gt;If the input format: PT-(***) and "14/04/2017" is output PT-012&lt;br/&gt;&lt;br/&gt;&lt;div&gt;If the "PC" format of the above data:&lt;div&gt;If the input format: PC-(dd)/(MM)/(yyyy)-(***) and "14/04/2015" is output: PC-14/04/2015-003&lt;div&gt;If the input format: PC-(dd)/(MM)/(yyyy)-(**) and "14/04/2016" then output: PC-14/04/2016-09&lt;div&gt;If the input format: PC-(dd)/(MM)/(yy)-(***) and "14/04/2017" then output: PC-14/04/17-013&lt;br/&gt;&lt;br/&gt;&lt;div&gt;How can SQL Server retrieve the largest ID in string form as shown above ?&lt;br/&gt;</description><pubDate>Mon, 17 Apr 2017 00:01:21 GMT</pubDate><dc:creator>Dong Trien Lam</dc:creator></item><item><title>RE: Query get the largest ID number of the string ?</title><link>http://forum.strataframe.net/FindPost33496.aspx</link><description>I have looked over your posts but when run erroneously, I sent my example to you for a failure</description><pubDate>Mon, 17 Apr 2017 00:01:21 GMT</pubDate><dc:creator>Dong Trien Lam</dc:creator></item><item><title>RE: Query get the largest ID number of the string ?</title><link>http://forum.strataframe.net/FindPost33495.aspx</link><description>Hopefully this makes sense. &amp;nbsp;You'll need to substring after either the first '-' or the second '-' depending on whether there are 2 '-' characters.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;&lt;br/&gt;&lt;div&gt;[code]&lt;div&gt;&lt;div&gt;DECLARE @temp TABLE&lt;div&gt;(&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;Field1 VARCHAR(30),&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;Field2 VARCHAR(20)&lt;div&gt;);&lt;br/&gt;&lt;br/&gt;&lt;div&gt;INSERT INTO @temp VALUES&lt;div&gt;('PC-04/08/16-008','04/08/2016'),&lt;div&gt;('PC-07/06/2016-006','07/06/2016'),&lt;div&gt;('PC-08/10/2017-010','08/10/2017'),&lt;div&gt;('PC-08/12/2017-012','08/12/2017'),&lt;div&gt;('PC-10/01/2015-2','10/01/2015'),&lt;div&gt;('PC-25/09/2017-009','25/09/2017'),&lt;div&gt;('PT-01/02/2015-001','01/02/2015'),&lt;div&gt;('PT-03/11/17-011','03/11/2017'),&lt;div&gt;('PT-05/04/2015-004','05/04/2015'),&lt;div&gt;('PT-15/07/2016-07','15/07/2016'),&lt;div&gt;('PT-20/05/2016-005','20/05/2016'),&lt;div&gt;('PT-31/03/2015-003','31/03/2015'),&lt;div&gt;('PT-003', '01/01/2015')&lt;br/&gt;&lt;br/&gt;&lt;div&gt;SELECT&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;MAX(T.StarNumber) Max_StringComparison,&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;MAX(CONVERT(INT, T.StarNumber)) Max_NumberComparison&lt;div&gt;FROM&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;(&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;SELECT&amp;nbsp;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;CASE&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;				&lt;/span&gt;WHEN CHARINDEX('-', T.Field1, 4) = 0 /* Only has 1 '-' */ THEN SUBSTRING(T.Field1, CHARINDEX('-', T.Field1, 1) + 1, 1000) /* Substring after the first '-' */&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;				&lt;/span&gt;ELSE SUBSTRING(T.Field1, CHARINDEX('-', T.Field1, 4) + 1, 1000) /* Substring after the second '-' */ END StarNumber&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;FROM&amp;nbsp;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;@temp T&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;) T&lt;div&gt;[/code]</description><pubDate>Fri, 14 Apr 2017 07:26:20 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item></channel></rss>