IN operator we use when we Determines whether a specified value matches any value in a sub query or a list. The IN operator allows you to specify multiple values in a WHERE clause.it also helps reduce the need to use multiple OR conditions. Continue reading “Using IN in SQL Server”
Tag: Sql functions
DATEPART() function in SQL Server
DATEPART() function returns a value equal to the part of a specified date.
Summery
DATEPART(datepart, date)
where date is the valid date or a string in date format
and datepart can be one of the following:
datepart | Abbreviation |
---|---|
year | yy, yyyy |
quarter | qq, q |
month | mm, m |
dayofyear | dy, y |
day | dd, d |
week | wk, ww |
weekday | dw, w |
hour | hh |
minute | mi, n |
second | ss, s |
millisecond | ms |
Example
Suppose you have a table ‘tbl_employee’
tbl_employee
Employee Id | EmpName | Age | DateOfJoin |
1 | Ankur | 28 | 11/04/1995 |
2 | David | 32 | 11/04/2002 |
The following query return the date of joining of the Ankur:
SELECT datepart("year", DateOfJoin)
Output will be:
—————–
1995
Now if you want to current year, you can use the following query:
SELECT DATEPART("Year",GETDATE())
–or—
SELECT DATEPART("yy",GETDATE())
GETDATE() function return the current date.
DATEDIFF() function in SQL Server
DATEDIFF() function is very useful when we are working with date operations. DATEDIFF function return the difference between date items in days, weeks, minutes, years and hours. It can also used with WHERE clause. Continue reading “DATEDIFF() function in SQL Server”
STR(), CHAR(), and ASCII() function in SQL Server
STR() function
In Sql Server STR() function converts a numeric value to character data. STR() function allows us to specify the total length and number of decimal places to be included when it is converted to character data. Continue reading “STR(), CHAR(), and ASCII() function in SQL Server”
DATALENGTH() AND COL_LENGTH() in SQL Server
DATALENGTH
DATALENGTH function returns the number of bytes used to represent any expression. DATALENGTH is especially very useful with varchar, varbinary, text, image, nvarchar, and ntext data types because these data types can store variable-length data. Continue reading “DATALENGTH() AND COL_LENGTH() in SQL Server”
ISNULL in SQL Server
ISNULL function replaces NULL values with the specified replacement value. you can use ISNULL as Continue reading “ISNULL in SQL Server”
GETDATE() function in SQL Server
The GETDATA() function return the current date and time. This value is derived from the operating system of the computer on which the instance of SQL Server is running. Continue reading “GETDATE() function in SQL Server”
USER_ID() and USER_NAME() in SQL Server
You can pass a name string into USER_ID() function and get the current database ID for that name. This is very useful when we are accessing the system tables and need to check names against group membership or permission value.
Continue reading “USER_ID() and USER_NAME() in SQL Server”
SPACE() and REPLACE() functions in SQL Server
SPACE()
The SPACE() function returns the specified number of the spaces. This function is mainly useful for formatting.
Continue reading “SPACE() and REPLACE() functions in SQL Server”
SUBSTRING() function in SQL Server
The SUBSTRING() function returns part of a character, text, binary, or image expression.