Important SQL Query
2011-02-25 21:17:33.933
Result
2011-02-25(getting only date from datetime)
Result
11:39:58(getting only time from datetime)
Result
2011(getting only year from datetime)
Result
getting the total count of tables
Result
April 04, 2016
+ ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [Month DD YYYY]
Result
April 04 2016
Result
06/21/2016
SELECT CONVERT(varchar(11),getdate(),103) -- dd/mm/yyyy
Result
21/06/2016
select REPLACE(CONVERT(CHAR(10), getdate(), 103), '/', '-') --dd-mm-yyyy
Result
21-06-2016
Eg:
SELECT LEFT (your_column, 4) FROM your_table
Result
Getting the character based on the number
CHARINDEX('.', REVERSE(FileName)) + 1, 999)) from your Column Name
Result
Getting file.pdf as file
Example
SELECT * FROM table_Name WHERE MONTH(column)=4 AND YEAR(column)=2016
Here we are getting the datas from April 2016
AND Month(columnname) = Month(CURRENT_TIMESTAMP)
The above query is used for getting all the records of the current month.
from tablename
Result
13:19 pm
Result
01:19 pm
select datediff(dd,dateadd(dd, 1-day(@sample),@sample), dateadd(m,1,dateadd(dd, 1-day(@sample),@sample)))
Here if the month is January we are getting the result as 31(Total days of January)
@FirstDateOfYear DATETIME,
@LastDateOfYear DATETIME
-- You can change @year to any year you desire
SELECT @year = 2016
SELECT @FirstDateOfYear = DATEADD(yyyy, @Year - 1900, 0)
SELECT @LastDateOfYear = DATEADD(yyyy, @Year - 1900 + 1, 0)
-- Creating Query to Prepare Year Data
;WITH cte AS (
SELECT 1 AS DayID,
@FirstDateOfYear AS FromDate,
DATENAME(dw, @FirstDateOfYear) AS Dayname
UNION ALL
SELECT cte.DayID + 1 AS DayID,
DATEADD(d, 1 ,cte.FromDate),
DATENAME(dw, DATEADD(d, 1 ,cte.FromDate)) AS Dayname
FROM cte
WHERE DATEADD(d,1,cte.FromDate) < @LastDateOfYear
)
SELECT FromDate AS Date, Dayname
FROM CTE
WHERE DayName IN ('Sunday')
/*
WHERE DayName IN ('Saturday,Sunday') -- For Weekend
WHERE DayName NOT IN ('Saturday','Sunday') -- For Weekday
WHERE DayName LIKE 'Monday' -- For Monday
WHERE DayName LIKE 'Sunday' -- For Sunday
*/
OPTION (MaxRecursion 370)
Result
Getting all the Sundays of current year
Result
21hr16mins
Result
Apr 2016
ALTER TABLE table_name ADD column datatype;
Result
By using,ALTER TABLE lets you can add columns to a table in a database.
SELECT AVG(column_name)
FROM table_name;
Result
AVG() is an aggregate function that returns the average value for a numeric column.
Thanks for visiting...
- For selecting only date without time
2011-02-25 21:17:33.933
Result
2011-02-25(getting only date from datetime)
- For select only time without date
Result
11:39:58(getting only time from datetime)
- For selecting only year
Result
2011(getting only year from datetime)
- For combine count of multiple tables
Result
getting the total count of tables
- For selecting date with month name
Result
April 04, 2016
- For selecting date with month name without comma
+ ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [Month DD YYYY]
Result
April 04 2016
- For selecting date in different formates
Result
06/21/2016
SELECT CONVERT(varchar(11),getdate(),103) -- dd/mm/yyyy
Result
21/06/2016
select REPLACE(CONVERT(CHAR(10), getdate(), 103), '/', '-') --dd-mm-yyyy
Result
21-06-2016
- For select particular character
Eg:
SELECT LEFT (your_column, 4) FROM your_table
Result
Getting the character based on the number
- For select filename without extension
CHARINDEX('.', REVERSE(FileName)) + 1, 999)) from your Column Name
Result
Getting file.pdf as file
- For select monthly date
Example
SELECT * FROM table_Name WHERE MONTH(column)=4 AND YEAR(column)=2016
Here we are getting the datas from April 2016
- For selecting monthly records
AND Month(columnname) = Month(CURRENT_TIMESTAMP)
The above query is used for getting all the records of the current month.
- For select time as am,pm format(in railway time)
from tablename
Result
13:19 pm
- For select time as am,pm format(in normal time)
Result
01:19 pm
- For select total days of current month
select datediff(dd,dateadd(dd, 1-day(@sample),@sample), dateadd(m,1,dateadd(dd, 1-day(@sample),@sample)))
Here if the month is January we are getting the result as 31(Total days of January)
- For select all sundays of a month
@FirstDateOfYear DATETIME,
@LastDateOfYear DATETIME
-- You can change @year to any year you desire
SELECT @year = 2016
SELECT @FirstDateOfYear = DATEADD(yyyy, @Year - 1900, 0)
SELECT @LastDateOfYear = DATEADD(yyyy, @Year - 1900 + 1, 0)
-- Creating Query to Prepare Year Data
;WITH cte AS (
SELECT 1 AS DayID,
@FirstDateOfYear AS FromDate,
DATENAME(dw, @FirstDateOfYear) AS Dayname
UNION ALL
SELECT cte.DayID + 1 AS DayID,
DATEADD(d, 1 ,cte.FromDate),
DATENAME(dw, DATEADD(d, 1 ,cte.FromDate)) AS Dayname
FROM cte
WHERE DATEADD(d,1,cte.FromDate) < @LastDateOfYear
)
SELECT FromDate AS Date, Dayname
FROM CTE
WHERE DayName IN ('Sunday')
/*
WHERE DayName IN ('Saturday,Sunday') -- For Weekend
WHERE DayName NOT IN ('Saturday','Sunday') -- For Weekday
WHERE DayName LIKE 'Monday' -- For Monday
WHERE DayName LIKE 'Sunday' -- For Sunday
*/
OPTION (MaxRecursion 370)
Result
Getting all the Sundays of current year
- Select sum of time
Result
21hr16mins
- Select only month and year without day
Result
Apr 2016
- ALTER TABLE(Add additional column to a table)
ALTER TABLE table_name ADD column datatype;
Result
By using,ALTER TABLE lets you can add columns to a table in a database.
- AVG
SELECT AVG(column_name)
FROM table_name;
Result
AVG() is an aggregate function that returns the average value for a numeric column.
Thanks for visiting...