SQL Server SELECT into existing table

SELECT … INTO … only works if the table specified in the INTO clause does not exist – otherwise, you have to use: INSERT INTO dbo.TABLETWO SELECT col1, col2 FROM dbo.TABLEONE WHERE col3 LIKE @search_key This assumes there’s only two columns in dbo.TABLETWO – you need to specify the columns otherwise: INSERT INTO dbo.TABLETWO (col1, … Read more

What is the meaning of the prefix N in T-SQL statements and when should I use it?

It’s declaring the string as nvarchar data type, rather than varchar You may have seen Transact-SQL code that passes strings around using an N prefix. This denotes that the subsequent string is in Unicode (the N actually stands for National language character set). Which means that you are passing an NCHAR, NVARCHAR or NTEXT value, … Read more

SQL MAX of multiple columns?

Here is another nice solution for the Max functionality using T-SQL and SQL Server SELECT [Other Fields], (SELECT Max(v) FROM (VALUES (date1), (date2), (date3),…) AS value(v)) as [MaxDate] FROM [YourTableName] Values is the Table Value Constructor. “Specifies a set of row value expressions to be constructed into a table. The Transact-SQL table value constructor allows … Read more

Pad a string with leading zeros so it’s 3 characters long in SQL Server 2008

If the field is already a string, this will work SELECT RIGHT(‘000’+ISNULL(field,”),3) If you want nulls to show as ‘000’ It might be an integer — then you would want SELECT RIGHT(‘000’+CAST(field AS VARCHAR(3)),3) As required by the question this answer only works if the length <= 3, if you want something larger you need … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)