Archive for March, 2013

Format SQL Date hh:mm AM/PM

March 11, 2013 1 comment

SELECT RIGHT( convert(varchar(25),GETDATE(),101)  +’ ‘+ CASE WHEN DATEPART(HH,GETDATE()) < 13 THEN RIGHT(REPLICATE(‘0’,2) + CAST(datepart( HH,GETDATE()) AS VARCHAR(2)),2) ELSE RIGHT(REPLICATE(‘0′,2) + CAST(datepart( HH, dateadd(HH, -12 ,GETDATE())) AS VARCHAR(2)),2) END +’:’+ RIGHT(REPLICATE(‘0′,2) + CAST(datepart( mi,GETDATE()) AS VARCHAR(2)),2) +’ ‘+ CASE WHEN DATEPART(HH,GETDATE()) < 13 THEN ‘AM’ ELSE ‘PM’ END,8)

Got a big sql statement to show the time format (hh:mm AM/PM) from date.

If any simple query to do it, please let me know.


Categories: MS SQL

Sort json Array with LINQ and JSON.NET

March 2, 2013 Leave a comment

string jsonStr = dal.GetUsersByType(userId, userType);
JObject json = JObject.Parse(jsonStr);

IEnumerable<JToken> users = from p in json[“Users”]
orderby p[“Firstname”]
select p;

return JsonConvert.SerializeObject(users);

I got it. Some complex join queries may not allow you to sort (order by) a particular column.

So my way is , first get the json array, convert to Json object using JSON.NET.

Then sort it using LINQ and serialize back to json string.

Sample code is shown above.

Happy Coding 🙂

Categories: ASP.NET Tags: