Pages

Saturday, May 31, 2014

SQL Function Format( )

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. Do you think January 5th 2013 should be shown as 1/5/2013 or 5/1/2013? The best answer is to each their own. The FORMAT function lets you pick the style, or better yet it can be relevant to the culture of the computer running the query.

It is worth to note that the outputs 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.
The FORMAT( ) function is used to format how a field is to be displayed.

SQL FORMAT( ) Syntax:

SELECT FORMAT(column_name,format) FROM table_name;

Wednesday, May 28, 2014

SQL Statement and Syntax

SQL Statement
Syntax
CREATE DATABASE
CREATE DATABASE database_name
CREATE TABLE
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name2 data_type,
...
)
ALTER TABLE
ALTER TABLE table_name
ADD column_name datatype
or
ALTER TABLE table_name
DROP COLUMN column_name
CREATE INDEX
CREATE INDEX index_name
ON table_name (column_name)
or
CREATE UNIQUE INDEX index_name
ON table_name (column_name)