MYSTERY ZILLION တွင် English သို့မဟုတ် Unicode ဖြင့်သာ အသုံးပြုခွင့်ရှိသည်။ ဇော်ဂျီ ၊ ဧရာ စသည်တို့ကို အသုံးပြုခွင့် မရှိ။ Unicode fonts များမှာ Mon3,Yunghkio, Myanamr3 စသည်များ အသုံးပြုနိုင်သည်။ Unicode Guide ကို ဒီမှာ Download ချပါ။ Zawgyi to Unicode Converter
Don't share ebook or software if nobody request. You can find free book websites on here. We are welcome for discussion or asking question instead.

how to cast to get Max number in sql dataBase

in my table
fileds Name(PersionerNo (varchar(50),Name varchar( 50))

include data
PersionerNo Name

  P-1                   Aye
  P-2                   Mya
  P-3                   Aung
  P-4                   Naung
  P-5                  kyi

i need Maximum No from ,I solve these problem this way.....:D:D:D:D

select SUBSTRING(PensionerNo,3,10000) as PNo from tblPensioner Order By PNo Desc

but..when P-10001 ,I cannot get Max.How can i do.?:-/


  • Registered Users

    Have you tested? :D

    SUBSTRING(xxx,3,10000) means it will substract from index 3 to 10000.
    With your code, it will still work with "P-1000000000000000000000000000000...etc" :D

    Don't forget to type cast the result to get correct maximum number. Eg.
    SELECT TOP 1 * FROM dbo.Table_Name ORDER BY CAST(SUBSTRING(Column_Name,3,100) AS INT) DESC

    Alternatively, you could replace "P-" with empty and type cast the rest if the first portion have fixed length.

  • Registered Users

    Brother Means this.

    select top 1 SUBSTRING(PensionerNo,3,LEN(PensionerNo)-2) as PNo from tblPensioner Order By PNo Desc

  • Registered Users

    Nope. As long as you are reading max value by ordering string column, it might not be correct. Because as a string, '10' is less than '2'.

    To get maximum value, you can simply use MAX() function with the combination of type casting.


    If you still need to get max value by sorting the table, you need type casting.

  • Registered Users

    :)) ရတွာပြိ အစ်ကိုရေ ကျေးကျေး

  • edited June 2014 Registered Users

    select * from tblPensioner where PensionerNo = Max(PesionerNo)

    PesionerNo = eg.'p-5'
    so i think these way,

    select * From tblPensioner where PesionerNo = concat('P-', select MAX(CAST(SUBSTRING(COLUMN_NAME,3,10) AS INT)) FROM TABLE_NAME)
    .. :D but i cannot do it! please fill my code ^:)^

  • edited June 2014 Administrators
    SELECT * FROM tblPensioner WHERE PensionerNo = (SELECT MAX(CAST(SUBSTRING(PensionerNo, 3, 10) AS INT)) FROM tblPensioner);

    ပုံမှန်အားဖြင့် Please fix or write the code for me ဆိုတဲ့ မေးခွန်းမျိုးဆိုရင် ကျွန်တော်က မဖြေပဲနေပါတယ်။ မေးခွန်းတွေကြည့်ပြီး အကြံပေးချင်တာက စာဖတ်ပါလို့ပဲ အကြံပေးရလိမ့်မယ် DML Statement တွေကို ကောင်းကောင်း နားလည်ပုံမရသေးဘူး အလုပ်လုပ်နေတဲ့ လူတွေအတွက် အရေးကြီးပါတယ်။

    T-SQL အတွက် စာအုပ် တအုပ်ရှာထားပေးတယ် အောက်က Link မှာကြည့်ပါ အဲဒီ့စာအုပ်ကို ပြီးအောင်ဖတ်ရင် DML အတွက် အခြေခံကောင်းကောင်းတော့ ရနိုင်ပါလိမ့်မယ်။ SQL Server 2012 T-SQL - Itzik Ben-Gan.pdf

  • Registered Users

    thank brother, i get these ways... :| :| .Yes, I am weak in DML Statement .
    SELECT * FROM tblPensioner WHERE PensionerNo = 'P-'+cast((SELECT MAX(CAST(SUBSTRING(PensionerNo, 3, 10) AS INT)) FROM tblPensioner) as char(8));

  • edited June 2014 Administrators

    Yes, you may need to fix, my answer is just for concept. I have no SQL Server and the answer is just re-assembled from your question.

  • edited June 2014 Registered Users

    The Next Problem.

    No Date

    1 2014

    1 2013

    1 2012

    i want to get Group-concat in sql server!

    select *,Group-concat( Date Separator ',') as concatDate from tbl where No = 1;(in Mysql)

    but Sql,how can i ??????????????

    :D ;) :D ;;)

  • Administrators

    Not that simple function in MS SQL, you can try to understand the following statement. Or you may need to write a stored function.

    SELECT STUFF((SELECT ',' + FROM city FOR XML PATH('')), 1, 1, '') as cities;
Sign In or Register to comment.