Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| tech-notes:sql [2021/05/14 11:39] – [6 Digit Random Number] gerardorourke | tech-notes:sql [2022/12/21 14:06] (current) – [SUBSTRING and CHARINDEX] gerardorourke | ||
|---|---|---|---|
| Line 34: | Line 34: | ||
| as MaxIncomePerDay | as MaxIncomePerDay | ||
| where VendorId in (' | where VendorId in (' | ||
| + | </ | ||
| + | |||
| + | ===== DateTime ===== | ||
| + | ====Convert DateTime to Just Date==== | ||
| + | <code sql> | ||
| + | CONVERT(Datetime, | ||
| + | </ | ||
| + | |||
| + | ====Convert DateTime to Date + Hour==== | ||
| + | <code sql> | ||
| + | Date = DATEADD(hour, | ||
| + | </ | ||
| + | |||
| + | ====Convert DateTime to Just the Year==== | ||
| + | < | ||
| + | |||
| + | ====Convert DateTime to Just the Month==== | ||
| + | < | ||
| + | |||
| + | ====Convert DateTime to the week ==== | ||
| + | < | ||
| + | CASE WHEN DATEPART(week, | ||
| + | THEN DATEADD(day, | ||
| + | ELSE DATEADD(day, | ||
| </ | </ | ||
| Line 53: | Line 77: | ||
| ==CONVERT== | ==CONVERT== | ||
| http:// | http:// | ||
| - | |||
| - | ==Convert DateTime to Just Date== | ||
| - | <code sql> | ||
| - | CONVERT(Datetime, | ||
| - | </ | ||
| - | |||
| - | ==Convert DateTime to Date + Hour== | ||
| - | <code sql> | ||
| - | Date = DATEADD(hour, | ||
| - | </ | ||
| Line 263: | Line 277: | ||
| https:// | https:// | ||
| - | Ref: https:// | ||
| - | <code sql> | ||
| - | CREATE TABLE dbo.RandomNumbers | ||
| - | ( | ||
| - | RowID INT, | ||
| - | Value INT, --UNIQUE, | ||
| - | PRIMARY KEY (RowID, Value) | ||
| - | ); | ||
| - | |||
| - | ;WITH x AS | ||
| - | ( | ||
| - | SELECT TOP (1000000) s1.[object_id] | ||
| - | FROM sys.all_objects AS s1 | ||
| - | CROSS JOIN sys.all_objects AS s2 | ||
| - | ORDER BY s1.[object_id] | ||
| - | ) | ||
| - | INSERT dbo.RandomNumbers(RowID, | ||
| - | SELECT | ||
| - | r = ROW_NUMBER() OVER (ORDER BY [object_id]), | ||
| - | n = ROW_NUMBER() OVER (ORDER BY NEWID()) | ||
| - | FROM x | ||
| - | ORDER BY r; | ||
| - | |||
| - | |||
| - | DELETE FROM [dbo].[RandomNumbers] Where Len(Value) <> 6 | ||
| - | </ | ||
| - | |||
| - | This query proves that there are no duplicate records | ||
| - | < | ||
| - | select Value , Count(*) | ||
| - | from RandomNumbers | ||
| - | Group By Value | ||
| - | HAVING COUNT(*) > 1 | ||
| - | </ | ||
| ===== SQL - Querying Remote Data Source ===== | ===== SQL - Querying Remote Data Source ===== | ||
| *https:// | *https:// | ||
| Line 328: | Line 308: | ||
| *https:// | *https:// | ||
| + | |||
| + | ===== SUBSTRING and CHARINDEX ===== | ||
| + | |||
| + | Here we have a column ' | ||
| + | < | ||
| + | [FirstVariable] = SUBSTRING([myColumn], | ||
| + | [SecondVariable] = SUBSTRING([myColumn], | ||
| + | </ | ||
| + | |||
| + | CHARINDEX find the comma and lets you know the position it is in. | ||
| + | SUBSTRING uses start and end position to extract a substring. | ||