Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Saturday, 4 February 2012

Import CSV Data into Sql Server Table

Getting a CSV file into a SQL Server table?

Found an even easier way to do it...

CREATE TABLE TelephoneNumbers
(TelephoneNumber NVarChar(15))
GO

BULK INSERT TelephoneNumbersFROM 'c:\TelephoneNumbers.csv'
WITH
(
 FIELDTERMINATOR = ',',
 ROWTERMINATOR = '\n'
)
GO

The code is self explanatory; I was surprised how easy it was!'

Friday, 11 November 2011

Calculating MySQL Database Size

A neat script for retrieving database size in MB. I found it useful for checking on Amazon RDS instances.

SELECT table_schema "Database", SUM( data_length + index_length) / 1024 / 1024 "Size (MB)" 
FROM information_schema.TABLES 
GROUP BY table_schema ;

Wednesday, 8 June 2011

T-SQL Simple Search & Replace on Table Content

This was thrown together in an attempt to save some time having to edit a hefty amount of text content. Simple amend the below script with your table, field, text you want replaced, what you want to replace it with and hey ho - you've hacked your content!

DECLARE @TABLE  VARCHAR(30) 
DECLARE @FIELD VARCHAR(30) 
DECLARE @WHERE VARCHAR(100) 
DECLARE @FIND VARCHAR(100) 
DECLARE @REPLACEWITH  VARCHAR(100) 
DECLARE @query VARCHAR(8000) 

SET @TABLE = 'MyTableName' 
SET @FIELD= 'MyField'
SET @FIND =  'text i want replacing' 
SET @REPLACEWITH = 'new text' 

BEGIN     
SET @query  = 
'UPDATE ' +  @TABLE +      
' SET ' +  @FIELD + 
'= REPLACE(CONVERT(varchar(8000),'     + 
@FIELD + '),''' +  @FIND + ''',''' + 
@REPLACEWITH  +''')'   

EXECUTE (@query) END 
GO