–Add a column to an existing table
ALTER TABLE dbo.Employee
ADD Active varchar(1)
GO
–Add default to new column
ALTER TABLE dbo.Employee
ADD CONSTRAINT Employee_Active
DEFAULT (‘N’) FOR Active
GO
Posted by Greg Owen on July 3, 2012
–Add a column to an existing table
ALTER TABLE dbo.Employee
ADD Active varchar(1)
GO
–Add default to new column
ALTER TABLE dbo.Employee
ADD CONSTRAINT Employee_Active
DEFAULT (‘N’) FOR Active
GO
Posted in Visual Studio Code Examples | Tagged: add column, add constraint, sql example, SQL Server, Technology | Leave a Comment »
Posted by Greg Owen on June 6, 2012
–List Column Names
SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = ‘YOUR_DB_NAME’
–List Tables
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_CATALOG = ‘YOUR_DB_NAME’
–List Views
SELECT *
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_CATALOG = ‘YOUR_DB_NAME’
–List All Columns in All Views
SELECT *
FROM INFORMATION_SCHEMA.VIEW_COLUMN_USAGE
WHERE TABLE_CATALOG = ‘YOUR_DB_NAME’
–List Stored Procedures
SELECT *
FROM sys.procedures
–List a Stored Procedure Script
EXEC sp_HelpText N’YourStoredProc’
Posted in SQL Server | Tagged: SQL Column List, SQL Display Stored Procedure, SQL Server, SQL Server Table List, sql server tips, Technology | Leave a Comment »
Posted by Greg Owen on May 15, 2012
–String Value
ALTER TABLE [Table1] ADD CONSTRAINT [Table1_Column1] DEFAULT (‘Not Applicable’) FOR [Column1]
GO
–Numeric Value
ALTER TABLE [Table1] ADD CONSTRAINT [Table1_Column1] DEFAULT ((0)) FOR [Column1]
GO
–Date Value
ALTER TABLE [Table1] ADD CONSTRAINT [Table1_Column1] DEFAULT (((1)/(1))/(1900)) FOR [Column1]
GO
Posted in SQL Server | Tagged: Alter Column, Alter Table, SQL Server, SQL Server Alter Column, SQL Server Alter Table, sql server tips, Technology | Leave a Comment »
Posted by Greg Owen on March 6, 2012
You can create a copy of a table using the SELECT INTO command.
SELECT * INTO TABLE2 FROM TABLE1
You can also join two or more tables and create a new table from the joined tables.
SELECT * INTO TABLE3 FROM TABLE1 a LEFT OUTER JOIN TABLE2 b ON a.field1 = b.field5
The newly created table will not have a primary key even if the existing table or tables do have a primary key.
Posted in SQL Server | Tagged: create a table from a sql statement, left outer join syntax, select into, select statement, SQL Server, sql server tip, Technology | Leave a Comment »
Posted by Greg Owen on February 22, 2012
select datepart(quarter,getdate())
–Get Week Day Numerical value, Sunday = 1st day of week
SELECT
CASE WHEN datepart(weekday,getdate()) = 1 THEN ‘Sunday’
WHEN datepart(weekday,getdate()) = 2 THEN ‘Monday’
WHEN datepart(weekday,getdate()) = 3 THEN ‘Tuesday’
WHEN datepart(weekday,getdate()) = 4 THEN ‘Wednesday’
WHEN datepart(weekday,getdate()) = 5 THEN ‘Thursday’
WHEN datepart(weekday,getdate()) = 6 THEN ‘Friday’
WHEN datepart(weekday,getdate()) = 7 THEN ‘Saturday’END
DECLARE @Date1 datetime
SET @Date1 = ’01/01/2020′
SELECT
CASE WHEN datepart(weekday,@Date1) = 1 THEN ‘Sunday’
WHEN datepart(weekday,@Date1) = 2 THEN ‘Monday’
WHEN datepart(weekday,@Date1) = 3 THEN ‘Tuesday’
WHEN datepart(weekday,@Date1) = 4 THEN ‘Wednesday’
WHEN datepart(weekday,@Date1) = 5 THEN ‘Thursday’
WHEN datepart(weekday,@Date1) = 6 THEN ‘Friday’
WHEN datepart(weekday,@Date1) = 7 THEN ‘Saturday’END
–Date Format DDD, MMM, DD, YYYY
DECLARE @Date2 datetime
SET @Date2 = ’01/01/2020′
SELECT
CASE WHEN datepart(weekday,@Date2) = 1 THEN ‘Sun’
WHEN datepart(weekday,@Date2) = 2 THEN ‘Mon’
WHEN datepart(weekday,@Date2) = 3 THEN ‘Tue’
WHEN datepart(weekday,@Date2) = 4 THEN ‘Wed’
WHEN datepart(weekday,@Date2) = 5 THEN ‘Thu’
WHEN datepart(weekday,@Date2) = 6 THEN ‘Fri’
WHEN datepart(weekday,@Date2) = 7 THEN ‘Sat’END
+ ‘, ‘ + convert(nvarchar(3),@Date2, 107) + ‘, ‘ + convert(nvarchar(2),@Date2,113) + ‘, ‘ + convert(nvarchar(4),year(@Date2))
select datepart(month,getdate())
select datepart(day,getdate())
select datepart(year,getdate())
select datepart(week,getdate())
select datepart(weekday,getdate())
select datepart(hour,getdate())
select datepart(minute,getdate())
select datepart(second,getdate())
select datepart(millisecond,getdate())
Posted in SQL Server | Tagged: datepart, sql datepart example, sql quick tip, sql server tips, Technology | Leave a Comment »
Posted by Greg Owen on February 20, 2012
Facebook recently changed the way you view photos. By default, when you click on a photo, the photo pop ups over the Facebook page. Previously, when you clicked on a photo, you would be directed to another Facebook page with the photo displayed on the page and previous and next links above the photo.
If you preferred the old way Facebook displayed photos, do the following:
Right Click on the picture.
Select Open link in new tab with the left mouse button.
That is it, you can now view photos without the pop up.
Posted in Facebook Tips | Tagged: Facebook Tips, Facebook view photos, Facebook view photos the old way, Facebook view photos without the pop up, Technology | Leave a Comment »
Posted by Greg Owen on February 20, 2012
–Date Difference Function
–Number of days between two dates
select datediff(day,getdate(),’09/01/2020′)
–Number of months between two dates
select datediff(month,getdate(),’09/01/2020′)
–Number of years between two dates
select datediff(year,getdate(),’09/01/2020′)
Posted in SQL Server | Tagged: date difference, datediff, sql server date difference function, sql server datediff, sql server tips, Technology | 1 Comment »
Posted by Greg Owen on February 9, 2012
–Get Current Date and Time
select getdate()
–Date Format MMM DD YYYY
select convert(nvarchar(12),getdate(),100)
–Date Format MM/DD/YYYY
select convert(nvarchar(10),getdate(),101)
–Date Format YYYY.MM.DD
select convert(nvarchar(10),getdate(),102)
–Date Format DD/MM/YYYY
select convert(nvarchar(10),getdate(),103)
–Date Format DD.MM.YYYY
select convert(nvarchar(10),getdate(),104)
–Date Format DD-MM-YYYY
select convert(nvarchar(10),getdate(),105)
–Date Format DD MMM YYYY
select convert(nvarchar(11),getdate(),106)
–Date Format MMM DD, YYYY
select convert(nvarchar(12),getdate(),107)
–Date Format hh:mm:ss (24hr)
select convert(nvarchar(10),getdate(),108)
–Date Format hh:mm:ss:mmm (AM PM)
select convert(nvarchar(30),getdate(),109)
–Date Format MM-DD-YYYY (AM PM)
select convert(nvarchar(10),getdate(),110)
–Date Format YYYY/MM/DD
select convert(nvarchar(10),getdate(),111)
–Date Format YYYYMMDD
select convert(nvarchar(10),getdate(),112)
–Date Format DD MMM YYYY hh:mm:ss:mmm(24h)
select convert(nvarchar(24),getdate(),113)
–Date Format hh:mm:ss:mmm(24h)
select convert(nvarchar(12),getdate(),114)
–Date Format hh:mm:ss:mmm(24h)
select convert(nvarchar(19),getdate(),120)
–Date Format YYYY-MM-DD hh:mm:ss:mmm(24h)
select convert(nvarchar(23),getdate(),121)
–Date Format YYYY-MM-DDThh:mm:ss:mmm(24h)
select convert(nvarchar(24),getdate(),126)
–Time Format hh:mm PM
select substring(convert(varchar(20), GetDate(), 109), 13, 5) + ‘ ‘ + substring(convert(varchar(30), GetDate(), 109), 25, 2)
–Time Format hh:mm:ss PM
select substring(convert(varchar(20), GetDate(), 109), 13, 8) + ‘ ‘ + substring(convert(varchar(30), GetDate(), 109), 25, 2)
–Date Format YYYY.MM.DD
select convert(nvarchar(10),getdate(),102)
–Date Get Month
select month(getdate())
–Date Get Numerical Day of Month
select day(getdate())
–Date Get Year
select year(getdate())
–Date Get Last Day of Current Month
select convert(nvarchar(10),dateadd(day,-1,convert(nvarchar(2),month(dateadd(month,1,getdate()))) + ‘/1/’ + convert(nvarchar(4),year(dateadd(month,1,getdate())))),101)
–Date Get First Day of Current Month
select convert(nvarchar(10),cast(convert(varchar(2),month(getdate())) + ‘/01/’ + convert(varchar(4),year(getdate())) as datetime),101)
–Date Get Last Day of Current Year
select ’12/31/’ + convert(nvarchar(4),year(dateadd(month,1,getdate())))
–Date Get First Day of Current Year
select ’01/01/’ + convert(varchar(4),year(getdate()))
Posted in SQL Server | Tagged: Database Tips, programming, Quick Tip, SQL Server, sql server date format, sql server tips, Technology | Leave a Comment »