Rss Feed Like Us on facebook Google Plus
Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

March 8, 2013

Check String is Palindrome or Not using T-Sql Script +Reverse Funtion

The palindrome is a word, phrase, or sequence that reads the same backward as forward. 

"A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction."

For example  
A man, a plan, a canal – Panama! is palindrome 
so as Was it a car or a cat I saw?

Palindrome in Sanskrit --

सारस नयना घन जघ
नारचित रतार कलिक हर सार रसा
सार रसारह कलिकर
तारत चिरनाघ जनघ नायनसरसा |
SQL Script

DECLARE @PalinString VARCHAR(256);
SET @PalinString = 'सारस नयना घन जघ नारचित रतार कलिक हर सार रसा सार रसारह कलिकर तारत चिरनाघ जनघ नायनसरसा';
SELECT CASE WHEN REPLACE(@PalinString, ' ', '') = REVERSE(REPLACE(@PalinString, ' ', ''))
THEN 'Palindrome'ELSE 'Not Palindrome' END AS [Answer]

Sql Script - "Was it a car or a cat I saw"

DECLARE @PalinString VARCHAR(256);
SET @PalinString = 'Was it a car or a cat I saw';SELECT CASE WHEN REPLACE(@PalinString, ' ', '') =REVERSE(REPLACE(@PalinString, ' ', ''))THEN 'Palindrome'ELSE 'Not Palindrome' END AS [Answer]

Again, if the word is not Palindrome you can just will get answered as it is not a palindrome. My script currently is removing all the spaces from the string. However, if your string is like A man, a plan, a canal – Panama! you may have to remove the exclamation mark and comma too using the REPLACE function.

Sql Script - " A man, a plan, a canal – Panama! "

DECLARE @PalinString VARCHAR(256);
SET @PalinString = 'A man, a plan, a canal - Panama!';
SELECT CASE WHEN REPLACE(REPLACE(REPLACE(REPLACE(@PalinString, '-',''), '!',''), ',',''), ' ', '')
= REVERSE(REPLACE(REPLACE(REPLACE(REPLACE(@PalinString, '-',''), '!',''), ',',''), ' ', ''))
THEN 'Palindrome'
ELSE 'Not Palindrome' END AS [Answer]
Read More

March 5, 2013

Export Query Results to CSV by using SQLCMD

This is indeed very easy process and very simple command to export any query data. For example we will use AdventureWorks2012 database. Here is the query we will be using for our demonstration.
USE AdventureWorks2012
SELECT TOP 10 sp.BusinessEntityID,sp.TerritoryID, sp.SalesQuota,sp.Bonus, sp.CommissionPctFROM Sales.SalesPerson sp
The above query will return following result set.
Now we can export above data to CSV using SQLCMD using following command.
SQLCMD -S . -d AdventureWorks2012 -Q “SELECT TOP 10 sp.BusinessEntityID, sp.TerritoryID, sp.SalesQuota, sp.Bonus, sp.CommissionPct FROM Sales.SalesPerson sp” -s “,” -o “e:\result.csv”
Generically you can use the following syntax:
SQLCMD -S YourSQLServer -d YourDatabase -U YourUserName -P YourPassword -Q “Your Query” -s “,” -o “C:\Yourfilename.csv”
Now you can go to your file location and open the file and you will see that new csv file created there. When you open the csv file you will notice the results of the query.
Read More

© 2011-2016 Techimpulsion All Rights Reserved.

The content is copyrighted to Tech Impulsion and may not be reproduced on other websites.