{"id":86,"date":"2011-12-21T12:57:30","date_gmt":"2011-12-21T12:57:30","guid":{"rendered":"https:\/\/sqlserverlearner.azurewebsites.net\/2011\/12\/21\/using-max-aggregate-function-on-varchar-column-vs-int-column-sql-server\/"},"modified":"2011-12-21T12:57:30","modified_gmt":"2011-12-21T12:57:30","slug":"using-max-aggregate-function-on-varchar-column-vs-int-column-sql-server","status":"publish","type":"post","link":"https:\/\/sqlserverlearner.com\/2011\/12\/21\/using-max-aggregate-function-on-varchar-column-vs-int-column-sql-server\/","title":{"rendered":"using max aggregate function on varchar column vs int column sql server"},"content":{"rendered":"

MAX aggregate Function behaves in a different way on numeric columns when compared to alpha-numeric columns.<\/strong><\/p>\n

consider the below TSQL script:
\n[sql]
\nDECLARE @vTable TABLE (ID INT)<\/p>\n

INSERT INTO @vTable(ID)
\nSELECT 1
\nUNION
\nSELECT 199
\nUNION
\nSELECT 99<\/p>\n

SELECT MAX(ID) FROM @vTable
\n[\/sql]<\/p>\n

We can expect the result of the MAX(ID) to be 199.<\/em><\/p>\n

Now consider this TSQL Script:
\n[sql]
\nDECLARE @vTable1 TABLE (ID varchar(10))<\/p>\n

INSERT INTO @vTable1(ID)
\nSELECT 1
\nUNION
\nSELECT 199
\nUNION
\nSELECT 99<\/p>\n

SELECT MAX(ID) FROM @vTable1
\n[\/sql]<\/p>\n

Now if you expect the result to be 199 again then you are wrong.
\nThe result is 99.<\/em><\/p>\n

Why this difference:<\/strong>
\nID in the second script is a varchar column.
\nso for varchar being alpha numeric the ascii codes start with NULL for dec(0) till dec(255).
\n1 comes at around dec(49), 9 comes at dec(57).
\nSo it compares 1 in 199 with 9 in 99 and sorts out that 99 is greater.
\nHence the result 99.<\/p>\n

Below query you can see the data in sorted format on the ID (varchar) column.
\n[sql]
\nDECLARE @vTable1 TABLE (ID varchar(10))<\/p>\n

INSERT INTO @vTable1(ID)
\nSELECT 1
\nUNION
\nSELECT 199
\nUNION
\nSELECT 99<\/p>\n

SELECT ID FROM @vTable1 order by ID asc
\n[\/sql]<\/p>\n

Results in sql server management studio:
\n1
\n199
\n99<\/p>\n","protected":false},"excerpt":{"rendered":"

MAX aggregate Function behaves in a different way on numeric columns when compared to alpha-numeric columns. consider the below TSQL script: [sql] DECLARE @vTable TABLE (ID INT) INSERT INTO @vTable(ID) SELECT 1 UNION SELECT 199 UNION SELECT 99 SELECT MAX(ID) FROM @vTable [\/sql] We can expect the result of the MAX(ID) to be 199. Now…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41,3],"tags":[450,451,452,453,454],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-sql-server-basics","category-sqlserverpedia-syndication","tag-max-aggregate-function-varchar-vs-int","tag-max-function-on-varchar","tag-using-max-aggregate-function-on-varchar-sql-server-2005","tag-using-max-aggregate-function-on-varchar-sql-server-2008","tag-why-max-aggregate-function-displays-wrong-data-when-it-contains-numbers"],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":0,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}