Thursday, March 6, 2014

SQL Cast Date


SQL Cast Date

Using SQL Server 2008
This first option of removing the date uses the SQL Server 2008 method. This will return only the date or only the time:
-- remove the time
SELECT CAST(GETDATE() AS DATE) -- 2009-07-12
-- remove the date
SELECT CAST(GETDATE() AS TIME) -- 08:46:25.8130000
If you’re not working with SQL Server 2008, you have to improvise.  
Cast Date With No Time Using Floor
This example removes the time from the date time by setting it to the beginning of the day.
-- Get the current day with no time
-- 2009-06-29 00:00:00.000
SELECT CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS datetime)

-- Get the next day
-- 2009-06-30 00:00:00.000
SELECT CAST(CEILING (CAST(GETDATE() AS FLOAT)) AS datetime)
Cast Date with No Time Using Convert
Using convert you can remove the time for display purposes, usually for reporting or the front end.
-- Month first
SELECT CONVERT(VARCHAR(12),GETDATE(), 101)  -- 06/29/2009
SELECT CONVERT(VARCHAR(12),GETDATE(), 110)  -- 06-29-2009
SELECT CONVERT(VARCHAR(12),GETDATE(), 100)  -- Jun 29 2009
SELECT CONVERT(VARCHAR(12),GETDATE(), 107)  -- Jun 29, 2009

-- Year first
SELECT CONVERT(VARCHAR(12),GETDATE(), 102)  -- 2009.06.29
SELECT CONVERT(VARCHAR(12),GETDATE(), 111)  -- 2009/06/29
SELECT CONVERT(VARCHAR(12),GETDATE(), 112)  -- 20090629

-- Day first
SELECT CONVERT(VARCHAR(12),GETDATE(), 103)  -- 29/06/2009
SELECT CONVERT(VARCHAR(12),GETDATE(), 105)  -- 29-06-2009
SELECT CONVERT(VARCHAR(12),GETDATE(), 104)  -- 29.06.2009
SELECT CONVERT(VARCHAR(12),GETDATE(), 106)  -- 29 Jun 2009

-- Time only
SELECT CONVERT(VARCHAR(12),GETDATE(), 108)  -- 07:26:16
SELECT CONVERT(VARCHAR(12),GETDATE(), 114)  -- 07:27:11:203
Roll Your Own Date
To roll your own date format, use the datename function. This is also the only way to get the full month name.
SELECT DATENAME(MONTH, GETDATE())   -- June
SELECT DATENAME(DAY,GETDATE())   -- 29
SELECT DATENAME(YEAR, GETDATE())      -- 2009

-- Concatente values
-- June.29.2009
SELECT DATENAME(MONTH, GETDATE()) + '.' + DATENAME(DAY,GETDATE()) + '.' +DATENAME(YEAR, GETDATE())
Here are the rest of the possible datepart intervals:
Date Part NameInterval Values
Yearyear, yyyy, yy
Quarterquarter, q, qq
Monthmonth, mm, mm
Day of the yeardayofyear, dy, y
Dayday, dd,. D
Weekdayweekday, dw
Weekweek, wk, ww
Hourhour, hh
Minuteminute, mi, n
Secondsecond, ss, s

SQL Server Date Formats

SQL Server Date Formats
One of the most frequently asked questions in SQL Server forums is how to format a datetime value or column into a specific date format.  Here's a summary of the different date formats that come standard in SQL Server as part of the CONVERT function.  Following the standard date formats are some extended date formats that are often asked by SQL Server developers.
It is worth to note that the output of these date formats are of VARCHAR data types already and not of DATETIME data type.  With this in mind, any date comparisons performed after the datetime value has been formatted are using the VARCHAR value of the date and time and not its original DATETIME value.
Standard Date Formats
Date FormatStandardSQL StatementSample Output
Mon DD YYYY 1
HH:MIAM (or PM)
DefaultSELECT CONVERT(VARCHAR(20), GETDATE(), 100)Jan 1 2005 1:29PM 1
MM/DD/YYUSASELECT CONVERT(VARCHAR(8), GETDATE(), 1) AS [MM/DD/YY]11/23/98
MM/DD/YYYYUSASELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY]11/23/1998
YY.MM.DDANSISELECT CONVERT(VARCHAR(8), GETDATE(), 2) AS [YY.MM.DD]72.01.01
YYYY.MM.DDANSISELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD]1972.01.01
DD/MM/YYBritish/FrenchSELECT CONVERT(VARCHAR(8), GETDATE(), 3) AS [DD/MM/YY]19/02/72
DD/MM/YYYYBritish/FrenchSELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY]19/02/1972
DD.MM.YYGermanSELECT CONVERT(VARCHAR(8), GETDATE(), 4) AS [DD.MM.YY]25.12.05
DD.MM.YYYYGermanSELECT CONVERT(VARCHAR(10), GETDATE(), 104) AS [DD.MM.YYYY]25.12.2005
DD-MM-YYItalianSELECT CONVERT(VARCHAR(8), GETDATE(), 5) AS [DD-MM-YY]24-01-98
DD-MM-YYYYItalianSELECT CONVERT(VARCHAR(10), GETDATE(), 105) AS [DD-MM-YYYY]24-01-1998
DD Mon YY 1-SELECT CONVERT(VARCHAR(9), GETDATE(), 6) AS [DD MON YY]04 Jul 06 1
DD Mon YYYY 1-SELECT CONVERT(VARCHAR(11), GETDATE(), 106) AS [DD MON YYYY]04 Jul 2006 1
Mon DD, YY 1-SELECT CONVERT(VARCHAR(10), GETDATE(), 7) AS [Mon DD, YY]Jan 24, 98 1
Mon DD, YYYY 1-SELECT CONVERT(VARCHAR(12), GETDATE(), 107) AS [Mon DD, YYYY]Jan 24, 1998 1
HH:MM:SS-SELECT CONVERT(VARCHAR(8), GETDATE(), 108)03:24:53
Mon DD YYYY HH:MI:SS:MMMAM (or PM) 1Default +
milliseconds
SELECT CONVERT(VARCHAR(26), GETDATE(), 109)Apr 28 2006 12:32:29:253PM 1
MM-DD-YYUSASELECT CONVERT(VARCHAR(8), GETDATE(), 10) AS [MM-DD-YY]01-01-06
MM-DD-YYYYUSASELECT CONVERT(VARCHAR(10), GETDATE(), 110) AS [MM-DD-YYYY]01-01-2006
YY/MM/DD-SELECT CONVERT(VARCHAR(8), GETDATE(), 11) AS [YY/MM/DD]98/11/23
YYYY/MM/DD-SELECT CONVERT(VARCHAR(10), GETDATE(), 111) AS [YYYY/MM/DD]1998/11/23
YYMMDDISOSELECT CONVERT(VARCHAR(6), GETDATE(), 12) AS [YYMMDD]980124
YYYYMMDDISOSELECT CONVERT(VARCHAR(8), GETDATE(), 112) AS [YYYYMMDD]19980124
DD Mon YYYY HH:MM:SS:MMM(24h) 1Europe default + millisecondsSELECT CONVERT(VARCHAR(24), GETDATE(), 113)28 Apr 2006 00:34:55:190 1
HH:MI:SS:MMM(24H)-SELECT CONVERT(VARCHAR(12), GETDATE(), 114) AS [HH:MI:SS:MMM(24H)]11:34:23:013
YYYY-MM-DD HH:MI:SS(24h)ODBC CanonicalSELECT CONVERT(VARCHAR(19), GETDATE(), 120)1972-01-01 13:42:24
YYYY-MM-DD HH:MI:SS.MMM(24h)ODBC Canonical
(with milliseconds)
SELECT CONVERT(VARCHAR(23), GETDATE(), 121)1972-02-19 06:35:24.489
YYYY-MM-DDTHH:MM:SS:MMMISO8601SELECT CONVERT(VARCHAR(23), GETDATE(), 126)1998-11-23T11:25:43:250
DD Mon YYYY HH:MI:SS:MMMAM1KuwaitiSELECT CONVERT(VARCHAR(26), GETDATE(), 130)28 Apr 2006 12:39:32:429AM 1
DD/MM/YYYY HH:MI:SS:MMMAMKuwaitiSELECT CONVERT(VARCHAR(25), GETDATE(), 131)28/04/2006 12:39:32:429AM


Here are some more date formats that does not come standard in SQL Server as part of the CONVERT function.
Extended Date Formats
Date FormatSQL StatementSample Output
YY-MM-DD
SELECT SUBSTRING(CONVERT(VARCHAR(10), GETDATE(), 120), 3, 8) AS [YY-MM-DD]
SELECT REPLACE(CONVERT(VARCHAR(8), GETDATE(), 11), '/', '-') AS [YY-MM-DD]
99-01-24
YYYY-MM-DD
SELECT CONVERT(VARCHAR(10), GETDATE(), 120) AS [YYYY-MM-DD]
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 111), '/', '-') AS [YYYY-MM-DD]
1999-01-24
MM/YYSELECT RIGHT(CONVERT(VARCHAR(8), GETDATE(), 3), 5) AS [MM/YY]
SELECT SUBSTRING(CONVERT(VARCHAR(8), GETDATE(), 3), 4, 5) AS [MM/YY]
08/99
MM/YYYYSELECT RIGHT(CONVERT(VARCHAR(10), GETDATE(), 103), 7) AS [MM/YYYY]12/2005
YY/MMSELECT CONVERT(VARCHAR(5), GETDATE(), 11) AS [YY/MM]99/08
YYYY/MMSELECT CONVERT(VARCHAR(7), GETDATE(), 111) AS [YYYY/MM]2005/12
Month DD, YYYY 1SELECT DATENAME(MM, GETDATE()) + RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY]July 04, 2006 1
Mon YYYY 1SELECT SUBSTRING(CONVERT(VARCHAR(11), GETDATE(), 113), 4, 8) AS [Mon YYYY]Apr 2006 1
Month YYYY1SELECT DATENAME(MM, GETDATE()) + ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [Month YYYY]February 20061
DD Month 1SELECT CAST(DAY(GETDATE()) AS VARCHAR(2)) + ' ' + DATENAME(MM, GETDATE()) AS [DD Month]11 September1
Month DD 1SELECT DATENAME(MM, GETDATE()) + ' ' + CAST(DAY(GETDATE()) AS VARCHAR(2)) AS [Month DD]September 111
DD Month YY 1SELECT CAST(DAY(GETDATE()) AS VARCHAR(2)) + ' ' + DATENAME(MM, GETDATE()) + ' ' + RIGHT(CAST(YEAR(GETDATE()) AS VARCHAR(4)), 2) AS [DD Month YY]19 February 721
DD Month YYYY 1SELECT CAST(DAY(GETDATE()) AS VARCHAR(2)) + ' ' + DATENAME(MM, GETDATE()) + ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [DD Month YYYY]11 September 2002 1
MM-YYSELECT RIGHT(CONVERT(VARCHAR(8), GETDATE(), 5), 5) AS [MM-YY]
SELECT SUBSTRING(CONVERT(VARCHAR(8), GETDATE(), 5), 4, 5) AS [MM-YY]
12/92
MM-YYYYSELECT RIGHT(CONVERT(VARCHAR(10), GETDATE(), 105), 7) AS [MM-YYYY]05-2006
YY-MMSELECT RIGHT(CONVERT(VARCHAR(7), GETDATE(), 120), 5) AS [YY-MM]
SELECT SUBSTRING(CONVERT(VARCHAR(10), GETDATE(), 120), 3, 5) AS [YY-MM]
92/12
YYYY-MMSELECT CONVERT(VARCHAR(7), GETDATE(), 120) AS [YYYY-MM]2006-05
MMDDYYSELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 1), '/', '') AS [MMDDYY]122506
MMDDYYYYSELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 101), '/', '') AS [MMDDYYYY]12252006
DDMMYYSELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 3), '/', '') AS [DDMMYY]240702
DDMMYYYYSELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 103), '/', '') AS [DDMMYYYY]24072002
Mon-YY 1SELECT REPLACE(RIGHT(CONVERT(VARCHAR(9), GETDATE(), 6), 6), ' ', '-') AS [Mon-YY]Sep-02 1
Mon-YYYY 1SELECT REPLACE(RIGHT(CONVERT(VARCHAR(11), GETDATE(), 106), 8), ' ', '-') AS [Mon-YYYY]Sep-2002 1
DD-Mon-YY1SELECT REPLACE(CONVERT(VARCHAR(9), GETDATE(), 6), ' ', '-') AS [DD-Mon-YY]25-Dec-05 1
DD-Mon-YYYY 1SELECT REPLACE(CONVERT(VARCHAR(11), GETDATE(), 106), ' ', '-') AS [DD-Mon-YYYY]25-Dec-2005 1
1 To make the month name in upper case, simply use the UPPER string function.

Friday, February 28, 2014

Connect database using Servlet & JSP in java web project


  • First, need to copy JDBC drivers into WEB-INF\lib folder. It will come up under "Web App Libraries" automatically.






































  • Second, create a java class for connecting to SQL Server database. 


























  • Third, create Servlet.























  • Fourth, create JSP page.





















Thursday, February 27, 2014

Developers Rank Best Application Servers

Developers Rank Best Application Servers: WebSphere, Apache Geronimo, Windows Server Top List

What's the best all-around application server? According to an Evans Data survey, developers rank IBM WebSphere, Apache Geronimo and Windows Server at the top of the list.

CIO — The "user's choice" for application servers, according to more than 700 software developers, include two of the oldest—one might say mature—and one relative newcomer. Developers ranked IBM WebSphere, the open-source Apache Geronimo and Windows Server among their favorite options, according to a free reportdistributed by Evans Data (free registration required).

Enterprise software developers are, perhaps, all too familiar with application servers: server-based software that can be called by client applications. Web servers are a subset which exclusively handle HTTP requests; in contrast, application servers can use any number of protocols to serve business logic to programs.
Evans Data interviewed more than 700 developers, asking them to rate 21 characteristics of application servers that they had personally used. Among the features and capabilities rated were performance, security features, database connectivity, scalability, support, diagnostics, event logs, and value to cost ratio.
In this survey, Adobe ColdFusion, Red Hat JBoss and Sun Java System Application Server/GlassFish also earned high marks from their users. SAP NetWeaver was also evaluated for their niche uses. And then there's WebLogic....
IBM's WebSphere ranked at the top in 10 of the 21 categories, including those which were identifies as being the most important to developers: performance, scalability, support and diagnostics.
Microsoft's Windows Server is used both as an operating system and as an application server, with its native support for ASP.NET Web development and Web Services technologies such as XMLSOAPUDDI (Universal Description, Discovery and Integration), and WSDSL (Web Services Description Language). In this survey, Windows Server garnered good marks across the board, but delivered the most satisfaction for its database connectivity, support and performance.
But Windows Server is beat out by a candidate from the open-source community: Geronimo, from the Apache Software Foundation. "As an open source program, Geronimo might have been burdened by the perception amongst users of a lack of support," says the report, because open-source software is often criticized by developers for its tech support qualities (or lack thereof). "However, this was not the case with Geronimo, and much of that has to do with IBM. IBM has provided resources and support in a variety of ways to Geronimo and the Apache Software Foundation, including technical support." As a result, Geronimo earned second place marks for quality of support, right behind IBM WebSphere. It also gets top marks from developers for performance and database connectivity.
Another open-source success, JBoss, was marked as delivering the best value to cost ratio, the best compatibility with other software and the best security—all of which matter particularly for enterprise software development.

Original link:
http://www.cio.com/article/455845/Developers_Rank_Best_Application_Servers_WebSphere_Apache_Geronimo_Windows_Server_Top_List?page=1&taxonomyId=3038